From f05cb73d7ab916fc4cb6be148cbbc958e3589f02 Mon Sep 17 00:00:00 2001 From: NeuralFlux <40491005+NeuralFlux@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:07:20 -0400 Subject: [PATCH] fix: add express instrumentation for async traces to show up --- package.json | 1 + src/controllers/opentelemetry.ts | 3 ++- src/controllers/threading/threadHandler.ts | 5 ----- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 2a7c03e..3d50019 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "@opentelemetry/exporter-jaeger": "^1.19.0", "@opentelemetry/exporter-metrics-otlp-proto": "^0.50.0", "@opentelemetry/exporter-trace-otlp-proto": "^0.50.0", + "@opentelemetry/instrumentation-express": "^0.43.0", "@opentelemetry/instrumentation-http": "^0.53.0", "@opentelemetry/resources": "^1.18.1", "@opentelemetry/sdk-metrics": "^1.18.1", diff --git a/src/controllers/opentelemetry.ts b/src/controllers/opentelemetry.ts index 77b81e3..557a5c0 100644 --- a/src/controllers/opentelemetry.ts +++ b/src/controllers/opentelemetry.ts @@ -1,6 +1,7 @@ import { NodeSDK } from "@opentelemetry/sdk-node"; import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-node"; import { HttpInstrumentation } from "@opentelemetry/instrumentation-http"; +import { ExpressInstrumentation } from "@opentelemetry/instrumentation-express"; import { Resource } from "@opentelemetry/resources"; import Debug from "debug"; import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; @@ -20,7 +21,7 @@ debug("Initializing Opentelemetry instrumentation..."); const sdk = new NodeSDK({ // metrics, if needed, shall be exported on a different endpoint // trace a subset of instrumentations to avoid performance overhead - instrumentations: [new HttpInstrumentation()], + instrumentations: [new HttpInstrumentation(), new ExpressInstrumentation()], resource: new Resource({ [ATTR_SERVICE_NAME]: "biothings-explorer", }), diff --git a/src/controllers/threading/threadHandler.ts b/src/controllers/threading/threadHandler.ts index c23e69c..6a6916a 100644 --- a/src/controllers/threading/threadHandler.ts +++ b/src/controllers/threading/threadHandler.ts @@ -221,11 +221,6 @@ export async function runTask(req: Request, res: Response, route: string, useBul propagation.inject(context.active(), otelData); const { traceparent, tracestate } = otelData; - // add req dest to root span name as HTTP instrumentation doesn't do it automatically - const activeContext: Context = context.active(); - const rootSpan: Span = trace.getSpan(activeContext); - if (rootSpan != undefined) rootSpan.updateName(`${req.method} ${req.originalUrl}`); - const taskInfo: TaskInfo = { data: { route,