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

Need a way to get the root span's duration

Open AadharshR opened this issue 3 years ago • 4 comments

In opentelemetry js, we can get the spanContext and the corresponding trace Id. But with the given trace Id, we are not able to perform operations like getting the duration from the root span to current span.

If we get the duration present in currentSpan, it only prints the current span's duration and not from the root span.

Can anyone please help me on this?

AadharshR avatar Aug 04 '22 15:08 AadharshR

What do you mean "duration from root span to current span"? Are you trying to figure out the duration between the root span start and the current span start? You are correct that is not possible to get timing information using API methods. Can you explain what use case you are trying to solve here?

dyladan avatar Aug 04 '22 15:08 dyladan

Yes, I am trying to figure out the time between start and current span start. I have a use case where there is a need to calculate the time taken by all the spans from the start ( when the application gets a request.) to the current span inside, lets say a method that does some operation.

Edit: Anything that can be like this would be very helpful:

const span = trace.getActiveSpan().getSpanContext() // This is just abstract which gets context and gets the span. const leafDuration = span.duration; const traceId = span.traceId; const rootSpan = trace.getSpanById(traceId); // This could be a great addition to have. const duration = rootSpan.getSpanContext().duration; const diff = duration - leafDuration

AadharshR avatar Aug 04 '22 15:08 AadharshR

The root span may be on a different process which runs on a different machine. So really hard to get via OTel API. Even if the root span is in the same process in some app it may be already ended, exported and removed from memory.

Calculations like that ones you propose are not intended for the traced process, they should be done in the backend which has the full picture and does the correlation of all spans participating in a trace.

Flarna avatar Aug 05 '22 14:08 Flarna

We also don't keep a "registry" of spans locally. There is no mechanism in the SDK to look up even local spans by ID since storing all spans would leak memory

dyladan avatar Aug 05 '22 14:08 dyladan

Closing as this is impossible with the current architecture and spec

dyladan avatar Oct 10 '22 15:10 dyladan