Context Propagation for Auto-Instrumentation in a Multi-Stage Bootstrap Environment #5802
Unanswered
gerardalvarez
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello OpenTelemetry Community,
I'm working with a large single-page application that has a complex, multi-stage bootstrap process. I'm facing a challenge getting the standard auto-instrumentations (like
instrumentation-fetch
) to respect a parent span that is created much later in the page lifecycle.I would greatly appreciate any guidance or recommended patterns for this scenario.
The Goal
I want all automatically generated spans to be children of a single, manually created
navigation
span. The desired result is a unified trace for the entire page load, like this:Currently, the
navigation
span is in one trace, and eachfetch
call creates its own new, separate trace.The Architecture & The Problem
Our application's loading process is the source of the issue:
telemetry-init.js
(First Script): This script loads very early. It is responsible for initializing theWebTracerProvider
withZoneContextManager
and callingregisterInstrumentations()
withgetWebAutoInstrumentations()
. At this moment, thefetch
andXHR
instrumentations become active, but they are running in a default, empty context.main-app.js
(Later Script): This script loads much later, after many other dependencies have been resolved. It contains our main application logic. It is only at this late stage that we have the necessary information (e.g., from session data) to create the rootnavigation
span that should be the parent of all activity.Because of this time gap, the auto-instrumentations are "live" long before the parent span they should be attached to even exists.
What I've Tried
context.with()
: Usingcontext.with()
around thenavigation
span creation in our later script doesn't work because the instrumentations have already been registered and don't retroactively adopt the new context.tracer.startSpan()
in the later script also seem to fail, likely because the instrumentations have already captured a reference to the original function during their early registration.The Core Question
What is the recommended approach for making auto-instrumentations aware of a parent span that is only created after they have been registered?
Is there a way to configure
getWebAutoInstrumentations
so that, at the moment a request is instrumented, it can dynamically "look up" the parent context from a custom, manually managed source?Illustrative Code
Here is a simplified representation of our setup:
MyManualContext.ts
(Our manual context holder)telemetry-init-early.ts
(The first script to run)main-app-init.ts
(Runs much later)My goal is to find a clean, reliable way for the
getWebAutoInstrumentations()
registered in the early script to use thenavigationSpan
from the late script as their parent.Thank you for your time and any insights you can provide!
Beta Was this translation helpful? Give feedback.
All reactions