aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/tests/src/api/server/open-telemetry.ts
blob: 8ed3801db0fe44465a232d407212d73aed0bc8ea (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { HttpStatusCode, PlaybackMetricCreate, VideoPrivacy, VideoResolution } from '@peertube/peertube-models'
import {
  cleanupTests,
  createSingleServer,
  makeRawRequest,
  PeerTubeServer,
  setAccessTokensToServers
} from '@peertube/peertube-server-commands'
import { expectLogDoesNotContain, expectLogContain } from '@tests/shared/checks.js'
import { MockHTTP } from '@tests/shared/mock-servers/mock-http.js'

describe('Open Telemetry', function () {
  let server: PeerTubeServer

  describe('Metrics', function () {
    const metricsUrl = 'http://127.0.0.1:9092/metrics'

    it('Should not enable open telemetry metrics', async function () {
      this.timeout(60000)

      server = await createSingleServer(1)

      let hasError = false
      try {
        await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
      } catch (err) {
        hasError = err.message.includes('ECONNREFUSED')
      }

      expect(hasError).to.be.true

      await server.kill()
    })

    it('Should enable open telemetry metrics', async function () {
      this.timeout(120000)

      await server.run({
        open_telemetry: {
          metrics: {
            enabled: true
          }
        }
      })

      // Simulate a HTTP request
      await server.videos.list()

      const res = await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.OK_200 })
      expect(res.text).to.contain('peertube_job_queue_total{')
      expect(res.text).to.contain('http_request_duration_ms_bucket{')
    })

    it('Should have playback metrics', async function () {
      await setAccessTokensToServers([ server ])

      const video = await server.videos.quickUpload({ name: 'video' })

      await server.metrics.addPlaybackMetric({
        metrics: {
          playerMode: 'p2p-media-loader',
          resolution: VideoResolution.H_1080P,
          fps: 30,
          resolutionChanges: 1,
          errors: 2,
          downloadedBytesP2P: 0,
          downloadedBytesHTTP: 0,
          uploadedBytesP2P: 5,
          p2pPeers: 1,
          p2pEnabled: false,
          videoId: video.uuid
        }
      })

      const res = await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.OK_200 })

      expect(res.text).to.contain('peertube_playback_http_downloaded_bytes_total{')
      expect(res.text).to.contain('peertube_playback_p2p_peers{')
      expect(res.text).to.contain('p2pEnabled="false"')
    })

    it('Should take the last playback metric', async function () {
      await setAccessTokensToServers([ server ])

      const video = await server.videos.quickUpload({ name: 'video' })

      const metrics = {
        playerMode: 'p2p-media-loader',
        resolution: VideoResolution.H_1080P,
        fps: 30,
        resolutionChanges: 1,
        errors: 2,
        downloadedBytesP2P: 0,
        downloadedBytesHTTP: 0,
        uploadedBytesP2P: 5,
        p2pPeers: 7,
        p2pEnabled: false,
        videoId: video.uuid
      } as PlaybackMetricCreate

      await server.metrics.addPlaybackMetric({ metrics })

      metrics.p2pPeers = 42
      await server.metrics.addPlaybackMetric({ metrics })

      const res = await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.OK_200 })

      // eslint-disable-next-line max-len
      const label = `{videoOrigin="local",playerMode="p2p-media-loader",resolution="1080",fps="30",p2pEnabled="false",videoUUID="${video.uuid}"}`
      expect(res.text).to.contain(`peertube_playback_p2p_peers${label} 42`)
      expect(res.text).to.not.contain(`peertube_playback_p2p_peers${label} 7`)
    })

    it('Should disable http request duration metrics', async function () {
      await server.kill()

      await server.run({
        open_telemetry: {
          metrics: {
            enabled: true,
            http_request_duration: {
              enabled: false
            }
          }
        }
      })

      // Simulate a HTTP request
      await server.videos.list()

      const res = await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.OK_200 })
      expect(res.text).to.not.contain('http_request_duration_ms_bucket{')
    })

    after(async function () {
      await server.kill()
    })
  })

  describe('Tracing', function () {
    let mockHTTP: MockHTTP
    let mockPort: number

    before(async function () {
      mockHTTP = new MockHTTP()
      mockPort = await mockHTTP.initialize()
    })

    it('Should enable open telemetry tracing', async function () {
      server = await createSingleServer(1)

      await expectLogDoesNotContain(server, 'Registering Open Telemetry tracing')

      await server.kill()
    })

    it('Should enable open telemetry metrics', async function () {
      await server.run({
        open_telemetry: {
          tracing: {
            enabled: true,
            jaeger_exporter: {
              endpoint: 'http://127.0.0.1:' + mockPort
            }
          }
        }
      })

      await expectLogContain(server, 'Registering Open Telemetry tracing')
    })

    it('Should upload a video and correctly works', async function () {
      await setAccessTokensToServers([ server ])

      const { uuid } = await server.videos.quickUpload({ name: 'video', privacy: VideoPrivacy.PUBLIC })

      const video = await server.videos.get({ id: uuid })

      expect(video.name).to.equal('video')
    })

    after(async function () {
      await mockHTTP.terminate()
    })
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})