]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/metrics.ts
Add playback metric endpoint sent to OTEL
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / metrics.ts
CommitLineData
fd3c2e87
C
1import express from 'express'
2import { body } from 'express-validator'
3import { isValidPlayerMode } from '@server/helpers/custom-validators/metrics'
4import { isIdOrUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc'
5import { CONFIG } from '@server/initializers/config'
6import { HttpStatusCode, PlaybackMetricCreate } from '@shared/models'
7import { logger } from '../../helpers/logger'
8import { areValidationErrors, doesVideoExist } from './shared'
9
10const 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
54export {
55 addPlaybackMetricValidator
56}