Skip to content

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

fix(otel): preserve batches with valueless attributes (#18104)

Valery Meleshkinf5967f3b9e0e
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.metadataDropped.test.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.metadataDropped.test.ts
index 06039eedac..0ee33c84ae 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.metadataDropped.test.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.metadataDropped.test.ts
@@ -58,7 +58,10 @@ const createProcessor = () =>
sdkVersion: "3.8.1",
});
-type OtelAttribute = { key: string; value: Record<string, unknown> };
+type OtelAttribute = {
+ key: string;
+ value: Record<string, unknown> | null | undefined;
+};
const buildBatch = (attributes: OtelAttribute[]): ResourceSpan[] => [
{
@@ -97,6 +100,23 @@ const buildBatch = (attributes: OtelAttribute[]): ResourceSpan[] => [
},
];
+const getObservations = async (path: "v3" | "v4", batch: ResourceSpan[]) => {
+ const processor = createProcessor();
+ if (path === "v4") {
+ return processor.processToEvent(batch);
+ }
+
+ return (await processor.processToIngestionEvents(batch))
+ .filter((event) => event.type === "span-create")
+ .map((event) => event.body);
+};
+
+const rawEmptyAttributes = (prefix: string) => [
+ { key: `${prefix}.missing`, value: undefined },
+ { key: `${prefix}.null`, value: null },
+ { key: `${prefix}.empty`, value: {} },
+];
+
const droppedCalls = () =>
recordIncrementMock.mock.calls.filter(([stat]) => stat === METRIC);
@@ -222,6 +242,168 @@ describe("gateway metadata", () => {
);
});
+describe("raw OTLP empty attribute values", () => {
+ it.each(["v3", "v4"] as const)(
+ "omits missing and empty values while preserving fallbacks and siblings on %s",
+ async (path) => {
+ const batch = buildBatch([
+ { key: "ai.operationId", value: undefined },
+ { key: "ai.prompt.messages", value: null },
+ { key: "ai.prompt", value: { stringValue: "prompt-fallback" } },
+ { key: "ai.result.text", value: {} },
+ {
+ key: "ai.response.object",
+ value: { stringValue: "response-fallback" },
+ },
+ { key: "gen_ai.usage.prompt_tokens", value: null },
+ { key: "gen_ai.usage.input_tokens", value: { intValue: "42" } },
+ { key: "gen_ai.usage.completion_tokens", value: {} },
+ { key: "gen_ai.usage.output_tokens", value: { intValue: "7" } },
+ {
+ key: "langfuse.observation.metadata.encodedNull",
+ value: { stringValue: null },
+ },
+ ]);
+ batch[0].resource!.attributes!.push(...rawEmptyAttributes("resource"), {
+ key: "resource.inheritedEmpty",
+ value: Object.create({ stringValue: null }),
+ });
+ const scopeSpan = batch[0].scopeSpans![0];
+ scopeSpan.scope!.name = "ai";
+ scopeSpan.scope!.attributes!.push(...rawEmptyAttributes("scope"));
+ scopeSpan.spans!.push({
+ ...scopeSpan.spans![0],
+ traceId: Buffer.from("fedcba9876543210fedcba9876543210", "hex"),
+ spanId: Buffer.from("fedcba9876543210", "hex"),
+ name: "sibling-span",
+ attributes: [
+ {
+ key: "langfuse.observation.type",
+ value: { stringValue: "span" },
+ },
+ ],
+ });
+
+ const observations = await getObservations(path, batch);
+ const primary = observations.find(
+ (observation) => observation.name === "test-span",
+ );
+ const sibling = observations.find(
+ (observation) => observation.name === "sibling-span",
+ );
+
+ expect(primary).toMatchObject({
+ name: "test-span",
+ input: "prompt-fallback",
+ output: "response-fallback",
+ ...(path === "v4"
+ ? { providedUsageDetails: { input: 42, output: 7 } }
+ : { usageDetails: { input: 42, output: 7 } }),
+ });
+ expect(primary?.metadata).toMatchObject({
+ encodedNull: null,
+ resourceAttributes: { "service.name": "test-svc" },
+ scope: { attributes: { public_key: "pk-test" } },
+ });
+ expect(primary?.metadata?.resourceAttributes).toEqual({
+ "service.name": "test-svc",
+ });
+ expect(primary?.metadata?.scope?.attributes).toEqual({
+ public_key: "pk-test",
+ });
+ expect(primary?.metadata?.attributes).not.toHaveProperty([
+ "ai.operationId",
+ ]);
+ expect(sibling).toMatchObject({ name: "sibling-span" });
+ },
+ );
+
+ it("preserves typed array values, nested positions, and empty arrays", () => {
+ const batch = buildBatch([
+ {
+ key: "langfuse.observation.metadata.values",
+ value: {
+ arrayValue: {
+ values: [
+ { stringValue: "" },
+ { boolValue: false },
+ { intValue: "0" },
+ { doubleValue: 0 },
+ {},
+ null,
+ {
+ arrayValue: {
+ values: [
+ { stringValue: "nested" },
+ {},
+ null,
+ { arrayValue: {} },
+ ],
+ },
+ },
+ ],
+ },
+ },
+ },
+ {
+ key: "langfuse.observation.metadata.emptyArray",
+ value: { arrayValue: {} },
+ },
+ ]);
+
+ const observation = createProcessor().processToEvent(batch)[0];
+ expect(observation.metadata).toMatchObject({
+ values: ["", false, 0, 0, null, null, ["nested", null, null, []]],
+ emptyArray: [],
+ });
+ });
+
+ it("omits empty event attributes while preserving typed falsy values", () => {
+ const empty = [
+ { key: "missing", value: undefined },
+ { key: "null", value: null },
+ { key: "empty", value: {} },
+ ];
+ const falsy = [
+ { key: "enabled", value: { boolValue: false } },
+ { key: "zero", value: { intValue: "0" } },
+ { key: "blank", value: { stringValue: "" } },
+ ];
+ const batch = buildBatch([]);
+ batch[0].scopeSpans![0].spans![0].events = [
+ {
+ name: "gen_ai.system.message",
+ attributes: [
+ { key: "content", value: { stringValue: "system" } },
+ ...empty,
+ ...falsy,
+ ],
+ },
+ {
+ name: "gen_ai.choice",
+ attributes: [
+ { key: "text", value: { stringValue: "choice" } },
+ ...empty,
+ ...falsy,
+ ],
+ },
+ ];
+
+ expect(createProcessor().processToEvent(batch)[0]).toMatchObject({
+ input: [
+ {
+ role: "system",
+ content: "system",
+ enabled: false,
+ zero: 0,
+ blank: "",
+ },
+ ],
+ output: { text: "choice", enabled: false, zero: 0, blank: "" },
+ });
+ });
+});
+
describe("OTel metadata_dropped metric", () => {
beforeEach(() => {
recordIncrementMock.mockClear();
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index a61896e639..f958ebd618 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -1360,7 +1360,8 @@ export class OtelIngestionProcessor {
): Record<string, unknown> {
return (
resourceSpan?.resource?.attributes?.reduce((acc: any, attr: any) => {
- acc[attr.key] = this.convertValueToPlainJavascript(attr.value);
+ const value = this.convertValueToPlainJavascript(attr.value);
+ if (value !== undefined) acc[attr.key] = value;
return acc;
}, {}) ?? {}
);
@@ -1369,7 +1370,8 @@ export class OtelIngestionProcessor {
private extractScopeAttributes(scopeSpan: any): Record<string, unknown> {
return (
scopeSpan?.scope?.attributes?.reduce((acc: any, attr: any) => {
- acc[attr.key] = this.convertValueToPlainJavascript(attr.value);
+ const value = this.convertValueToPlainJavascript(attr.value);
+ if (value !== undefined) acc[attr.key] = value;
return acc;
}, {}) ?? {}
);
@@ -1378,7 +1380,8 @@ export class OtelIngestionProcessor {
private extractSpanAttributes(span: any): Record<string, unknown> {
return (
span?.attributes?.reduce((acc: any, attr: any) => {
- acc[attr.key] = this.convertValueToPlainJavascript(attr.value);
+ const value = this.convertValueToPlainJavascript(attr.value);
+ if (value !== undefined) acc[attr.key] = value;
return acc;
}, {}) ?? {}
);
@@ -1525,7 +1528,18 @@ export class OtelIngestionProcessor {
});
}
- private convertValueToPlainJavascript(value: Record<string, any>): any {
+ // Empty AnyValues are omitted from attribute maps; array callers preserve
+ // their positions as null instead.
+ private convertValueToPlainJavascript(
+ value: Record<string, any> | null | undefined,
+ ): any {
+ if (
+ value == null ||
+ (OtelIngestionProcessor.isPlainObject(value) &&
+ Object.keys(value).length === 0)
+ ) {
+ return undefined;
+ }
if (value.stringValue !== undefined) {
return value.stringValue;
}
@@ -1544,9 +1558,9 @@ export class OtelIngestionProcessor {
if (value.boolValue !== undefined) {
return value.boolValue;
}
- if (value.arrayValue && value.arrayValue.values !== undefined) {
- return value.arrayValue.values.map((v: any) =>
- this.convertValueToPlainJavascript(v),
+ if (value.arrayValue) {
+ return (value.arrayValue.values ?? []).map(
+ (v: any) => this.convertValueToPlainJavascript(v) ?? null,
);
}
if (value.intValue !== undefined) {
@@ -2046,7 +2060,8 @@ export class OtelIngestionProcessor {
const eventAttributes: Record<string, unknown> =
event.attributes?.reduce(
(acc: Record<string, unknown>, attr: any) => {
- acc[attr.key] = this.convertValueToPlainJavascript(attr.value);
+ const value = this.convertValueToPlainJavascript(attr.value);
+ if (value !== undefined) acc[attr.key] = value;
return acc;
},
{} as Record<string, unknown>,
@@ -2077,9 +2092,8 @@ export class OtelIngestionProcessor {
? inputEvents.map((event: any) => {
const eventAttributes =
event.attributes?.reduce((acc: any, attr: any) => {
- acc[attr.key] = this.convertValueToPlainJavascript(
- attr.value,
- );
+ const value = this.convertValueToPlainJavascript(attr.value);
+ if (value !== undefined) acc[attr.key] = value;
return acc;
}, {}) ?? {};
@@ -2095,9 +2109,8 @@ export class OtelIngestionProcessor {
? outputEvents.map((event: any) => {
const eventAttributes =
event.attributes?.reduce((acc: any, attr: any) => {
- acc[attr.key] = this.convertValueToPlainJavascript(
- attr.value,
- );
+ const value = this.convertValueToPlainJavascript(attr.value);
+ if (value !== undefined) acc[attr.key] = value;
return acc;
}, {}) ?? {};
@@ -2129,12 +2142,14 @@ export class OtelIngestionProcessor {
if (input || output) {
input =
input?.reduce((acc: any, attr: any) => {
- acc[attr.key] = this.convertValueToPlainJavascript(attr.value);
+ const value = this.convertValueToPlainJavascript(attr.value);
+ if (value !== undefined) acc[attr.key] = value;
return acc;
}, {}) ?? {};
output =
output?.reduce((acc: any, attr: any) => {
- acc[attr.key] = this.convertValueToPlainJavascript(attr.value);
+ const value = this.convertValueToPlainJavascript(attr.value);
+ if (value !== undefined) acc[attr.key] = value;
return acc;
}, {}) ?? {};
diff --git a/worker/src/queues/__tests__/otelConversionFailureLogging.test.ts b/worker/src/queues/__tests__/otelConversionFailureLogging.test.ts
index d999a11ec6..cc472e3905 100644
--- a/worker/src/queues/__tests__/otelConversionFailureLogging.test.ts
+++ b/worker/src/queues/__tests__/otelConversionFailureLogging.test.ts
@@ -27,27 +27,6 @@ const expectedContext = {
instrumentationScopes: ["opentelemetry.instrumentation.openai"],
};
-// Crash site: convertValueToPlainJavascript reads `value.stringValue` on undefined
-const batchWithAttributeWithoutValue = [
- {
- scopeSpans: [
- {
- scope: { name: "opentelemetry.instrumentation.openai" },
- spans: [
- {
- traceId: "0123456789abcdef0123456789abcdef",
- spanId: "0123456789abcdef",
- name: "chat openai",
- attributes: [{ key: "gen_ai.system" }],
- startTimeUnixNano: "1752384000000000000",
- endTimeUnixNano: "1752384001000000000",
- },
- ],
- },
- ],
- },
-];
-
// Crash site: parseId calls Buffer.from(undefined) when traceId is missing
const batchWithSpanWithoutTraceId = [
{
@@ -73,23 +52,6 @@ describe("OtelIngestionProcessor conversion failure logging", () => {
vi.restoreAllMocks();
});
- it("logs attribution context when processToIngestionEvents fails on an attribute without value", async () => {
- const errorSpy = vi.spyOn(logger, "error");
-
- const result = await createProcessor().processToIngestionEvents(
- batchWithAttributeWithoutValue,
- );
-
- expect(result).toEqual([]);
- expect(errorSpy).toHaveBeenCalledWith(
- "Error processing OTEL spans:",
- expect.objectContaining({
- ...expectedContext,
- error: expect.any(Error),
- }),
- );
- });
-
it("logs attribution context when processToIngestionEvents fails on a span without traceId", async () => {
const errorSpy = vi.spyOn(logger, "error");