]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/opentelemetry/tracing.ts
Fix lint on hls.ts
[github/Chocobozzz/PeerTube.git] / server / lib / opentelemetry / tracing.ts
CommitLineData
ce6b3765 1import { SequelizeInstrumentation } from 'opentelemetry-instrumentation-sequelize'
630d0a1b
C
2import { diag, DiagLogLevel, trace } from '@opentelemetry/api'
3import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
4import { registerInstrumentations } from '@opentelemetry/instrumentation'
5import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'
6import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
7import FsInstrumentation from '@opentelemetry/instrumentation-fs'
8import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
9import { PgInstrumentation } from '@opentelemetry/instrumentation-pg'
10import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis-4'
11import { Resource } from '@opentelemetry/resources'
12import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
13import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
14import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
15import { logger } from '@server/helpers/logger'
16import { CONFIG } from '@server/initializers/config'
17
18function registerOpentelemetryTracing () {
19 if (CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true) return
20
21 logger.info('Registering Open Telemetry tracing')
22
23 const customLogger = (level: string) => {
24 return (message: string, ...args: unknown[]) => {
25 let fullMessage = message
26
27 for (const arg of args) {
28 if (typeof arg === 'string') fullMessage += arg
29 else break
30 }
31
32 logger[level](fullMessage)
33 }
34 }
35
36 diag.setLogger({
37 error: customLogger('error'),
38 warn: customLogger('warn'),
39 info: customLogger('info'),
40 debug: customLogger('debug'),
41 verbose: customLogger('verbose')
42 }, DiagLogLevel.INFO)
43
44 const tracerProvider = new NodeTracerProvider({
45 resource: new Resource({
46 [SemanticResourceAttributes.SERVICE_NAME]: 'peertube'
47 })
48 })
49
50 registerInstrumentations({
ba2684ce 51 tracerProvider,
630d0a1b
C
52 instrumentations: [
53 new PgInstrumentation({
54 enhancedDatabaseReporting: true
55 }),
56 new DnsInstrumentation(),
57 new HttpInstrumentation(),
58 new ExpressInstrumentation(),
59 new RedisInstrumentation({
60 dbStatementSerializer: function (cmdName, cmdArgs) {
61 return [ cmdName, ...cmdArgs ].join(' ')
62 }
63 }),
ce6b3765
C
64 new FsInstrumentation(),
65 new SequelizeInstrumentation()
630d0a1b
C
66 ]
67 })
68
69 tracerProvider.addSpanProcessor(
70 new BatchSpanProcessor(
71 new JaegerExporter({ endpoint: CONFIG.OPEN_TELEMETRY.TRACING.JAEGER_EXPORTER.ENDPOINT })
72 )
73 )
74
75 tracerProvider.register()
76}
77
78const tracer = trace.getTracer('peertube')
79
80export {
81 registerOpentelemetryTracing,
82 tracer
83}