aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/opentelemetry/metrics.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/opentelemetry/metrics.ts')
-rw-r--r--server/lib/opentelemetry/metrics.ts123
1 files changed, 0 insertions, 123 deletions
diff --git a/server/lib/opentelemetry/metrics.ts b/server/lib/opentelemetry/metrics.ts
deleted file mode 100644
index bffe00840..000000000
--- a/server/lib/opentelemetry/metrics.ts
+++ /dev/null
@@ -1,123 +0,0 @@
1import { Application, Request, Response } from 'express'
2import { Meter, metrics } from '@opentelemetry/api'
3import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'
4import { MeterProvider } from '@opentelemetry/sdk-metrics'
5import { logger } from '@server/helpers/logger'
6import { CONFIG } from '@server/initializers/config'
7import { MVideoImmutable } from '@server/types/models'
8import { PlaybackMetricCreate } from '@shared/models'
9import {
10 BittorrentTrackerObserversBuilder,
11 JobQueueObserversBuilder,
12 LivesObserversBuilder,
13 NodeJSObserversBuilder,
14 PlaybackMetrics,
15 StatsObserversBuilder,
16 ViewersObserversBuilder
17} from './metric-helpers'
18
19class OpenTelemetryMetrics {
20
21 private static instance: OpenTelemetryMetrics
22
23 private meter: Meter
24
25 private onRequestDuration: (req: Request, res: Response) => void
26
27 private playbackMetrics: PlaybackMetrics
28
29 private constructor () {}
30
31 init (app: Application) {
32 if (CONFIG.OPEN_TELEMETRY.METRICS.ENABLED !== true) return
33
34 app.use((req, res, next) => {
35 res.once('finish', () => {
36 if (!this.onRequestDuration) return
37
38 this.onRequestDuration(req as Request, res as Response)
39 })
40
41 next()
42 })
43 }
44
45 registerMetrics (options: { trackerServer: any }) {
46 if (CONFIG.OPEN_TELEMETRY.METRICS.ENABLED !== true) return
47
48 logger.info('Registering Open Telemetry metrics')
49
50 const provider = new MeterProvider({
51 views: [
52 ...NodeJSObserversBuilder.getViews()
53 ]
54 })
55
56 provider.addMetricReader(new PrometheusExporter({
57 host: CONFIG.OPEN_TELEMETRY.METRICS.PROMETHEUS_EXPORTER.HOSTNAME,
58 port: CONFIG.OPEN_TELEMETRY.METRICS.PROMETHEUS_EXPORTER.PORT
59 }))
60
61 metrics.setGlobalMeterProvider(provider)
62
63 this.meter = metrics.getMeter('default')
64
65 if (CONFIG.OPEN_TELEMETRY.METRICS.HTTP_REQUEST_DURATION.ENABLED === true) {
66 this.buildRequestObserver()
67 }
68
69 this.playbackMetrics = new PlaybackMetrics(this.meter)
70 this.playbackMetrics.buildCounters()
71
72 const nodeJSObserversBuilder = new NodeJSObserversBuilder(this.meter)
73 nodeJSObserversBuilder.buildObservers()
74
75 const jobQueueObserversBuilder = new JobQueueObserversBuilder(this.meter)
76 jobQueueObserversBuilder.buildObservers()
77
78 const statsObserversBuilder = new StatsObserversBuilder(this.meter)
79 statsObserversBuilder.buildObservers()
80
81 const livesObserversBuilder = new LivesObserversBuilder(this.meter)
82 livesObserversBuilder.buildObservers()
83
84 const viewersObserversBuilder = new ViewersObserversBuilder(this.meter)
85 viewersObserversBuilder.buildObservers()
86
87 const bittorrentTrackerObserversBuilder = new BittorrentTrackerObserversBuilder(this.meter, options.trackerServer)
88 bittorrentTrackerObserversBuilder.buildObservers()
89 }
90
91 observePlaybackMetric (video: MVideoImmutable, metrics: PlaybackMetricCreate) {
92 this.playbackMetrics.observe(video, metrics)
93 }
94
95 private buildRequestObserver () {
96 const requestDuration = this.meter.createHistogram('http_request_duration_ms', {
97 unit: 'milliseconds',
98 description: 'Duration of HTTP requests in ms'
99 })
100
101 this.onRequestDuration = (req: Request, res: Response) => {
102 const duration = Date.now() - res.locals.requestStart
103
104 requestDuration.record(duration, {
105 path: this.buildRequestPath(req.originalUrl),
106 method: req.method,
107 statusCode: res.statusCode + ''
108 })
109 }
110 }
111
112 private buildRequestPath (path: string) {
113 return path.split('?')[0]
114 }
115
116 static get Instance () {
117 return this.instance || (this.instance = new this())
118 }
119}
120
121export {
122 OpenTelemetryMetrics
123}