aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/opentelemetry/tracing.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-07-05 15:43:21 +0200
committerChocobozzz <me@florianbigard.com>2022-07-06 15:13:55 +0200
commit630d0a1bf5897fff203cb07e426223f55dcc882d (patch)
tree5e6fa9d26f3f21178a538bd1ac38fa0a3f4f228c /server/lib/opentelemetry/tracing.ts
parent15b43b214eb37b05aa65aa8ef61fd0e6aa0b62d2 (diff)
downloadPeerTube-630d0a1bf5897fff203cb07e426223f55dcc882d.tar.gz
PeerTube-630d0a1bf5897fff203cb07e426223f55dcc882d.tar.zst
PeerTube-630d0a1bf5897fff203cb07e426223f55dcc882d.zip
Introduce experimental telemetry
Diffstat (limited to 'server/lib/opentelemetry/tracing.ts')
-rw-r--r--server/lib/opentelemetry/tracing.ts81
1 files changed, 81 insertions, 0 deletions
diff --git a/server/lib/opentelemetry/tracing.ts b/server/lib/opentelemetry/tracing.ts
new file mode 100644
index 000000000..5358d04de
--- /dev/null
+++ b/server/lib/opentelemetry/tracing.ts
@@ -0,0 +1,81 @@
1import { diag, DiagLogLevel, trace } from '@opentelemetry/api'
2import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
3import { registerInstrumentations } from '@opentelemetry/instrumentation'
4import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'
5import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
6import FsInstrumentation from '@opentelemetry/instrumentation-fs'
7import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
8import { PgInstrumentation } from '@opentelemetry/instrumentation-pg'
9import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis-4'
10import { Resource } from '@opentelemetry/resources'
11import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
12import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
13import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
14import { logger } from '@server/helpers/logger'
15import { CONFIG } from '@server/initializers/config'
16
17function registerOpentelemetryTracing () {
18 if (CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true) return
19
20 logger.info('Registering Open Telemetry tracing')
21
22 const customLogger = (level: string) => {
23 return (message: string, ...args: unknown[]) => {
24 let fullMessage = message
25
26 for (const arg of args) {
27 if (typeof arg === 'string') fullMessage += arg
28 else break
29 }
30
31 logger[level](fullMessage)
32 }
33 }
34
35 diag.setLogger({
36 error: customLogger('error'),
37 warn: customLogger('warn'),
38 info: customLogger('info'),
39 debug: customLogger('debug'),
40 verbose: customLogger('verbose')
41 }, DiagLogLevel.INFO)
42
43 const tracerProvider = new NodeTracerProvider({
44 resource: new Resource({
45 [SemanticResourceAttributes.SERVICE_NAME]: 'peertube'
46 })
47 })
48
49 registerInstrumentations({
50 tracerProvider: tracerProvider,
51 instrumentations: [
52 new PgInstrumentation({
53 enhancedDatabaseReporting: true
54 }),
55 new DnsInstrumentation(),
56 new HttpInstrumentation(),
57 new ExpressInstrumentation(),
58 new RedisInstrumentation({
59 dbStatementSerializer: function (cmdName, cmdArgs) {
60 return [ cmdName, ...cmdArgs ].join(' ')
61 }
62 }),
63 new FsInstrumentation()
64 ]
65 })
66
67 tracerProvider.addSpanProcessor(
68 new BatchSpanProcessor(
69 new JaegerExporter({ endpoint: CONFIG.OPEN_TELEMETRY.TRACING.JAEGER_EXPORTER.ENDPOINT })
70 )
71 )
72
73 tracerProvider.register()
74}
75
76const tracer = trace.getTracer('peertube')
77
78export {
79 registerOpentelemetryTracing,
80 tracer
81}