Skip to content

Commit

Permalink
fix: make otel tracing leaner and synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
NeuralFlux committed Oct 10, 2024
1 parent 979c8fb commit b8c3648
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@
"@bull-board/api": "^5.9.1",
"@bull-board/express": "^5.9.1",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/auto-instrumentations-node": "^0.44.0",
"@opentelemetry/exporter-jaeger": "^1.19.0",
"@opentelemetry/exporter-metrics-otlp-proto": "^0.50.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.50.0",
"@opentelemetry/instrumentation-http": "^0.53.0",
"@opentelemetry/resources": "^1.18.1",
"@opentelemetry/sdk-metrics": "^1.18.1",
"@opentelemetry/sdk-node": "^0.50.0",
Expand Down
14 changes: 9 additions & 5 deletions src/controllers/opentelemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NodeSDK } from "@opentelemetry/sdk-node";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { Resource } from "@opentelemetry/resources";
import Debug from "debug";
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
Expand All @@ -9,17 +10,20 @@ import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
const jaegerHost = process.env.JAEGER_HOST ?? 'jaeger-otel-collector.sri';
const jaegerPort = process.env.JAEGER_PORT ?? 4318;
const jaegerResName = process.env.JAEGER_RES_NAME ?? '/v1/traces';
const traceExporter = new OTLPTraceExporter({
url: `http://${jaegerHost}:${jaegerPort}${jaegerResName}`
});

debug("Initializing Opentelemetry instrumentation...");
const sdk = new NodeSDK({
// metrics, if needed, shall be exported on a different endpoint
traceExporter: new OTLPTraceExporter({
url: `http://${jaegerHost}:${jaegerPort}${jaegerResName}`
}),
instrumentations: [getNodeAutoInstrumentations()],
// trace a subset of instrumentations to avoid performance overhead
instrumentations: [new HttpInstrumentation()],
resource: new Resource({
[ATTR_SERVICE_NAME]: "biothings-explorer",
}),
// use simple span processor to avoid losing data when the forked process exits (taskHandler)
spanProcessors: [new SimpleSpanProcessor(traceExporter)],
});
debug(`OTel URL http://${jaegerHost}:${jaegerPort}${jaegerResName}`);
sdk.start();
Expand Down
3 changes: 1 addition & 2 deletions src/controllers/threading/taskHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ async function runTask({
});

let activeContext: Context = propagation.extract(context.active(), { traceparent, tracestate });
debug(`OTel task ${traceparent} and ${tracestate}`);
debug(`OTel task context: ${traceparent} and ${tracestate}`);
let tracer = trace.getTracer("biothings-explorer-thread")
span = tracer.startSpan(
routeNames[route],
{kind: 1}, // specifies internal span
activeContext,
);
debug(`OTel span created ${span}`);

span.setAttribute("bte.requestData", JSON.stringify(req.data.queryGraph));
Telemetry.setOtelSpan(span);
Expand Down
7 changes: 6 additions & 1 deletion src/controllers/threading/threadHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MessageChannel, threadId } from "worker_threads";
import Debug from "debug";
import { context, propagation } from "@opentelemetry/api";
import { context, propagation, trace } from "@opentelemetry/api";
const debug = Debug("bte:biothings-explorer-trapi:threading");
import path from "path";
import { redisClient } from "@biothings-explorer/utils";
Expand Down Expand Up @@ -221,6 +221,11 @@ 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.active();
const rootSpan = trace.getSpan(activeContext);
rootSpan.updateName(`${req.method} ${req.originalUrl}`);

const taskInfo: TaskInfo = {
data: {
route,
Expand Down

0 comments on commit b8c3648

Please sign in to comment.