diff options
Diffstat (limited to 'packages/tests/src/api/server/open-telemetry.ts')
-rw-r--r-- | packages/tests/src/api/server/open-telemetry.ts | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/packages/tests/src/api/server/open-telemetry.ts b/packages/tests/src/api/server/open-telemetry.ts new file mode 100644 index 000000000..8ed3801db --- /dev/null +++ b/packages/tests/src/api/server/open-telemetry.ts | |||
@@ -0,0 +1,193 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { HttpStatusCode, PlaybackMetricCreate, VideoPrivacy, VideoResolution } from '@peertube/peertube-models' | ||
5 | import { | ||
6 | cleanupTests, | ||
7 | createSingleServer, | ||
8 | makeRawRequest, | ||
9 | PeerTubeServer, | ||
10 | setAccessTokensToServers | ||
11 | } from '@peertube/peertube-server-commands' | ||
12 | import { expectLogDoesNotContain, expectLogContain } from '@tests/shared/checks.js' | ||
13 | import { MockHTTP } from '@tests/shared/mock-servers/mock-http.js' | ||
14 | |||
15 | describe('Open Telemetry', function () { | ||
16 | let server: PeerTubeServer | ||
17 | |||
18 | describe('Metrics', function () { | ||
19 | const metricsUrl = 'http://127.0.0.1:9092/metrics' | ||
20 | |||
21 | it('Should not enable open telemetry metrics', async function () { | ||
22 | this.timeout(60000) | ||
23 | |||
24 | server = await createSingleServer(1) | ||
25 | |||
26 | let hasError = false | ||
27 | try { | ||
28 | await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | ||
29 | } catch (err) { | ||
30 | hasError = err.message.includes('ECONNREFUSED') | ||
31 | } | ||
32 | |||
33 | expect(hasError).to.be.true | ||
34 | |||
35 | await server.kill() | ||
36 | }) | ||
37 | |||
38 | it('Should enable open telemetry metrics', async function () { | ||
39 | this.timeout(120000) | ||
40 | |||
41 | await server.run({ | ||
42 | open_telemetry: { | ||
43 | metrics: { | ||
44 | enabled: true | ||
45 | } | ||
46 | } | ||
47 | }) | ||
48 | |||
49 | // Simulate a HTTP request | ||
50 | await server.videos.list() | ||
51 | |||
52 | const res = await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.OK_200 }) | ||
53 | expect(res.text).to.contain('peertube_job_queue_total{') | ||
54 | expect(res.text).to.contain('http_request_duration_ms_bucket{') | ||
55 | }) | ||
56 | |||
57 | it('Should have playback metrics', async function () { | ||
58 | await setAccessTokensToServers([ server ]) | ||
59 | |||
60 | const video = await server.videos.quickUpload({ name: 'video' }) | ||
61 | |||
62 | await server.metrics.addPlaybackMetric({ | ||
63 | metrics: { | ||
64 | playerMode: 'p2p-media-loader', | ||
65 | resolution: VideoResolution.H_1080P, | ||
66 | fps: 30, | ||
67 | resolutionChanges: 1, | ||
68 | errors: 2, | ||
69 | downloadedBytesP2P: 0, | ||
70 | downloadedBytesHTTP: 0, | ||
71 | uploadedBytesP2P: 5, | ||
72 | p2pPeers: 1, | ||
73 | p2pEnabled: false, | ||
74 | videoId: video.uuid | ||
75 | } | ||
76 | }) | ||
77 | |||
78 | const res = await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.OK_200 }) | ||
79 | |||
80 | expect(res.text).to.contain('peertube_playback_http_downloaded_bytes_total{') | ||
81 | expect(res.text).to.contain('peertube_playback_p2p_peers{') | ||
82 | expect(res.text).to.contain('p2pEnabled="false"') | ||
83 | }) | ||
84 | |||
85 | it('Should take the last playback metric', async function () { | ||
86 | await setAccessTokensToServers([ server ]) | ||
87 | |||
88 | const video = await server.videos.quickUpload({ name: 'video' }) | ||
89 | |||
90 | const metrics = { | ||
91 | playerMode: 'p2p-media-loader', | ||
92 | resolution: VideoResolution.H_1080P, | ||
93 | fps: 30, | ||
94 | resolutionChanges: 1, | ||
95 | errors: 2, | ||
96 | downloadedBytesP2P: 0, | ||
97 | downloadedBytesHTTP: 0, | ||
98 | uploadedBytesP2P: 5, | ||
99 | p2pPeers: 7, | ||
100 | p2pEnabled: false, | ||
101 | videoId: video.uuid | ||
102 | } as PlaybackMetricCreate | ||
103 | |||
104 | await server.metrics.addPlaybackMetric({ metrics }) | ||
105 | |||
106 | metrics.p2pPeers = 42 | ||
107 | await server.metrics.addPlaybackMetric({ metrics }) | ||
108 | |||
109 | const res = await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.OK_200 }) | ||
110 | |||
111 | // eslint-disable-next-line max-len | ||
112 | const label = `{videoOrigin="local",playerMode="p2p-media-loader",resolution="1080",fps="30",p2pEnabled="false",videoUUID="${video.uuid}"}` | ||
113 | expect(res.text).to.contain(`peertube_playback_p2p_peers${label} 42`) | ||
114 | expect(res.text).to.not.contain(`peertube_playback_p2p_peers${label} 7`) | ||
115 | }) | ||
116 | |||
117 | it('Should disable http request duration metrics', async function () { | ||
118 | await server.kill() | ||
119 | |||
120 | await server.run({ | ||
121 | open_telemetry: { | ||
122 | metrics: { | ||
123 | enabled: true, | ||
124 | http_request_duration: { | ||
125 | enabled: false | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | }) | ||
130 | |||
131 | // Simulate a HTTP request | ||
132 | await server.videos.list() | ||
133 | |||
134 | const res = await makeRawRequest({ url: metricsUrl, expectedStatus: HttpStatusCode.OK_200 }) | ||
135 | expect(res.text).to.not.contain('http_request_duration_ms_bucket{') | ||
136 | }) | ||
137 | |||
138 | after(async function () { | ||
139 | await server.kill() | ||
140 | }) | ||
141 | }) | ||
142 | |||
143 | describe('Tracing', function () { | ||
144 | let mockHTTP: MockHTTP | ||
145 | let mockPort: number | ||
146 | |||
147 | before(async function () { | ||
148 | mockHTTP = new MockHTTP() | ||
149 | mockPort = await mockHTTP.initialize() | ||
150 | }) | ||
151 | |||
152 | it('Should enable open telemetry tracing', async function () { | ||
153 | server = await createSingleServer(1) | ||
154 | |||
155 | await expectLogDoesNotContain(server, 'Registering Open Telemetry tracing') | ||
156 | |||
157 | await server.kill() | ||
158 | }) | ||
159 | |||
160 | it('Should enable open telemetry metrics', async function () { | ||
161 | await server.run({ | ||
162 | open_telemetry: { | ||
163 | tracing: { | ||
164 | enabled: true, | ||
165 | jaeger_exporter: { | ||
166 | endpoint: 'http://127.0.0.1:' + mockPort | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | }) | ||
171 | |||
172 | await expectLogContain(server, 'Registering Open Telemetry tracing') | ||
173 | }) | ||
174 | |||
175 | it('Should upload a video and correctly works', async function () { | ||
176 | await setAccessTokensToServers([ server ]) | ||
177 | |||
178 | const { uuid } = await server.videos.quickUpload({ name: 'video', privacy: VideoPrivacy.PUBLIC }) | ||
179 | |||
180 | const video = await server.videos.get({ id: uuid }) | ||
181 | |||
182 | expect(video.name).to.equal('video') | ||
183 | }) | ||
184 | |||
185 | after(async function () { | ||
186 | await mockHTTP.terminate() | ||
187 | }) | ||
188 | }) | ||
189 | |||
190 | after(async function () { | ||
191 | await cleanupTests([ server ]) | ||
192 | }) | ||
193 | }) | ||