Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to safely get the root span from the tracer #725

Closed
alexrhogue opened this issue Oct 25, 2019 · 5 comments
Closed

Add the ability to safely get the root span from the tracer #725

alexrhogue opened this issue Oct 25, 2019 · 5 comments

Comments

@alexrhogue
Copy link

alexrhogue commented Oct 25, 2019

It would be very helpful to be able to get the root span of a request safely from the tracer object via a public function/getter. Currently to do this I need to use a private property (code provided by @rochdev):

// anywhere during the request lifecycle
const span = tracer.scope().active()

if (span) {
  const rootSpan = span.context()._trace.started[0];
}

I propose something like the following:

tracer.scope().rootSpan();
//or
tracer.scope().active().rootSpan();

This would make it much easier to add tags and information to the root of a request without having to communicate via the req object and/or plugin hooks.

@rochdev
Copy link
Member

rochdev commented Dec 3, 2020

This is unfortunately not something we can support since it would break for asynchronous workflows. I will close this for now, but please feel free to re-open if you have a use case where using span hooks doesn't work.

@rochdev rochdev closed this as completed Dec 3, 2020
@mintuhouse
Copy link

I want to addTags to localRootSpan (similar to https://docs.datadoghq.com/tracing/setup_overview/custom_instrumentation/java/#set-tags--errors-on-a-root-span-from-a-child-span in java)

I don't have access to express req at the point where I am adding these tags. Is there a way to addTags on localRootSpan?

@rochdev
Copy link
Member

rochdev commented Mar 23, 2021

@mintuhouse Unfortunately there is no official way right now. You can grab the root on the span context of any span within a trace with span.context()._trace.started[0] but that is a private API that might change.

@rkrv
Copy link

rkrv commented Apr 26, 2023

Hi folks, just chiming in about our use case. We are using Next.js and adding an owner tag on the entry span to route errors to appropriate teams that maintain those pages. Hooks are not enough in this case, as the owner metadata lives together with the source code for the Next.js page. Any suggestions for implementation besides using the span.context()._trace.started[0] snippet?

@TREFZ
Copy link

TREFZ commented Jul 12, 2023

if you have a use case where using span hooks doesn't work

I'm trying to do the same as the OP, add tags to the root span. But I'm unfamiliar with span hooks, so it's possible that would work for my use case. Can someone direct me to docs on span hooks, or an example of how that would work?

EDIT: I found it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants