aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/metrics.ts
blob: 909963fa7a6af118ea177f50da112dd28bee8301 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import express from 'express'
import { CONFIG } from '@server/initializers/config'
import { OpenTelemetryMetrics } from '@server/lib/opentelemetry/metrics'
import { HttpStatusCode, PlaybackMetricCreate } from '@shared/models'
import { addPlaybackMetricValidator, apiRateLimiter, asyncMiddleware } from '../../middlewares'

const metricsRouter = express.Router()

metricsRouter.use(apiRateLimiter)

metricsRouter.post('/playback',
  asyncMiddleware(addPlaybackMetricValidator),
  addPlaybackMetric
)

// ---------------------------------------------------------------------------

export {
  metricsRouter
}

// ---------------------------------------------------------------------------

function addPlaybackMetric (req: express.Request, res: express.Response) {
  if (!CONFIG.OPEN_TELEMETRY.METRICS.ENABLED) {
    return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
  }

  const body: PlaybackMetricCreate = req.body

  OpenTelemetryMetrics.Instance.observePlaybackMetric(res.locals.onlyImmutableVideo, body)

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}