Skip to content

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

fix: parse negative integer signs on otel timestamps (#6497)

Steffen Schmitzc1c22a9b9b68
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index 30b28d07f5..9b86ce09f3 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -1,4 +1,7 @@
-import { convertOtelSpanToIngestionEvent } from "@/src/features/otel/server";
+import {
+ convertOtelSpanToIngestionEvent,
+ convertNanoTimestampToISO,
+} from "@/src/features/otel/server";
import { ingestionEvent } from "@langfuse/shared/src/server";
describe("OTel Resource Span Mapping", () => {
@@ -1201,4 +1204,53 @@ describe("OTel Resource Span Mapping", () => {
},
);
});
+
+ describe("Timestamp Conversion", () => {
+ it("should correctly convert OpenTelemetry timestamps to ISO strings", () => {
+ // Test case with positive low value
+ const positiveTimestamp = {
+ low: 1095848032,
+ high: 406260507,
+ unsigned: true,
+ };
+
+ // Test case with negative low value
+ const negativeTimestamp = {
+ low: -1431863980,
+ high: 406260507,
+ unsigned: true,
+ };
+
+ // Expected ISO strings based on the provided mapping
+ const expectedStartTime = "2025-04-17T07:39:52.317Z";
+ const expectedEndTime = "2025-04-17T07:39:54.084Z";
+
+ // Convert timestamps to ISO strings
+ const actualStartTime = convertNanoTimestampToISO(positiveTimestamp);
+ const actualEndTime = convertNanoTimestampToISO(negativeTimestamp);
+
+ // Verify conversions match expected values
+ expect(actualStartTime).toBe(expectedStartTime);
+ expect(actualEndTime).toBe(expectedEndTime);
+ });
+
+ it("should handle various timestamp formats correctly", () => {
+ // Test with string timestamp (nanoseconds)
+ const stringTimestamp = "1744317592317227000"; // Same as positiveTimestamp above
+ const expectedStringResult = "2025-04-10T20:39:52.317Z";
+ expect(convertNanoTimestampToISO(stringTimestamp)).toBe(
+ expectedStringResult,
+ );
+
+ // Test with zero timestamp
+ const zeroTimestamp = {
+ low: 0,
+ high: 0,
+ unsigned: true,
+ };
+ expect(convertNanoTimestampToISO(zeroTimestamp)).toBe(
+ "1970-01-01T00:00:00.000Z",
+ );
+ });
+ });
});
diff --git a/web/src/features/otel/server/index.ts b/web/src/features/otel/server/index.ts
index 7efda97e30..65f02dd0bf 100644
--- a/web/src/features/otel/server/index.ts
+++ b/web/src/features/otel/server/index.ts
@@ -2,7 +2,7 @@ import { type IngestionEventType } from "@langfuse/shared/src/server";
import { randomUUID } from "crypto";
import { ObservationLevel } from "@langfuse/shared";
-const convertNanoTimestampToISO = (
+export const convertNanoTimestampToISO = (
timestamp:
| number
| string
@@ -12,14 +12,22 @@ const convertNanoTimestampToISO = (
},
) => {
if (typeof timestamp === "string") {
- return new Date(parseInt(timestamp, 10) / 1e6).toISOString();
+ return new Date(Number(BigInt(timestamp) / BigInt(1e6))).toISOString();
}
if (typeof timestamp === "number") {
return new Date(timestamp / 1e6).toISOString();
}
- return new Date(
- (timestamp.high * Math.pow(2, 32) + timestamp.low) / 1e6,
- ).toISOString();
+
+ // Convert high and low to BigInt
+ const highBits = BigInt(timestamp.high) << BigInt(32);
+ const lowBits = BigInt(timestamp.low >>> 0);
+
+ // Combine high and low bits
+ const nanosBigInt = highBits | lowBits;
+
+ // Convert nanoseconds to milliseconds for JavaScript Date
+ const millisBigInt = nanosBigInt / BigInt(1000000);
+ return new Date(Number(millisBigInt)).toISOString();
};
const convertValueToPlainJavascript = (value: Record<string, any>): any => {
@@ -420,9 +428,7 @@ const extractCostDetails = (
return {};
};
-const extractTags = (
- attributes: Record<string, unknown>,
-): string[] => {
+const extractTags = (attributes: Record<string, unknown>): string[] => {
const tagsValue = attributes["langfuse.tags"];
// If no tags, return empty array
@@ -432,7 +438,7 @@ const extractTags = (
// If already an array (converted by convertValueToPlainJavascript)
if (Array.isArray(tagsValue)) {
- return tagsValue.map(tag => String(tag));
+ return tagsValue.map((tag) => String(tag));
}
// If JSON string array
@@ -440,7 +446,7 @@ const extractTags = (
try {
const parsedTags = JSON.parse(tagsValue);
if (Array.isArray(parsedTags)) {
- return parsedTags.map(tag => String(tag));
+ return parsedTags.map((tag) => String(tag));
}
} catch (e) {
// If parsing fails, continue with other methods
@@ -449,7 +455,7 @@ const extractTags = (
// If CSV string
if (typeof tagsValue === "string" && tagsValue.includes(",")) {
- return tagsValue.split(",").map(tag => tag.trim());
+ return tagsValue.split(",").map((tag) => tag.trim());
}
// If single string value