Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(instrumentation): preserve OTel baggage across startNewTrace (#14953)

Mark Salpeter88ca8b026eb9
diff --git a/packages/shared/src/server/instrumentation/index.test.ts b/packages/shared/src/server/instrumentation/index.test.ts
new file mode 100644
index 0000000000..de2628f6f4
--- /dev/null
+++ b/packages/shared/src/server/instrumentation/index.test.ts
@@ -0,0 +1,59 @@
+import { context } from "@opentelemetry/api";
+import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import { normalizeClickHouseQueryTags } from "../clickhouse/queryTags";
+import { contextWithLangfuseProps } from "../headerPropagation";
+import { instrumentAsync, instrumentSync } from ".";
+
+describe("instrumentation baggage propagation", () => {
+ // Baggage only propagates through context.with once a manager is registered.
+ const contextManager = new AsyncLocalStorageContextManager();
+
+ beforeAll(() => {
+ contextManager.enable();
+ context.setGlobalContextManager(contextManager);
+ });
+
+ afterAll(() => {
+ context.disable();
+ });
+
+ it("instrumentAsync keeps worker surface/route across startNewTrace", async () => {
+ const workerContext = contextWithLangfuseProps({
+ projectId: "project-1",
+ clickhouse: { surface: "worker", route: "langfuse.queue.monitor" },
+ });
+
+ const tags = await context.with(workerContext, () =>
+ instrumentAsync(
+ { name: "process monitor", startNewTrace: true },
+ async () => normalizeClickHouseQueryTags(),
+ ),
+ );
+
+ expect(tags).toMatchObject({
+ surface: "worker",
+ route: "langfuse.queue.monitor",
+ projectId: "project-1",
+ });
+ });
+
+ it("instrumentSync keeps worker surface/route across startNewTrace", () => {
+ const workerContext = contextWithLangfuseProps({
+ projectId: "project-1",
+ clickhouse: { surface: "worker", route: "langfuse.queue.monitor" },
+ });
+
+ const tags = context.with(workerContext, () =>
+ instrumentSync({ name: "process monitor", startNewTrace: true }, () =>
+ normalizeClickHouseQueryTags(),
+ ),
+ );
+
+ expect(tags).toMatchObject({
+ surface: "worker",
+ route: "langfuse.queue.monitor",
+ projectId: "project-1",
+ });
+ });
+});
diff --git a/packages/shared/src/server/instrumentation/index.ts b/packages/shared/src/server/instrumentation/index.ts
index 29487cd696..73067ecfbf 100644
--- a/packages/shared/src/server/instrumentation/index.ts
+++ b/packages/shared/src/server/instrumentation/index.ts
@@ -51,12 +51,18 @@ export type SpanCtx = {
type AsyncCallbackFn<T> = (span: opentelemetry.Span) => Promise<T>;
+/** instrumentAsync runs an async callback inside a fresh OTel span. */
export async function instrumentAsync<T>(
ctx: SpanCtx,
callback: AsyncCallbackFn<T>,
): Promise<T> {
const activeContext = ctx.startNewTrace
- ? opentelemetry.ROOT_CONTEXT
+ ? // Sever the parent trace but carry baggage onto the new root.
+ opentelemetry.propagation.setBaggage(
+ opentelemetry.ROOT_CONTEXT,
+ opentelemetry.propagation.getBaggage(opentelemetry.context.active()) ??
+ opentelemetry.propagation.createBaggage(),
+ )
: ctx.traceContext
? opentelemetry.propagation.extract(
opentelemetry.context.active(),
@@ -95,12 +101,18 @@ export async function instrumentAsync<T>(
type SyncCallbackFn<T> = (span: opentelemetry.Span) => T;
+/** instrumentSync runs a callback inside a fresh OTel span. */
export function instrumentSync<T>(
ctx: SpanCtx,
callback: SyncCallbackFn<T>,
): T {
const activeContext = ctx.startNewTrace
- ? opentelemetry.ROOT_CONTEXT
+ ? // Sever the parent trace but carry baggage onto the new root.
+ opentelemetry.propagation.setBaggage(
+ opentelemetry.ROOT_CONTEXT,
+ opentelemetry.propagation.getBaggage(opentelemetry.context.active()) ??
+ opentelemetry.propagation.createBaggage(),
+ )
: ctx.traceContext
? opentelemetry.propagation.extract(
opentelemetry.context.active(),