2022-08-12 10:41:29 -04:00
|
|
|
import express from 'express'
|
2023-07-31 08:34:36 -04:00
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { OpenTelemetryMetrics } from '@server/lib/opentelemetry/metrics.js'
|
|
|
|
import { HttpStatusCode, PlaybackMetricCreate } from '@peertube/peertube-models'
|
|
|
|
import { addPlaybackMetricValidator, apiRateLimiter, asyncMiddleware } from '../../middlewares/index.js'
|
2022-08-12 10:41:29 -04:00
|
|
|
|
|
|
|
const metricsRouter = express.Router()
|
|
|
|
|
2023-06-20 08:17:34 -04:00
|
|
|
metricsRouter.use(apiRateLimiter)
|
|
|
|
|
2022-08-12 10:41:29 -04:00
|
|
|
metricsRouter.post('/playback',
|
|
|
|
asyncMiddleware(addPlaybackMetricValidator),
|
|
|
|
addPlaybackMetric
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
metricsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function addPlaybackMetric (req: express.Request, res: express.Response) {
|
2023-03-14 03:50:44 -04:00
|
|
|
if (!CONFIG.OPEN_TELEMETRY.METRICS.ENABLED) {
|
|
|
|
return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
|
|
|
|
}
|
|
|
|
|
2022-08-12 10:41:29 -04:00
|
|
|
const body: PlaybackMetricCreate = req.body
|
|
|
|
|
|
|
|
OpenTelemetryMetrics.Instance.observePlaybackMetric(res.locals.onlyImmutableVideo, body)
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|