1
0
Fork 0

Add tracker opentelemtry metrics

This commit is contained in:
Chocobozzz 2023-01-05 10:19:51 +01:00
parent 60bab7b540
commit 5847724474
No known key found for this signature in database
GPG key ID: 583A612D890159BE
5 changed files with 61 additions and 4 deletions

View file

@ -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

View file

@ -121,7 +121,7 @@ function createWebsocketTrackerServer (app: express.Application) {
// Don't destroy socket, we have Socket.IO too
})
return server
return { server, trackerServer }
}
// ---------------------------------------------------------------------------

View file

@ -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 ])
}
}

View file

@ -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'

View file

@ -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) {