autotelic/fastify-opentelemetry

Including traceid in response header

Closed this issue · 1 comments

I have this hook I've included in my application that adds the traceid as a header in my response, so that traces can be easily looked up for specific requests for debugging (not for propagation). Do you think this is something that would be within the scope of your plugin to include as an option?

import { isValidTraceId } from '@opentelemetry/api';

// ...

fastify.addHook('onRequest', async (request, reply) => {
  const { traceId } = request.openTelemetry().activeSpan.spanContext();
  if (isValidTraceId(traceId)) reply.header('traceid', traceId);
});
HW13 commented

Hi @10xLaCroixDrinker, this specific implementation would be a little out of scope. We want to stay within the realm of the OpenTelemetry API/spec. An option adding an onSend hook that propagates trace context to the reply headers would be within scope though (and a welcome addition). I realize that your header is primarily for debugging, but the traceid would still exist within a propagation header.

// Assuming this is the default `traceparent` propagation header...
const [
  version,
  traceId,
  parentSpanId,
  traceFlags,
] = headers.get('traceparent').split('-')