PeerTube/server/lib/opentelemetry/metrics.ts

115 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-07-05 15:43:21 +02:00
import { Application, Request, Response } from 'express'
import { Meter, metrics } from '@opentelemetry/api-metrics'
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'
2022-09-09 11:11:47 +02:00
import { MeterProvider } from '@opentelemetry/sdk-metrics'
2022-07-05 15:43:21 +02:00
import { logger } from '@server/helpers/logger'
import { CONFIG } from '@server/initializers/config'
import { MVideoImmutable } from '@server/types/models'
import { PlaybackMetricCreate } from '@shared/models'
2022-07-27 16:19:25 +02:00
import {
JobQueueObserversBuilder,
LivesObserversBuilder,
NodeJSObserversBuilder,
PlaybackMetrics,
2022-07-27 16:19:25 +02:00
StatsObserversBuilder,
ViewersObserversBuilder
} from './metric-helpers'
2022-07-05 15:43:21 +02:00
class OpenTelemetryMetrics {
private static instance: OpenTelemetryMetrics
private meter: Meter
private onRequestDuration: (req: Request, res: Response) => void
private playbackMetrics: PlaybackMetrics
2022-07-05 15:43:21 +02:00
private constructor () {}
init (app: Application) {
if (CONFIG.OPEN_TELEMETRY.METRICS.ENABLED !== true) return
app.use((req, res, next) => {
res.once('finish', () => {
if (!this.onRequestDuration) return
this.onRequestDuration(req as Request, res as Response)
})
next()
})
}
registerMetrics () {
if (CONFIG.OPEN_TELEMETRY.METRICS.ENABLED !== true) return
logger.info('Registering Open Telemetry metrics')
const provider = new MeterProvider({
views: [
...NodeJSObserversBuilder.getViews()
]
})
2022-07-05 15:43:21 +02:00
provider.addMetricReader(new PrometheusExporter({ port: CONFIG.OPEN_TELEMETRY.METRICS.PROMETHEUS_EXPORTER.PORT }))
metrics.setGlobalMeterProvider(provider)
this.meter = metrics.getMeter('default')
this.buildRequestObserver()
this.playbackMetrics = new PlaybackMetrics(this.meter)
this.playbackMetrics.buildCounters()
const nodeJSObserversBuilder = new NodeJSObserversBuilder(this.meter)
2022-07-19 11:58:19 +02:00
nodeJSObserversBuilder.buildObservers()
2022-07-05 15:43:21 +02:00
2022-07-19 11:58:19 +02:00
const jobQueueObserversBuilder = new JobQueueObserversBuilder(this.meter)
jobQueueObserversBuilder.buildObservers()
2022-07-05 15:43:21 +02:00
2022-07-19 11:58:19 +02:00
const statsObserversBuilder = new StatsObserversBuilder(this.meter)
statsObserversBuilder.buildObservers()
2022-07-27 16:19:25 +02:00
const livesObserversBuilder = new LivesObserversBuilder(this.meter)
livesObserversBuilder.buildObservers()
const viewersObserversBuilder = new ViewersObserversBuilder(this.meter)
viewersObserversBuilder.buildObservers()
2022-07-05 15:43:21 +02:00
}
observePlaybackMetric (video: MVideoImmutable, metrics: PlaybackMetricCreate) {
this.playbackMetrics.observe(video, metrics)
}
2022-07-05 15:43:21 +02:00
private buildRequestObserver () {
const requestDuration = this.meter.createHistogram('http_request_duration_ms', {
unit: 'milliseconds',
description: 'Duration of HTTP requests in ms'
})
this.onRequestDuration = (req: Request, res: Response) => {
const duration = Date.now() - res.locals.requestStart
requestDuration.record(duration, {
path: this.buildRequestPath(req.originalUrl),
method: req.method,
statusCode: res.statusCode + ''
})
}
}
private buildRequestPath (path: string) {
return path.split('?')[0]
}
static get Instance () {
return this.instance || (this.instance = new this())
}
}
export {
OpenTelemetryMetrics
}