diff options
author | Chocobozzz <me@florianbigard.com> | 2022-07-05 15:43:21 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-07-06 15:13:55 +0200 |
commit | 630d0a1bf5897fff203cb07e426223f55dcc882d (patch) | |
tree | 5e6fa9d26f3f21178a538bd1ac38fa0a3f4f228c /server/lib/opentelemetry/tracing.ts | |
parent | 15b43b214eb37b05aa65aa8ef61fd0e6aa0b62d2 (diff) | |
download | PeerTube-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.ts | 81 |
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 @@ | |||
1 | import { diag, DiagLogLevel, trace } from '@opentelemetry/api' | ||
2 | import { JaegerExporter } from '@opentelemetry/exporter-jaeger' | ||
3 | import { registerInstrumentations } from '@opentelemetry/instrumentation' | ||
4 | import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns' | ||
5 | import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express' | ||
6 | import FsInstrumentation from '@opentelemetry/instrumentation-fs' | ||
7 | import { HttpInstrumentation } from '@opentelemetry/instrumentation-http' | ||
8 | import { PgInstrumentation } from '@opentelemetry/instrumentation-pg' | ||
9 | import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis-4' | ||
10 | import { Resource } from '@opentelemetry/resources' | ||
11 | import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base' | ||
12 | import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node' | ||
13 | import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions' | ||
14 | import { logger } from '@server/helpers/logger' | ||
15 | import { CONFIG } from '@server/initializers/config' | ||
16 | |||
17 | function 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 | |||
76 | const tracer = trace.getTracer('peertube') | ||
77 | |||
78 | export { | ||
79 | registerOpentelemetryTracing, | ||
80 | tracer | ||
81 | } | ||