opentelemetry-js icon indicating copy to clipboard operation
opentelemetry-js copied to clipboard

Workaround needed: Setting SPAN_KEY on some parent span from a child span

Open NPellet opened this issue 2 years ago • 1 comments
trafficstars

Is your feature request related to a problem? Please describe.

Let us assume I create a HTTP server (Express or Fastify, doesn't matter), with a bunch of middlewares.

There is a root span, when the HTTP instrument picks up the request, in context ROOT_CONTEXT (let's say no propagation header was found). Then there's a bunch of nested spans in between, due to all the cool middlewares and the web server Now we enter the handler of the route. At this stage, let's say it's a special route, where the context is passed a bit differently, for example in the payload of the HTTP request. Not something the HTTP instrumentation could have know about. It's up to that route to know how to extract the context. There's currently no way to change the root span, which is created on ROOT_CONTEXT, to assign it the newly discovered context.

Describe the solution you'd like

A method to reach the first span whose isRemote flag is false Allow to mutate / change its context

Alternatively, I was wondering if the principle of immutability of a context really applies for its spanContext. For example, the baggage API allows to mutate a context by adding data to it (as far as I understand). So should the context really be immutable with regards to its spanContext ?

Describe alternatives you've considered

I'm guessing this is absolutely a no-go, probably due to the immutability of the span's context.

I'm also guessing I might be referred ton span links, which, true, is a way to solve the problem, but doesn't convey the whole picture (as the complete HTTP route was triggered by the producer, not just my route handler)

So I don't know, anyone has an idea maybe ?

Additional context

Using the following code


    const ctx = opentelemetry.propagation.extract(
      opentelemetry.context.active(),
      data.detail,
      contextGetter
    );

    await opentelemetry.context.with(ctx, async () => {
      console.log('Event data recieved:', data);
      // To something here with the span
    });

Works by "splitting" my tracing in two:

image

image

At least I get propagation, but ideally I'd like to see my fist trace embedded in my second trace.

Am I making any sense ?

NPellet avatar Nov 30 '22 11:11 NPellet

I'm not sure if I got your question clearly. Are you going to modify the root span with information retrieved after the span has been created?

legendecas avatar Dec 06 '22 09:12 legendecas

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Feb 06 '23 06:02 github-actions[bot]

This issue was closed because it has been stale for 14 days with no activity.

github-actions[bot] avatar Feb 20 '23 06:02 github-actions[bot]