OpenTelemetry without zone.js
Keep the active span of OpenTelemetry through await in the browser, with the context manager of the library.
The problem
OpenTelemetry keeps the active span in a context. In the browser, the ZoneContextManager of OpenTelemetry keeps the context with zone.js. zone.js patches the callback APIs, but it does not see a native await. After an await, a span can get the wrong parent, or no parent.
AsyncContextManager keeps the context with the AsyncLocalStorage class of this library. In code that the Vite plugin or the Babel preset transforms, the active span stays correct after each await.
Install
npm install async-browser-context @opentelemetry/api @opentelemetry/sdk-trace-webSet up the transform. The first guide tells you how.
Register the context manager
// tracing.ts
import { WebTracerProvider } from "@opentelemetry/sdk-trace-web";
import { AsyncContextManager } from "async-browser-context/opentelemetry";
const provider = new WebTracerProvider();
provider.register({ contextManager : new AsyncContextManager() });Import async-browser-context before all other modules in the entry file of the application. The library patches the global functions when it loads (refer to Patched APIs).
Make spans
import { trace } from "@opentelemetry/api";
const tracer = trace.getTracer("shop");
async function checkout(cart : Cart) : Promise<void> {
await tracer.startActiveSpan("checkout", async (span) => {
const prices = await loadPrices(cart);
// The span of loadPrices() and of save() have the parent "checkout".
await save(cart, prices);
span.end();
});
}Without the library, the code after the first await can get the root context. Then the spans of save() start a new trace.
What the context manager does
| Method | What it does |
|---|---|
active() | Gives the active context, or the root context outside with(). |
with(context, fn, thisArg, ...args) | Starts fn with the context as the active context. The context stays after each await in transformed code, and in the callbacks of the patched APIs. |
bind(context, target) | For a function, gives a function that starts target in the context. The function keeps the this value, the arguments and the length. For an EventTarget or a Node.js EventEmitter, patches the methods that add and remove listeners. Other values stay as they are. Without a context, bind() uses the active context. |
disable() | active() gives the root context until the next with(). The next section tells where. |
bind(context, target) for an EventTarget or a Node.js EventEmitter operates as in the context managers of OpenTelemetry. Each listener function that the program adds later operates with the context. The remove methods find the bound listener. For an emitter, the patch replaces on, addListener, once, prependListener, prependOnceListener, off, removeListener and removeAllListeners.
On Node.js, the node export condition selects an entry with the native AsyncLocalStorage. Thus, code that runs on the server and in the browser can use one import.
disable()
disable() disables the AsyncLocalStorage of the context manager. The result depends on the runtime:
| Runtime | Effect of disable() |
|---|---|
The browser runtime, Node.js 22, and Node.js 24 with --no-async-context-frame | active() gives the root context in all code, until the next with(). |
Node.js 24, with AsyncContextFrame (the default) | active() gives the root context in the current context, also in the callbacks that this context registered. Other contexts keep their active context. |
Events
An event listener gets its active span from rule C13 of the library:
| The code that dispatches the event | The active span in the listener |
|---|---|
Code with an active span, for example dispatchEvent() in startActiveSpan() | The span of the dispatch |
Code outside all with() calls, for example at the top level of a module | The span of the addEventListener() call |
| The browser, for example for a click of the user | The span of the addEventListener() call |
If the listener must have no span, start the dispatch in context.with(ROOT_CONTEXT, ...). The with() method makes a context of the library, also for ROOT_CONTEXT. Thus, the listener gets ROOT_CONTEXT, and trace.getActiveSpan() gives undefined.
import { context, ROOT_CONTEXT } from "@opentelemetry/api";
context.with(ROOT_CONTEXT, () => target.dispatchEvent(new Event("refresh")));Cautionzone.js
Do not use the library together with zone.js or with the ZoneContextManager. The two libraries patch the same functions with different rules.
Tests
The tests in test/api/opentelemetry.test.ts use the context manager through the API of OpenTelemetry. They examine await, timers, then callbacks, two operations at the same time, the active span and disable(). They also examine bind() for a function, without a context, for an event emitter and for an event target. The tests operate on Node.js and in Chromium, Firefox and WebKit. The tests in test/browser/opentelemetry-events.test.ts examine three dispatches in the browsers: in the root context, in a span, and in context.with(ROOT_CONTEXT, ...).