-
Notifications
You must be signed in to change notification settings - Fork 810
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
[instrumentation-fetch] Add a requestHook to @opentelemetry/instrumentation-fetch #5084
Comments
Hi @patricklafrance . I am having trouble picturing exactly what you are trying to achieve that is not possible today. The fetch example app in the repo includes the usage of a If I am misunderstanding your question though, could you please provide a small reproducer, including what you expect to happen and what actually happens? |
Hey @JamieDanielson! Thanks for the reply. Indeed it looks similar. I am not familiar with zone.js though. One key difference with the example you provided is that in my case the fetch request is not triggered in the span scope. I am not sure if it matters? Otherwise, yeah maybe it could work. That being said, the ZoneContextManager code is not trivial to adapt to other use cases. It requires deep knowledge about how OpenTelemetry context works. I would argue that using a global variable to share the "active span" with a |
I can provide more specific information. For the following trace: The custom Then, a web application using our library would execute fetch requests using React Query once the Once those requests are done, another event is dispatched, and catch by our library function, to end the We expect the fetch requests to be nested under our I am not sure if those additional information help or are confusing hehe. Let me know. |
One last thing, correct me if I am wrong.. but I think using a solution similar to the ZoneContextManager would nest ANY spans created by automated instrumentation right? Instead of only nesting fetch span like Resulting in a trace similar to this one with nested |
Is your feature request related to a problem? Please describe.
I am trying to nest some
HTTP GET
spans created by@opentelemetry/instrumentation-fetch
automated instrumentation under a span created manually.The resulting trace should be something like this:
In the above screenshot, the
HTTP GET
spans created by@opentelemetry/instrumentation-fetch
automated instrumentation are nested under the manually createddata-fetch
span.I haven't found an elegant way to achieve this result using the official API. According to the documentation and the messages in this issue, spans should be nested under a parent using either the
tracer.startActiveSpan
function:or using the
context.with
function:Since my goal is to nest spans that are created by
@opentelemetry/instrumentation-fetch
, I don't think that I can use eithertracer.startActiveSpan
orcontext.with
.Another specificity of my current issue is that the manual spans are created by listening to events. For example, the
data-fetch
span is created when a listener receives adata-fetching-started
event:I tried to hack my way around this with a solution similar to the one described in this reply by providing a
applyCustomAttributesOnSpan
function to@opentelemetry/instrumentation-fetch
:The main difference is that instead of adding a custom attribute like
behavior.trace_id
, I am overriding thetrace.trace_id
andtrace.parent_id
attributes.It kind of works, this is how I have been able to capture the following screenshot:
BUT, then I found out that it break distributed tracing.... The reason is that the
applyCustomAttributesOnSpan
function is executed AFTER the request has been executed. Therefore, the requesttraceparent
header is using the originaltrace.trace_id
andtrace.parent_id
attributes instead of the overrides I am providing.As a result, the trace of the server are not bound to any existing span:
Then, I tried to define my own
ContextManager
to manually override the active context, something similar to the solution described in this reply.Doesn't work either, maybe I could get it working but there are MANY edge cases to handle AND it affect ALL the spans. I ended up with traces like the following:
I would prefer a solution that is scoped to spans created by
@opentelemetry/instrumentation-fetch
.Describe the solution you'd like
Ideally, a
requestHook
would be added to@opentelemetry/instrumentation-fetch
. Something similar to the requestHook ofinstrumentation-http
.This hook would allow an application to provide custom attributes BEFORE the request is executed. For my specific case, it would allow me to provide the
trace.trace_id
andtrace.parent_id
attributes before thetraceparent
header is added to the request.Describe alternatives you've considered
I tried the 3 solutions explained in the first section of this issue:
tracer.startActiveSpan
andcontext.with
APIsContextManager
as described hereAdditional context
N/A
The text was updated successfully, but these errors were encountered: