Skip to content

Commit

Permalink
fix: refactor OTel tracing to group POST and EXEC spans in query and …
Browse files Browse the repository at this point in the history
…asyncquery
  • Loading branch information
NeuralFlux committed Sep 12, 2024
1 parent 2b0f57b commit 49c24ad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
9 changes: 8 additions & 1 deletion src/controllers/async/asyncquery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { AxiosError, AxiosResponse } from "axios";
import { context, propagation } from "@opentelemetry/api";
import { customAlphabet } from "nanoid";
import * as utils from "../../utils/common";
import { redisClient } from "@biothings-explorer/utils";
Expand Down Expand Up @@ -42,8 +43,14 @@ export async function asyncquery(
}
const url = `${req.protocol}://${req.header("host")}/v1/asyncquery_status/${jobId}`;

// add OTel trace context
const otelData: Partial<{ traceparent: string; tracestate: string }> = {};
propagation.inject(context.active(), otelData);
const { traceparent, tracestate } = otelData;
queueData = { ...queueData, traceparent, tracestate };

const job = await queryQueue.add(
{ ...queueData, url: url.replace("status", "response") },
{ ...queueData, traceparent: traceparent, tracestate: tracestate, url: url.replace("status", "response") },
{
jobId: jobId,
timeout: parseInt(process.env.JOB_TIMEOUT ?? (1000 * 60 * 5).toString()),
Expand Down
11 changes: 5 additions & 6 deletions src/controllers/opentelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { NodeSDK } from "@opentelemetry/sdk-node";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { Resource } from "@opentelemetry/resources";
import Debug from "debug";
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
const debug = Debug("bte:biothings-explorer:otel-init");
import { JaegerExporter } from "@opentelemetry/exporter-jaeger";
const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-conventions');

debug("Initializing Opentelemetry instrumentation...");
const sdk = new NodeSDK({
traceExporter: new JaegerExporter({
host: process.env.JAEGER_HOST ?? "jaeger-otel-agent.sri",
port: parseInt(process.env.JAEGER_PORT ?? "6832"),
}),
// uses OTEL_EXPORTER_OTLP_ENDPOINT env var
traceExporter: new OTLPTraceExporter(),
instrumentations: [getNodeAutoInstrumentations()],
resource: new Resource({
"service.name": "biothings-explorer",
[SEMRESATTRS_SERVICE_NAME]: "biothings-explorer",
}),
});
sdk.start();
Expand Down
17 changes: 9 additions & 8 deletions src/controllers/threading/taskHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { tasks } from "../../routes/index";
import { getQueryQueue } from "../async/asyncquery_queue";
import * as Sentry from "@sentry/node";
import { ProfilingIntegration } from "@sentry/profiling-node";
import OpenTelemetry, { Span } from "@opentelemetry/api";
import { Span, trace, context, propagation, Context } from "@opentelemetry/api";
import { Telemetry } from "@biothings-explorer/utils";
import { InnerTaskData } from "@biothings-explorer/types";

Expand Down Expand Up @@ -90,13 +90,14 @@ async function runTask({
scope.setSpan(transaction);
});

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

span.setAttribute("bte.requestData", JSON.stringify(req.data.queryGraph));
Telemetry.setOtelSpan(span);
} catch (error) {
Expand Down
14 changes: 9 additions & 5 deletions src/controllers/threading/threadHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,8 @@ async function queueTaskToWorkers(pool: Piscina, taskInfo: TaskInfo, route: stri
const abortController = new AbortController();
const { port1: toWorker, port2: fromWorker } = new MessageChannel();

// get otel context

const otelData: Partial<{ traceparent: string; tracestate: string }> = {};
propagation.inject(context.active(), otelData);
const { traceparent, tracestate } = otelData;
const traceparent: string = taskInfo.data.traceparent;
const tracestate: string = taskInfo.data.tracestate;

const taskData: InnerTaskData = { req: taskInfo, route, traceparent, tracestate, port: toWorker };
taskData.req.data.options = {...taskData.req.data.options, metakg: global.metakg?.ops, smartapi: global.smartapi} as QueryHandlerOptions;
Expand Down Expand Up @@ -219,6 +216,11 @@ async function queueTaskToWorkers(pool: Piscina, taskInfo: TaskInfo, route: stri

export async function runTask(req: Request, res: Response, route: string, useBullSync = true): Promise<TrapiResponse> {
const queryQueue: Queue = global.queryQueue.bte_sync_query_queue;

const otelData: Partial<{ traceparent: string; tracestate: string }> = {};
propagation.inject(context.active(), otelData);
const { traceparent, tracestate } = otelData;

const taskInfo: TaskInfo = {
data: {
route,
Expand All @@ -233,6 +235,8 @@ export async function runTask(req: Request, res: Response, route: string, useBul
},
params: req.params,
endpoint: req.originalUrl,
traceparent: traceparent,
tracestate: tracestate,
},
};

Expand Down

0 comments on commit 49c24ad

Please sign in to comment.