diff options
author | Chocobozzz <me@florianbigard.com> | 2022-08-12 16:41:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-08-16 10:33:27 +0200 |
commit | fd3c2e87051f5029cdec39d877b576a62f48e219 (patch) | |
tree | a3c657f178702a3363af680ed8ffb7cd038243b8 /server/middlewares | |
parent | 0e6cd1c00f71554fe7375a96db693a6983951ba6 (diff) | |
download | PeerTube-fd3c2e87051f5029cdec39d877b576a62f48e219.tar.gz PeerTube-fd3c2e87051f5029cdec39d877b576a62f48e219.tar.zst PeerTube-fd3c2e87051f5029cdec39d877b576a62f48e219.zip |
Add playback metric endpoint sent to OTEL
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/validators/index.ts | 1 | ||||
-rw-r--r-- | server/middlewares/validators/metrics.ts | 56 |
2 files changed, 57 insertions, 0 deletions
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index b0ad04819..ffadb3b49 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts | |||
@@ -10,6 +10,7 @@ export * from './express' | |||
10 | export * from './feeds' | 10 | export * from './feeds' |
11 | export * from './follows' | 11 | export * from './follows' |
12 | export * from './jobs' | 12 | export * from './jobs' |
13 | export * from './metrics' | ||
13 | export * from './logs' | 14 | export * from './logs' |
14 | export * from './oembed' | 15 | export * from './oembed' |
15 | export * from './pagination' | 16 | export * from './pagination' |
diff --git a/server/middlewares/validators/metrics.ts b/server/middlewares/validators/metrics.ts new file mode 100644 index 000000000..b1dbec603 --- /dev/null +++ b/server/middlewares/validators/metrics.ts | |||
@@ -0,0 +1,56 @@ | |||
1 | import express from 'express' | ||
2 | import { body } from 'express-validator' | ||
3 | import { isValidPlayerMode } from '@server/helpers/custom-validators/metrics' | ||
4 | import { isIdOrUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc' | ||
5 | import { CONFIG } from '@server/initializers/config' | ||
6 | import { HttpStatusCode, PlaybackMetricCreate } from '@shared/models' | ||
7 | import { logger } from '../../helpers/logger' | ||
8 | import { areValidationErrors, doesVideoExist } from './shared' | ||
9 | |||
10 | const addPlaybackMetricValidator = [ | ||
11 | body('resolution') | ||
12 | .isInt({ min: 0 }).withMessage('Invalid resolution'), | ||
13 | body('fps') | ||
14 | .optional() | ||
15 | .isInt({ min: 0 }).withMessage('Invalid fps'), | ||
16 | body('playerMode') | ||
17 | .custom(isValidPlayerMode).withMessage('Invalid playerMode'), | ||
18 | |||
19 | body('resolutionChanges') | ||
20 | .isInt({ min: 0 }).withMessage('Invalid resolutionChanges'), | ||
21 | |||
22 | body('errors') | ||
23 | .isInt({ min: 0 }).withMessage('Invalid errors'), | ||
24 | |||
25 | body('downloadedBytesP2P') | ||
26 | .isInt({ min: 0 }).withMessage('Invalid downloadedBytesP2P'), | ||
27 | body('downloadedBytesHTTP') | ||
28 | .isInt({ min: 0 }).withMessage('Invalid downloadedBytesHTTP'), | ||
29 | |||
30 | body('uploadedBytesP2P') | ||
31 | .isInt({ min: 0 }).withMessage('Invalid uploadedBytesP2P'), | ||
32 | |||
33 | body('videoId') | ||
34 | .customSanitizer(toCompleteUUID) | ||
35 | .optional() | ||
36 | .custom(isIdOrUUIDValid), | ||
37 | |||
38 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
39 | logger.debug('Checking addPlaybackMetricValidator parameters.', { parameters: req.query }) | ||
40 | |||
41 | if (!CONFIG.OPEN_TELEMETRY.METRICS.ENABLED) return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
42 | |||
43 | const body: PlaybackMetricCreate = req.body | ||
44 | |||
45 | if (areValidationErrors(req, res)) return | ||
46 | if (!await doesVideoExist(body.videoId, res, 'only-immutable-attributes')) return | ||
47 | |||
48 | return next() | ||
49 | } | ||
50 | ] | ||
51 | |||
52 | // --------------------------------------------------------------------------- | ||
53 | |||
54 | export { | ||
55 | addPlaybackMetricValidator | ||
56 | } | ||