opentelemetry-js: Http Spans are not linked / does not set parent span

Hello,

I’m manually implementing tracing within my NodeJs server and I’m running into an issue with the span traces are not linking or setting the parent span.

i.e

let requestHeaders = request.headers["x-request-id"]; // Passes traceId within the request headers to another server that also has tracing implemented

let parentSpan = {
    traceId: requestHeaders,
    spanId: prevSpan?.context().spanId,

  };
  span = tracer.startSpan("testSpan ", { parentSpan});

In Jaeger I know that all spans needs to share the same traceID so I’m trying to start spans with a the traceId that is being passed through services.

  • This only affects the JavaScript OpenTelemetry library
  • This may affect other libraries, but I would like to get opinions here first

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

@djknowles21 hello, any updates on this? I’m having similar troubles

The API how to specify a parent has changed over time. In the past it was possible via an explicit parentSpan property in options but this was removed. Now it works only via context (third argument of startSpan).

See https://github.com/open-telemetry/opentelemetry-js/blob/611a30f19145e7294efdc587efc863410c900604/examples/basic-tracer-node/index.js#L48-L49 for a sample.

I would advise to use a NonRecordingSpan that wraps your span context, there is a method for this in the API: https://github.com/open-telemetry/opentelemetry-js-api/blob/e1745d19b9bdfd1af7d805a6b2c32a5a5dbf4d15/src/trace/context-utils.ts#L63

import { trace, context } from '@opentelemetry/api'

const handler = (request) {
   // you'll need to have a propagation component implemented that pickup the trace id from the headers
   const ctx = propagation.extract(ROOT_CONTEXT, request.headers)
   const spanContext = trace.getSpanContext(ctx)
   return context.with(setSpanContext(context.active(), spanContext), () => {
     // continue whatever you want to do,  new span should get the correct remote span
   })
}