Skip to content

Commit

Permalink
Set up child span between threads
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Jan 20, 2024
1 parent f0873e9 commit 06c5437
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/controllers/threading/taskHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ try {
debug(error);
}

const runTask = async ({ req, route, port, job: { jobId, queueName } = {} }) => {
const runTask = async ({ req, route, traceparent, tracestate, port, job: { jobId, queueName } = {} }) => {
debug(`Worker thread ${threadId} beginning ${workerData.queue} task.`);

global.SCHEMA_VERSION = "1.4.0";
Expand Down Expand Up @@ -81,7 +81,7 @@ const runTask = async ({ req, route, port, job: { jobId, queueName } = {} }) =>
scope.setSpan(transaction);
});

span = opentelemetry.trace.getTracer('biothings-explorer-thread').startSpan(routeNames[route])
span = opentelemetry.trace.getTracer('biothings-explorer-thread').startSpan(routeNames[route], undefined, opentelemetry.propagation.extract(opentelemetry.context.active(), {traceparent, tracestate}))
span.setAttribute("bte.requestData", JSON.stringify(req.data.queryGraph));
Telemetry.setOtelSpan(span);
} catch (error) {
Expand Down
10 changes: 9 additions & 1 deletion src/controllers/threading/threadHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { MessageChannel, threadId } = require("worker_threads");
const { context, propagation, trace } = require("@opentelemetry/api");
const debug = require("debug")("bte:biothings-explorer-trapi:threading");
const path = require("path");
// const taskHandler = require("./taskHandler");
Expand Down Expand Up @@ -97,7 +98,14 @@ const queueTaskToWorkers = async (pool, req, route, job) => {
let WorkerThreadID;
const abortController = new AbortController();
const { port1: toWorker, port2: fromWorker } = new MessageChannel();
const taskData = { req, route, port: toWorker };

// get otel context
const otelData = {};
propagation.inject(context.active(), otelData);
const { traceparent, tracestate } = otelData;


const taskData = { req, route, traceparent, tracestate, port: toWorker };
if (job) taskData.job = { jobId: job.id, queueName: job.queue.name };
const task = pool.run(taskData, { signal: abortController.signal, transferList: [toWorker] });
if (job) {
Expand Down

0 comments on commit 06c5437

Please sign in to comment.