From 5847724474b9a1d94679e347ca2c24d2b10d0b20 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 5 Jan 2023 10:19:51 +0100 Subject: [PATCH] Add tracker opentelemtry metrics --- server.ts | 5 +- server/controllers/tracker.ts | 2 +- .../bittorrent-tracker-observers-builder.ts | 51 +++++++++++++++++++ .../lib/opentelemetry/metric-helpers/index.ts | 1 + server/lib/opentelemetry/metrics.ts | 6 ++- 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 server/lib/opentelemetry/metric-helpers/bittorrent-tracker-observers-builder.ts diff --git a/server.ts b/server.ts index dd595e951..f6a153fb7 100644 --- a/server.ts +++ b/server.ts @@ -279,7 +279,7 @@ app.use((err, _req, res: express.Response, _next) => { }) }) -const server = createWebsocketTrackerServer(app) +const { server, trackerServer } = createWebsocketTrackerServer(app) // ----------- Run ----------- @@ -328,7 +328,8 @@ async function startApplication () { VideoChannelSyncLatestScheduler.Instance.enable() VideoViewsBufferScheduler.Instance.enable() GeoIPUpdateScheduler.Instance.enable() - OpenTelemetryMetrics.Instance.registerMetrics() + + OpenTelemetryMetrics.Instance.registerMetrics({ trackerServer }) PluginManager.Instance.init(server) // Before PeerTubeSocket init diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts index 0ef98c35e..c4f3a8889 100644 --- a/server/controllers/tracker.ts +++ b/server/controllers/tracker.ts @@ -121,7 +121,7 @@ function createWebsocketTrackerServer (app: express.Application) { // Don't destroy socket, we have Socket.IO too }) - return server + return { server, trackerServer } } // --------------------------------------------------------------------------- diff --git a/server/lib/opentelemetry/metric-helpers/bittorrent-tracker-observers-builder.ts b/server/lib/opentelemetry/metric-helpers/bittorrent-tracker-observers-builder.ts new file mode 100644 index 000000000..ef40c0fa9 --- /dev/null +++ b/server/lib/opentelemetry/metric-helpers/bittorrent-tracker-observers-builder.ts @@ -0,0 +1,51 @@ +import { Meter } from '@opentelemetry/api' + +export class BittorrentTrackerObserversBuilder { + + constructor (private readonly meter: Meter, private readonly trackerServer: any) { + + } + + buildObservers () { + const activeInfohashes = this.meter.createObservableGauge('peertube_bittorrent_tracker_active_infohashes_total', { + description: 'Total active infohashes in the PeerTube BitTorrent Tracker' + }) + const inactiveInfohashes = this.meter.createObservableGauge('peertube_bittorrent_tracker_inactive_infohashes_total', { + description: 'Total inactive infohashes in the PeerTube BitTorrent Tracker' + }) + const peers = this.meter.createObservableGauge('peertube_bittorrent_tracker_peers_total', { + description: 'Total peers in the PeerTube BitTorrent Tracker' + }) + + this.meter.addBatchObservableCallback(observableResult => { + const infohashes = Object.keys(this.trackerServer.torrents) + + const counters = { + activeInfohashes: 0, + inactiveInfohashes: 0, + peers: 0, + uncompletedPeers: 0 + } + + for (const infohash of infohashes) { + const content = this.trackerServer.torrents[infohash] + + const peers = content.peers + if (peers.keys.length !== 0) counters.activeInfohashes++ + else counters.inactiveInfohashes++ + + for (const peerId of peers.keys) { + const peer = peers.peek(peerId) + if (peer == null) return + + counters.peers++ + } + } + + observableResult.observe(activeInfohashes, counters.activeInfohashes) + observableResult.observe(inactiveInfohashes, counters.inactiveInfohashes) + observableResult.observe(peers, counters.peers) + }, [ activeInfohashes, inactiveInfohashes, peers ]) + } + +} diff --git a/server/lib/opentelemetry/metric-helpers/index.ts b/server/lib/opentelemetry/metric-helpers/index.ts index 775d954ba..47b24a54f 100644 --- a/server/lib/opentelemetry/metric-helpers/index.ts +++ b/server/lib/opentelemetry/metric-helpers/index.ts @@ -1,3 +1,4 @@ +export * from './bittorrent-tracker-observers-builder' export * from './lives-observers-builder' export * from './job-queue-observers-builder' export * from './nodejs-observers-builder' diff --git a/server/lib/opentelemetry/metrics.ts b/server/lib/opentelemetry/metrics.ts index 226d514c0..9cc067e4a 100644 --- a/server/lib/opentelemetry/metrics.ts +++ b/server/lib/opentelemetry/metrics.ts @@ -7,6 +7,7 @@ import { CONFIG } from '@server/initializers/config' import { MVideoImmutable } from '@server/types/models' import { PlaybackMetricCreate } from '@shared/models' import { + BittorrentTrackerObserversBuilder, JobQueueObserversBuilder, LivesObserversBuilder, NodeJSObserversBuilder, @@ -41,7 +42,7 @@ class OpenTelemetryMetrics { }) } - registerMetrics () { + registerMetrics (options: { trackerServer: any }) { if (CONFIG.OPEN_TELEMETRY.METRICS.ENABLED !== true) return logger.info('Registering Open Telemetry metrics') @@ -80,6 +81,9 @@ class OpenTelemetryMetrics { const viewersObserversBuilder = new ViewersObserversBuilder(this.meter) viewersObserversBuilder.buildObservers() + + const bittorrentTrackerObserversBuilder = new BittorrentTrackerObserversBuilder(this.meter, options.trackerServer) + bittorrentTrackerObserversBuilder.buildObservers() } observePlaybackMetric (video: MVideoImmutable, metrics: PlaybackMetricCreate) { -- 2.41.0