aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/opentelemetry/metric-helpers/playback-metrics.ts
blob: 1eb08b5a6642b92fe1f29fcb620dcddd1e355439 (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
import { Counter, Meter } from '@opentelemetry/api'
import { MVideoImmutable } from '@server/types/models'
import { PlaybackMetricCreate } from '@shared/models'

export class PlaybackMetrics {
  private errorsCounter: Counter
  private resolutionChangesCounter: Counter

  private downloadedBytesP2PCounter: Counter
  private uploadedBytesP2PCounter: Counter

  private downloadedBytesHTTPCounter: Counter

  private peersP2PPeersGaugeBuffer: {
    value: number
    attributes: any
  }[] = []

  constructor (private readonly meter: Meter) {

  }

  buildCounters () {
    this.errorsCounter = this.meter.createCounter('peertube_playback_errors_count', {
      description: 'Errors collected from PeerTube player.'
    })

    this.resolutionChangesCounter = this.meter.createCounter('peertube_playback_resolution_changes_count', {
      description: 'Resolution changes collected from PeerTube player.'
    })

    this.downloadedBytesHTTPCounter = this.meter.createCounter('peertube_playback_http_downloaded_bytes', {
      description: 'Downloaded bytes with HTTP by PeerTube player.'
    })
    this.downloadedBytesP2PCounter = this.meter.createCounter('peertube_playback_p2p_downloaded_bytes', {
      description: 'Downloaded bytes with P2P by PeerTube player.'
    })

    this.uploadedBytesP2PCounter = this.meter.createCounter('peertube_playback_p2p_uploaded_bytes', {
      description: 'Uploaded bytes with P2P by PeerTube player.'
    })

    this.meter.createObservableGauge('peertube_playback_p2p_peers', {
      description: 'Total P2P peers connected to the PeerTube player.'
    }).addCallback(observableResult => {
      for (const gauge of this.peersP2PPeersGaugeBuffer) {
        observableResult.observe(gauge.value, gauge.attributes)
      }

      this.peersP2PPeersGaugeBuffer = []
    })
  }

  observe (video: MVideoImmutable, metrics: PlaybackMetricCreate) {
    const attributes = {
      videoOrigin: video.remote
        ? 'remote'
        : 'local',

      playerMode: metrics.playerMode,

      resolution: metrics.resolution + '',
      fps: metrics.fps + '',

      p2pEnabled: metrics.p2pEnabled,

      videoUUID: video.uuid
    }

    this.errorsCounter.add(metrics.errors, attributes)
    this.resolutionChangesCounter.add(metrics.resolutionChanges, attributes)

    this.downloadedBytesHTTPCounter.add(metrics.downloadedBytesHTTP, attributes)
    this.downloadedBytesP2PCounter.add(metrics.downloadedBytesP2P, attributes)

    this.uploadedBytesP2PCounter.add(metrics.uploadedBytesP2P, attributes)

    if (metrics.p2pPeers) {
      this.peersP2PPeersGaugeBuffer.push({
        value: metrics.p2pPeers,
        attributes
      })
    }
  }
}