autotelic / fastify-opentelemetry

A Fastify plugin that utilizes the OpenTelemetry API to provide request tracing.
MIT License
76 stars 12 forks source link

Including traceid in response header #66

Closed 10xLaCroixDrinker closed 6 months ago

10xLaCroixDrinker commented 7 months ago

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 7 months ago

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('-')