Skip to content

Commit 170c1f3

Browse files
committed
fix: prevent IEEE 754 precision loss in OTLP nanosecond timestamps
Multiplying epoch milliseconds by 1,000,000 before BigInt conversion produces values exceeding Number.MAX_SAFE_INTEGER (~9e15), causing ~256ns precision errors in ~0.2% of timestamps. Convert to BigInt first, then multiply: - getNowInNanoseconds(): BigInt(ms) * BigInt(1_000_000) - calculateDurationFromStart(): same pattern - calculateDurationFromStartJsDate(): same pattern - recordRunDebugLog(): same pattern The correct pattern already existed in convertDateToNanoseconds() in the same file. Fixes #3292 Made-with: Cursor
1 parent 73ea586 commit 170c1f3

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

apps/webapp/app/v3/eventRepository/common.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function extractContextFromCarrier(carrier: Record<string, unknown>) {
2121
}
2222

2323
export function getNowInNanoseconds(): bigint {
24-
return BigInt(new Date().getTime() * 1_000_000);
24+
return BigInt(new Date().getTime()) * BigInt(1_000_000);
2525
}
2626

2727
export function getDateFromNanoseconds(nanoseconds: bigint): Date {
@@ -35,7 +35,7 @@ export function calculateDurationFromStart(
3535
) {
3636
const $endtime = typeof endTime === "string" ? new Date(endTime) : endTime;
3737

38-
const duration = Number(BigInt($endtime.getTime() * 1_000_000) - startTime);
38+
const duration = Number(BigInt($endtime.getTime()) * BigInt(1_000_000) - startTime);
3939

4040
if (minimumDuration && duration < minimumDuration) {
4141
return minimumDuration;
@@ -47,7 +47,7 @@ export function calculateDurationFromStart(
4747
export function calculateDurationFromStartJsDate(startTime: Date, endTime: Date = new Date()) {
4848
const $endtime = typeof endTime === "string" ? new Date(endTime) : endTime;
4949

50-
return ($endtime.getTime() - startTime.getTime()) * 1_000_000;
50+
return Number(BigInt($endtime.getTime() - startTime.getTime()) * BigInt(1_000_000));
5151
}
5252

5353
export function convertDateToNanoseconds(date: Date): bigint {

apps/webapp/app/v3/eventRepository/index.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ async function recordRunEvent(
215215
runId: foundRun.friendlyId,
216216
...attributes,
217217
},
218-
startTime: BigInt((startTime?.getTime() ?? Date.now()) * 1_000_000),
218+
startTime: BigInt(startTime?.getTime() ?? Date.now()) * BigInt(1_000_000),
219219
...optionsRest,
220220
});
221221

0 commit comments

Comments
 (0)