Skip to content

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

fix(otel): Remove workaround to denormalize (internal) AI-feature child trace names (#19026)

Lysander Kiesel7f90045c1b25
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 59d7a562c8..71c806d200 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -32,10 +32,7 @@ import {
type IngestionSdkCanonicalName,
} from "../";
-import {
- AI_FEATURE_OTEL_SDK_NAME,
- LangfuseOtelSpanAttributes,
-} from "./attributes";
+import { LangfuseOtelSpanAttributes } from "./attributes";
import {
isClaudeManagedAgentSessionCreation,
isClaudeManagedAgentSpan,
@@ -739,10 +736,6 @@ export class OtelIngestionProcessor {
return events;
});
- if (this.sdkName === AI_FEATURE_OTEL_SDK_NAME) {
- this.denormalizeAiFeatureChildTraceNames(events);
- }
-
return events;
} catch (error) {
logger.error("Error processing OTEL spans to events:", {
@@ -755,45 +748,6 @@ export class OtelIngestionProcessor {
});
}
- /**
- * Copy the wrapping root's observation.traceName onto sibling events in the
- * same batch that omitted langfuse.trace.name. That OTEL attribute is a
- * trace-update signal; filling the events-table field keeps cost-by-trace-name
- * filters working without rewriting the trace.
- */
- private denormalizeAiFeatureChildTraceNames(
- events: Array<{
- traceId?: string;
- traceName?: string | null;
- parentSpanId?: string | null;
- isAppRoot?: boolean;
- }>,
- ): void {
- const traceNameByTraceId = new Map<string, string>();
-
- for (const event of events) {
- if (!event.traceId || !event.traceName) {
- continue;
- }
-
- const isRoot = !event.parentSpanId || event.isAppRoot === true;
- if (isRoot) {
- traceNameByTraceId.set(event.traceId, event.traceName);
- }
- }
-
- for (const event of events) {
- if (event.traceName || !event.traceId) {
- continue;
- }
-
- const traceName = traceNameByTraceId.get(event.traceId);
- if (traceName !== undefined) {
- event.traceName = traceName;
- }
- }
- }
-
/**
* Process resource spans and convert them to Langfuse ingestion events.
* Handles trace deduplication automatically using internal state.
diff --git a/packages/shared/src/server/otel/internalAiFeatureOtelWriter.test.ts b/packages/shared/src/server/otel/internalAiFeatureOtelWriter.test.ts
index d88dcce4a3..c3689e92a1 100644
--- a/packages/shared/src/server/otel/internalAiFeatureOtelWriter.test.ts
+++ b/packages/shared/src/server/otel/internalAiFeatureOtelWriter.test.ts
@@ -6,6 +6,7 @@ import {
AI_FEATURE_OTEL_SDK_NAME,
LangfuseOtelSpanAttributes,
} from "./attributes";
+import type { ResourceSpan } from "./OtelIngestionProcessor";
import { publishAiFeatureTraceViaOtelIngestion } from "./internalAiFeatureOtelWriter";
const publishToOtelIngestionQueue = vi.fn().mockResolvedValue(undefined);
@@ -95,38 +96,28 @@ const processedEvents = [
},
];
-const getPublishedResourceSpans = () => {
+const getPublishedResourceSpans = (): ResourceSpan[] => {
expect(publishToOtelIngestionQueue).toHaveBeenCalledTimes(1);
return publishToOtelIngestionQueue.mock.calls[0][0];
};
-const spanAttributeMap = (span: {
- attributes?: Array<{ key: string; value: { stringValue?: string } }>;
-}) =>
- Object.fromEntries(
- (span.attributes ?? []).map((attribute) => [
- attribute.key,
- attribute.value.stringValue,
- ]),
- );
-
-const createProcessor = async (sdkName = AI_FEATURE_OTEL_SDK_NAME) => {
+const createProcessor = async () => {
const { OtelIngestionProcessor } = await vi.importActual<
typeof import("./OtelIngestionProcessor")
>("./OtelIngestionProcessor");
return new OtelIngestionProcessor({
projectId: "project-1",
publicKey: "",
- sdkName,
+ sdkName: AI_FEATURE_OTEL_SDK_NAME,
sdkVersion: "unknown",
ingestionVersion: "4",
isLangfuseInternal: false,
});
};
-const processPublishedSpans = async (sdkName = AI_FEATURE_OTEL_SDK_NAME) => {
+const processPublishedSpans = async () => {
const resourceSpans = getPublishedResourceSpans();
- return (await createProcessor(sdkName)).processToEvent(resourceSpans);
+ return (await createProcessor()).processToEvent(resourceSpans);
};
const processPublishedIngestionEvents = async () => {
@@ -229,55 +220,45 @@ describe("publishAiFeatureTraceViaOtelIngestion", () => {
});
});
- it("keeps langfuse.trace.name on the wrapping root only", async () => {
- await publishFixture();
-
- const spans = getPublishedResourceSpans().flatMap((resourceSpan: any) =>
- resourceSpan.scopeSpans.flatMap((scopeSpan: any) => scopeSpan.spans),
- );
- const root = spans.find((span: any) => !span.parentSpanId);
- const children = spans.filter((span: any) => span.parentSpanId);
-
- expect(spanAttributeMap(root)[LangfuseOtelSpanAttributes.TRACE_NAME]).toBe(
- "agent-turn",
- );
- expect(children).toHaveLength(3);
- for (const child of children) {
- expect(
- spanAttributeMap(child)[LangfuseOtelSpanAttributes.TRACE_NAME],
- ).toBe(undefined);
- }
- });
-
- it("does not copy the batch-root traceName onto children for other SDK names", async () => {
+ it("does not infer a missing child trace name from other spans in the batch", async () => {
await publishFixture();
- const events = await processPublishedSpans("langfuse-internal-otel-writer");
- const wrappingRoot = events.find((event) => event.spanId === TRACE_ID);
- const generation = events.find(
- (event) => event.spanId === `${RUN_ID}-llm-0`,
- );
-
- expect(wrappingRoot?.traceName).toBe("agent-turn");
- expect(generation?.name).toBe("invoke-model");
- expect(generation?.traceName).toBeNull();
- });
-
- it("does not emit named trace-create rewrites from child spans", async () => {
- await publishFixture();
-
- const events = await processPublishedIngestionEvents();
- const namedTraceCreates = events.filter(
- (event) =>
- event.type === "trace-create" &&
- Boolean((event.body as { name?: string }).name),
- );
-
- expect(namedTraceCreates).toHaveLength(1);
- expect(namedTraceCreates[0]?.body).toMatchObject({
- id: TRACE_ID,
- name: "agent-turn",
- });
+ const resourceSpan = getPublishedResourceSpans()[0]!;
+ const scopeSpan = resourceSpan.scopeSpans![0]!;
+ const generation = scopeSpan.spans!.find(
+ (span) => span.name === "invoke-model",
+ )!;
+ const unnamedGeneration = {
+ ...generation,
+ attributes: generation.attributes?.filter(
+ (attribute) => attribute.key !== LangfuseOtelSpanAttributes.TRACE_NAME,
+ ),
+ };
+ const processor = await createProcessor();
+ const [independentEvent] = await processor.processToEvent([
+ {
+ ...resourceSpan,
+ scopeSpans: [{ ...scopeSpan, spans: [unnamedGeneration] }],
+ },
+ ]);
+ const batchedEvents = await processor.processToEvent([
+ {
+ ...resourceSpan,
+ scopeSpans: [
+ {
+ ...scopeSpan,
+ spans: scopeSpan.spans!.map((span) =>
+ span === generation ? unnamedGeneration : span,
+ ),
+ },
+ ],
+ },
+ ]);
+
+ expect(independentEvent?.traceName).toBeNull();
+ expect(
+ batchedEvents.find((event) => event.name === "invoke-model"),
+ ).toEqual(independentEvent);
});
it("links the generation prompt as an integer version in legacy ingestion events", async () => {
diff --git a/packages/shared/src/server/otel/internalAiFeatureOtelWriter.ts b/packages/shared/src/server/otel/internalAiFeatureOtelWriter.ts
index 6ac099813c..31d06623f6 100644
--- a/packages/shared/src/server/otel/internalAiFeatureOtelWriter.ts
+++ b/packages/shared/src/server/otel/internalAiFeatureOtelWriter.ts
@@ -1,6 +1,9 @@
import type { InternalTraceEventInput } from "../llm/internalTraceEvents";
import { type ResourceSpan } from "./OtelIngestionProcessor";
-import { AI_FEATURE_OTEL_SDK_NAME } from "./attributes";
+import {
+ AI_FEATURE_OTEL_SDK_NAME,
+ LangfuseOtelSpanAttributes,
+} from "./attributes";
import {
internalTraceEventToOtelAttributes,
publishOtelResourceSpans,
@@ -62,9 +65,10 @@ function aiFeatureTraceInputsToResourceSpans(
kind: 1,
startTimeUnixNano: timestampToUnixNano(eventInput.startTimeISO),
endTimeUnixNano: timestampToUnixNano(eventInput.endTimeISO),
- attributes: attributesToOtelList(
- internalTraceEventToOtelAttributes(eventInput, { isRoot }),
- ),
+ attributes: attributesToOtelList({
+ ...internalTraceEventToOtelAttributes(eventInput, { isRoot }),
+ [LangfuseOtelSpanAttributes.TRACE_NAME]: eventInput.traceName,
+ }),
status: {
...(eventInput.level === "ERROR" ? { code: 2 } : {}),
...(eventInput.statusMessage