🪢 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
fix(otel): handle OTLP/JSON string-encoded numeric attribute values (#14047)
Steffen Schmitzf957e640b986
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex bb5e870ee7..96476daa26 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -1287,6 +1287,15 @@ export class OtelIngestionProcessor {return value.stringValue;}if (value.doubleValue !== undefined) {+ // OTLP/JSON encodes regular doubles as JSON numbers, but the special+ // values "NaN", "Infinity" and "-Infinity" are encoded as strings.+ // Coerce numeric strings; keep non-finite values as their string form so+ // we never emit NaN/Infinity into metadata (which is not valid JSON and+ // fails ingestion validation).+ if (typeof value.doubleValue === "string") {+ const parsed = Number(value.doubleValue);+ return Number.isFinite(parsed) ? parsed : value.doubleValue;+ }return value.doubleValue;}if (value.boolValue !== undefined) {@@ -1297,23 +1306,50 @@ export class OtelIngestionProcessor {this.convertValueToPlainJavascript(v),);}- if (value.intValue && value.intValue.high === 0) {- return value.intValue.low;+ if (value.intValue !== undefined) {+ const parsedInt = this.convertOtelIntValue(value.intValue);+ if (parsedInt !== undefined) {+ return parsedInt;+ }}- if (value.intValue && typeof value.intValue === "number") {- return value.intValue;+ return JSON.stringify(value);+ }++ /**+ * Converts an OTLP int64 attribute value into a plain number.+ *+ * The same logical value reaches us in different shapes depending on the+ * transport:+ * - protobuf (`application/x-protobuf`) is decoded into a Long-like object+ * `{ low, high, unsigned }`.+ * - OTLP/JSON (`application/json`) encodes int64 fields as decimal strings+ * (e.g. `"7"`) per the spec, since JSON numbers cannot safely represent+ * the full int64 range.+ * - some encoders send a plain JavaScript number.+ *+ * Returns `undefined` for values we cannot parse so the caller can fall back+ * to a safe representation instead of emitting `NaN`.+ */+ private convertOtelIntValue(intValue: any): number | undefined {+ if (typeof intValue === "number") {+ return intValue;}- if (- value.intValue &&- value.intValue.high === -1 &&- value.intValue.low === -1- ) {- return -1;+ if (typeof intValue === "string") {+ const parsed = Number(intValue);+ return Number.isNaN(parsed) ? undefined : parsed;}- if (value.intValue && value.intValue.high !== 0) {- return value.intValue.high * Math.pow(2, 32) + value.intValue.low;+ if (intValue && typeof intValue === "object") {+ if (intValue.high === 0) {+ return intValue.low;+ }+ if (intValue.high === -1 && intValue.low === -1) {+ return -1;+ }+ if (typeof intValue.high === "number") {+ return intValue.high * Math.pow(2, 32) + intValue.low;+ }}- return JSON.stringify(value);+ return undefined;}private convertKeyPathToNestedObject(diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex 851df107b1..6312ee606d 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -3670,6 +3670,39 @@ describe("OTel Resource Span Mapping", () => {entityAttributeValue: '{"temperature": 75, "condition": "sunny"}',},],+ [+ // OTLP/JSON encodes int64 attribute values as decimal strings (e.g. "100")+ // rather than the Long object produced by the protobuf decoder. See+ // https://opentelemetry.io/docs/specs/otlp/#json-protobuf-encoding+ "should convert OTLP/JSON string-encoded intValue (max_tokens)",+ {+ entity: "observation",+ otelAttributeKey: "gen_ai.request.max_tokens",+ otelAttributeValue: { intValue: "100" },+ entityAttributeKey: "modelParameters.max_tokens",+ entityAttributeValue: 100,+ },+ ],+ [+ "should convert OTLP/JSON string-encoded intValue (usage input)",+ {+ entity: "observation",+ otelAttributeKey: "gen_ai.usage.input_tokens",+ otelAttributeValue: { intValue: "100" },+ entityAttributeKey: "usageDetails.input",+ entityAttributeValue: 100,+ },+ ],+ [+ "should convert OTLP/JSON string-encoded doubleValue (cost)",+ {+ entity: "observation",+ otelAttributeKey: "gen_ai.usage.cost",+ otelAttributeValue: { doubleValue: "0.000151" },+ entityAttributeKey: "costDetails.total",+ entityAttributeValue: 0.000151,+ },+ ],])("Attributes: %s",async (@@ -3729,6 +3762,46 @@ describe("OTel Resource Span Mapping", () => {},);+ // Regression: OTLP/JSON exporters (e.g. the OpenTelemetry PHP SDK) send+ // int64 resource attributes as decimal strings (intValue: "7") rather than+ // the Long object produced by the protobuf decoder. Previously these were+ // converted to NaN, which failed ingestion validation on body.metadata.+ it("should convert OTLP/JSON string-encoded int resource attributes without producing NaN", async () => {+ // Setup - resource attributes as emitted by an OTLP/JSON exporter+ const resourceSpan = {+ resource: {+ attributes: [+ { key: "service.name", value: { stringValue: "langfuse-demo" } },+ { key: "process.pid", value: { intValue: "7" } },+ ],+ },+ scopeSpans: [+ {+ scope: { name: "demo-tracer" },+ spans: [defaultSpanProps],+ },+ ],+ };++ // When+ const langfuseEvents = await convertOtelSpanToIngestionEvent(+ resourceSpan,+ new Set(),+ );++ // Then - the int is parsed to a number, not NaN+ const resourceAttributes =+ langfuseEvents[1].body.metadata.resourceAttributes;+ expect(resourceAttributes["process.pid"]).toBe(7);+ expect(resourceAttributes["service.name"]).toBe("langfuse-demo");++ // And - the events pass ingestion validation (previously rejected with an+ // invalid_union error on body.metadata because of the NaN value)+ const schema = createIngestionEventSchema();+ const parsedEvents = langfuseEvents.map((event) => schema.parse(event));+ expect(parsedEvents).toHaveLength(2);+ });+it.each([["should extract version on trace from resource attribute service.version",