From fd3c2e87051f5029cdec39d877b576a62f48e219 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 12 Aug 2022 16:41:29 +0200 Subject: Add playback metric endpoint sent to OTEL --- server/middlewares/validators/index.ts | 1 + server/middlewares/validators/metrics.ts | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 server/middlewares/validators/metrics.ts (limited to 'server/middlewares') 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' export * from './feeds' export * from './follows' export * from './jobs' +export * from './metrics' export * from './logs' export * from './oembed' 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 @@ +import express from 'express' +import { body } from 'express-validator' +import { isValidPlayerMode } from '@server/helpers/custom-validators/metrics' +import { isIdOrUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc' +import { CONFIG } from '@server/initializers/config' +import { HttpStatusCode, PlaybackMetricCreate } from '@shared/models' +import { logger } from '../../helpers/logger' +import { areValidationErrors, doesVideoExist } from './shared' + +const addPlaybackMetricValidator = [ + body('resolution') + .isInt({ min: 0 }).withMessage('Invalid resolution'), + body('fps') + .optional() + .isInt({ min: 0 }).withMessage('Invalid fps'), + body('playerMode') + .custom(isValidPlayerMode).withMessage('Invalid playerMode'), + + body('resolutionChanges') + .isInt({ min: 0 }).withMessage('Invalid resolutionChanges'), + + body('errors') + .isInt({ min: 0 }).withMessage('Invalid errors'), + + body('downloadedBytesP2P') + .isInt({ min: 0 }).withMessage('Invalid downloadedBytesP2P'), + body('downloadedBytesHTTP') + .isInt({ min: 0 }).withMessage('Invalid downloadedBytesHTTP'), + + body('uploadedBytesP2P') + .isInt({ min: 0 }).withMessage('Invalid uploadedBytesP2P'), + + body('videoId') + .customSanitizer(toCompleteUUID) + .optional() + .custom(isIdOrUUIDValid), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking addPlaybackMetricValidator parameters.', { parameters: req.query }) + + if (!CONFIG.OPEN_TELEMETRY.METRICS.ENABLED) return res.sendStatus(HttpStatusCode.NO_CONTENT_204) + + const body: PlaybackMetricCreate = req.body + + if (areValidationErrors(req, res)) return + if (!await doesVideoExist(body.videoId, res, 'only-immutable-attributes')) return + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + addPlaybackMetricValidator +} -- cgit v1.2.3