Skip to content

Commit

Permalink
Merge pull request #41 from biothings/fix-857-bte
Browse files Browse the repository at this point in the history
Update OTel for better tracing
  • Loading branch information
tokebe authored Oct 2, 2024
2 parents 5790cb4 + 555e7ee commit c1146be
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@opentelemetry/sdk-metrics": "^1.18.1",
"@opentelemetry/sdk-node": "^0.50.0",
"@opentelemetry/sdk-trace-node": "^1.18.1",
"@opentelemetry/semantic-conventions": "^1.27.0",
"@sentry/node": "^7.74.1",
"@sentry/profiling-node": "^1.2.1",
"axios": "^0.27.2",
Expand Down
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
12 changes: 7 additions & 5 deletions src/controllers/opentelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ 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";
import { ATTR_SERVICE_NAME } from '@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"),
// metrics, if needed, shall be exported on a different endpoint
traceExporter: new OTLPTraceExporter({
url: `${process.env.JAEGER_HOST ?? 'jaeger-otel-collector'}:${process.env.JAEGER_PORT ?? 4318}/v1/traces`
}),
instrumentations: [getNodeAutoInstrumentations()],
resource: new Resource({
"service.name": "biothings-explorer",
[ATTR_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 c1146be

Please sign in to comment.