]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/opentelemetry/tracing.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / opentelemetry / tracing.ts
index 23764e9e49c01788e769e034e860579d395a6ca0..9a81680b2b9987af0ab8de7fe8eb16be095e7d24 100644 (file)
@@ -1,12 +1,13 @@
-import { diag, DiagLogLevel, trace } from '@opentelemetry/api'
+import { SequelizeInstrumentation } from 'opentelemetry-instrumentation-sequelize'
+import { context, diag, DiagLogLevel, trace } from '@opentelemetry/api'
 import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
 import { registerInstrumentations } from '@opentelemetry/instrumentation'
 import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'
 import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
 import FsInstrumentation from '@opentelemetry/instrumentation-fs'
 import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
+import { IORedisInstrumentation } from '@opentelemetry/instrumentation-ioredis'
 import { PgInstrumentation } from '@opentelemetry/instrumentation-pg'
-import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis-4'
 import { Resource } from '@opentelemetry/resources'
 import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
 import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
@@ -14,6 +15,8 @@ import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
 import { logger } from '@server/helpers/logger'
 import { CONFIG } from '@server/initializers/config'
 
+const tracer = trace.getTracer('peertube')
+
 function registerOpentelemetryTracing () {
   if (CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true) return
 
@@ -55,12 +58,13 @@ function registerOpentelemetryTracing () {
       new DnsInstrumentation(),
       new HttpInstrumentation(),
       new ExpressInstrumentation(),
-      new RedisInstrumentation({
+      new IORedisInstrumentation({
         dbStatementSerializer: function (cmdName, cmdArgs) {
           return [ cmdName, ...cmdArgs ].join(' ')
         }
       }),
-      new FsInstrumentation()
+      new FsInstrumentation(),
+      new SequelizeInstrumentation()
     ]
   })
 
@@ -73,9 +77,18 @@ function registerOpentelemetryTracing () {
   tracerProvider.register()
 }
 
-const tracer = trace.getTracer('peertube')
+async function wrapWithSpanAndContext <T> (spanName: string, cb: () => Promise<T>) {
+  const span = tracer.startSpan(spanName)
+  const activeContext = trace.setSpan(context.active(), span)
+
+  const result = await context.with(activeContext, () => cb())
+  span.end()
+
+  return result
+}
 
 export {
   registerOpentelemetryTracing,
-  tracer
+  tracer,
+  wrapWithSpanAndContext
 }