🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore(otel): log SDK, version, instrumentation scopes, and span count on OTEL conversion failures (LFE-10869) (#15023)
Niklas Semmler07b6661df0f2
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex a7d53f0e0d..426a8d62ad 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -51,6 +51,12 @@ export interface OtelIngestionProcessorConfig {* schema strips.*/isLangfuseInternal?: boolean;+ /**+ * S3 key of the raw OTLP payload this batch was replayed from. Only set on+ * the queue-consumer side; included in conversion failure logs so a single+ * log line points at the replayable payload.+ */+ fileKey?: string;}interface CreateTraceEventParams {@@ -166,6 +172,7 @@ export class OtelIngestionProcessor {private readonly sdkVersion: string;private readonly ingestionVersion?: string;private readonly isLangfuseInternal?: boolean;+ private readonly fileKey?: string;constructor(config: OtelIngestionProcessorConfig) {this.projectId = config.projectId;@@ -180,6 +187,7 @@ export class OtelIngestionProcessor {// only used as a write-path hint, not as SDK attribution.this.ingestionVersion = config.ingestionVersion;this.isLangfuseInternal = config.isLangfuseInternal;+ this.fileKey = config.fileKey;}/**@@ -540,7 +548,10 @@ export class OtelIngestionProcessor {return events;});} catch (error) {- logger.error("Error processing OTEL spans to events:", error);+ logger.error("Error processing OTEL spans to events:", {+ error,+ ...this.getConversionFailureLogContext(resourceSpans),+ });traceException(error, span);throw error;}@@ -617,7 +628,10 @@ export class OtelIngestionProcessor {}// Log error but don't throw to avoid breaking the ingestion pipeline- logger.error("Error processing OTEL spans:", error);+ logger.error("Error processing OTEL spans:", {+ error,+ ...this.getConversionFailureLogContext(resourceSpans),+ });traceException(error, span);return [];@@ -3171,6 +3185,52 @@ export class OtelIngestionProcessor {});}+ /**+ * Attribution context for conversion failure logs so a Datadog log line+ * answers which SDK/version/instrumentation produced a malformed batch and+ * how many spans were lost. Must never throw.+ */+ private getConversionFailureLogContext(+ resourceSpans: ResourceSpan[],+ ): Record<string, unknown> {+ return {+ projectId: this.projectId,+ sdkName: this.sdkName,+ sdkVersion: this.sdkVersion,+ fileKey: this.fileKey,+ spanCount: this.getTotalSpanCount(resourceSpans),+ instrumentationScopes: this.getInstrumentationScopes(resourceSpans),+ };+ }++ private getInstrumentationScopes(+ resourceSpans: ResourceSpan[],+ limit = 10,+ ): string[] {+ try {+ if (!Array.isArray(resourceSpans)) {+ return [];+ }++ const scopes = new Set<string>();+ for (const resourceSpan of resourceSpans) {+ for (const scopeSpan of resourceSpan?.scopeSpans ?? []) {+ const name = scopeSpan?.scope?.name;+ if (name) {+ scopes.add(name);+ if (scopes.size >= limit) {+ return [...scopes];+ }+ }+ }+ }+ return [...scopes];+ } catch (error) {+ logger.warn("Failed to collect instrumentation scopes:", error);+ return [];+ }+ }+/*** Count the total number of spans across all resource spans.* Returns -1 if an error occurs during counting to avoid throwing exceptions.diff --git a/worker/src/queues/__tests__/otelConversionFailureLogging.test.ts b/worker/src/queues/__tests__/otelConversionFailureLogging.test.tsnew file mode 100644index 0000000000..d999a11ec6--- /dev/null+++ b/worker/src/queues/__tests__/otelConversionFailureLogging.test.ts@@ -0,0 +1,125 @@+/**+ * Tests that OTEL conversion failures log structured attribution context+ * (SDK name/version, instrumentation scopes, span count, project id, file key)+ * so a single Datadog log line answers which SDK produced a malformed batch+ * and how many spans were lost.+ */+import { describe, it, expect, afterEach, vi } from "vitest";+import { logger, OtelIngestionProcessor } from "@langfuse/shared/src/server";++const FILE_KEY = "events/otel/test-project/2026/07/13/batch.json";++const createProcessor = () =>+ new OtelIngestionProcessor({+ projectId: "test-project",+ publicKey: "test-public-key",+ sdkName: "python",+ sdkVersion: "3.2.1",+ fileKey: FILE_KEY,+ });++const expectedContext = {+ projectId: "test-project",+ sdkName: "python",+ sdkVersion: "3.2.1",+ fileKey: FILE_KEY,+ spanCount: 1,+ 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 = [+ {+ scopeSpans: [+ {+ scope: { name: "opentelemetry.instrumentation.openai" },+ spans: [+ {+ spanId: "0123456789abcdef",+ name: "chat openai",+ attributes: [],+ startTimeUnixNano: "1752384000000000000",+ endTimeUnixNano: "1752384001000000000",+ },+ ],+ },+ ],+ },+];++describe("OtelIngestionProcessor conversion failure logging", () => {+ afterEach(() => {+ 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");++ const result = await createProcessor().processToIngestionEvents(+ batchWithSpanWithoutTraceId,+ );++ expect(result).toEqual([]);+ expect(errorSpy).toHaveBeenCalledWith(+ "Error processing OTEL spans:",+ expect.objectContaining({+ ...expectedContext,+ error: expect.any(Error),+ }),+ );+ });++ it("logs attribution context when processToEvent fails on a span without traceId", () => {+ const errorSpy = vi.spyOn(logger, "error");++ expect(() =>+ createProcessor().processToEvent(batchWithSpanWithoutTraceId),+ ).toThrow();++ expect(errorSpy).toHaveBeenCalledWith(+ "Error processing OTEL spans to events:",+ expect.objectContaining({+ ...expectedContext,+ error: expect.any(Error),+ }),+ );+ });+});diff --git a/worker/src/queues/otelIngestionQueue.ts b/worker/src/queues/otelIngestionQueue.tsindex 7c7e50c486..2f2688dffc 100644--- a/worker/src/queues/otelIngestionQueue.ts+++ b/worker/src/queues/otelIngestionQueue.ts@@ -314,6 +314,7 @@ export const otelIngestionQueueProcessorBuilder = (publicKey: attribution.ingestionApiKey,sdkName: attribution.ingestionSdkName,sdkVersion: attribution.ingestionSdkVersion,+ fileKey,});const events: IngestionEventType[] =await processor.processToIngestionEvents(parsedSpans);