How to created nested spans without passing parents around
See original GitHub issue- This only affects the JavaScript OpenTelemetry library
- [_] This may affect other libraries, but I would like to get opinions here first
I’ve just started using this library in my project so it’s very possible I’m missing something obvious. Apologies if so!
I’ve gotten tracing with Zipkin working - really easily thanks to the guides 😃 I’ve also gotten Express tracing working, and the creation of custom spans. I can also do nested spans, but only if I pass the parent down the call hierarchy.
Is there some way to achieve nested spans without needing to pass the parent around? This is especially important if I want to have the child created several steps below the parent.
For example:
function a() {
const span = opentelemetry.trace.getTracer('default').startSpan('a');
b();
span.end();
}
function b() {
c();
}
function c() {
d();
}
function d() {
const span = ....; // What happens here?
span.end();
}
I’d really like to have the span created in d() be a child of the span created in a(), but without needing to pass that parent span down from a->b->c->d.
Is this possible at all?
And to make things even more complicated, I’d like to be able to do it for async functions as well. Is that possible at all?
I’m somewhat assuming that it is possible, because I’m assuming that the contrib libraries for things like HTTP and pg are doing similar, so that they participate in active spans instead of starting new ones. I’ve not made use of those yet to prove that though, and I’ve not been able to follow the code of the HTTP one to see how it could possibly do that.
Cheers
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)

Top Related StackOverflow Question
Should this issue still be open? This is now documented and by using
trace.startActiveSpanyou don’t need to do the dance of passing context and/or spans around: https://opentelemetry.io/docs/instrumentation/js/instrumentation/#create-nested-spans@sazzer that method is now at
otel.trace.setSpan.https://open-telemetry.github.io/opentelemetry-js-api/modules.html#trace