aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/opentelemetry/metrics.ts
blob: ba33c95059a60b1210e65765a36712a71649563a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Application, Request, Response } from 'express'
import { Meter, metrics } from '@opentelemetry/api-metrics'
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'
import { MeterProvider } from '@opentelemetry/sdk-metrics-base'
import { logger } from '@server/helpers/logger'
import { CONFIG } from '@server/initializers/config'
import { MVideoImmutable } from '@server/types/models'
import { PlaybackMetricCreate } from '@shared/models'
import {
  JobQueueObserversBuilder,
  LivesObserversBuilder,
  NodeJSObserversBuilder,
  PlaybackMetrics,
  StatsObserversBuilder,
  ViewersObserversBuilder
} from './metric-helpers'

class OpenTelemetryMetrics {

  private static instance: OpenTelemetryMetrics

  private meter: Meter

  private onRequestDuration: (req: Request, res: Response) => void

  private playbackMetrics: PlaybackMetrics

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

    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)
    nodeJSObserversBuilder.buildObservers()

    const jobQueueObserversBuilder = new JobQueueObserversBuilder(this.meter)
    jobQueueObserversBuilder.buildObservers()

    const statsObserversBuilder = new StatsObserversBuilder(this.meter)
    statsObserversBuilder.buildObservers()

    const livesObserversBuilder = new LivesObserversBuilder(this.meter)
    livesObserversBuilder.buildObservers()

    const viewersObserversBuilder = new ViewersObserversBuilder(this.meter)
    viewersObserversBuilder.buildObservers()
  }

  observePlaybackMetric (video: MVideoImmutable, metrics: PlaybackMetricCreate) {
    this.playbackMetrics.observe(video, metrics)
  }

  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
}