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'
18 function registerOpentelemetryTracing () {
19 if (CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true) return
21 logger.info('Registering Open Telemetry tracing')
23 const customLogger = (level: string) => {
24 return (message: string, ...args: unknown[]) => {
25 let fullMessage = message
27 for (const arg of args) {
28 if (typeof arg === 'string') fullMessage += arg
32 logger[level](fullMessage)
37 error: customLogger('error'),
38 warn: customLogger('warn'),
39 info: customLogger('info'),
40 debug: customLogger('debug'),
41 verbose: customLogger('verbose')
44 const tracerProvider = new NodeTracerProvider({
45 resource: new Resource({
46 [SemanticResourceAttributes.SERVICE_NAME]: 'peertube'
50 registerInstrumentations({
53 new PgInstrumentation({
54 enhancedDatabaseReporting: true
56 new DnsInstrumentation(),
57 new HttpInstrumentation(),
58 new ExpressInstrumentation(),
59 new RedisInstrumentation({
60 dbStatementSerializer: function (cmdName, cmdArgs) {
61 return [ cmdName, ...cmdArgs ].join(' ')
64 new FsInstrumentation(),
65 new SequelizeInstrumentation()
69 tracerProvider.addSpanProcessor(
70 new BatchSpanProcessor(
71 new JaegerExporter({ endpoint: CONFIG.OPEN_TELEMETRY.TRACING.JAEGER_EXPORTER.ENDPOINT })
75 tracerProvider.register()
78 const tracer = trace.getTracer('peertube')
81 registerOpentelemetryTracing,