]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/opentelemetry/tracing.ts
252a3b6645bf703224f639a3e58f3d2510d76741
[github/Chocobozzz/PeerTube.git] / server / lib / opentelemetry / tracing.ts
1 import { SequelizeInstrumentation } from 'opentelemetry-instrumentation-sequelize'
2 import { diag, DiagLogLevel, trace } from '@opentelemetry/api'
3 import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
4 import { registerInstrumentations } from '@opentelemetry/instrumentation'
5 import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'
6 import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
7 import FsInstrumentation from '@opentelemetry/instrumentation-fs'
8 import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
9 import { PgInstrumentation } from '@opentelemetry/instrumentation-pg'
10 import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis-4'
11 import { Resource } from '@opentelemetry/resources'
12 import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
13 import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
14 import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
15 import { logger } from '@server/helpers/logger'
16 import { CONFIG } from '@server/initializers/config'
17
18 function 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({
51 tracerProvider,
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 }),
64 new FsInstrumentation(),
65 new SequelizeInstrumentation()
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
78 const tracer = trace.getTracer('peertube')
79
80 export {
81 registerOpentelemetryTracing,
82 tracer
83 }