Skip to content

Commit 3827f4d

Browse files
committed
fix: use BigInt arithmetic for nanosecond timestamp conversion
Multiplying Date.getTime() by 1_000_000 as a float before converting to BigInt causes IEEE 754 precision loss once the product exceeds Number.MAX_SAFE_INTEGER (~9007T, which ms * 1e6 hits around 2255 AD). The correct pattern already exists in convertDateToNanoseconds(): BigInt(date.getTime()) * BigInt(1_000_000) Apply it consistently to the three call sites that still use the float-multiply pattern (getNowInNanoseconds, calculateDurationFromStart, and the runEngineHandlers retry-event recordEvent call).
1 parent 8fcb035 commit 3827f4d

4 files changed

Lines changed: 10 additions & 4 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: fix
4+
---
5+
6+
Fix OTLP trace timestamps losing precision for runs after approximately 2255 AD. Timestamps are now computed with full 64-bit integer arithmetic.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function extractContextFromCarrier(carrier: Record<string, unknown>) {
2626
}
2727

2828
export function getNowInNanoseconds(): bigint {
29-
return BigInt(new Date().getTime() * 1_000_000);
29+
return BigInt(new Date().getTime()) * BigInt(1_000_000);
3030
}
3131

3232
export function getDateFromNanoseconds(nanoseconds: bigint): Date {
@@ -40,7 +40,7 @@ export function calculateDurationFromStart(
4040
) {
4141
const $endtime = typeof endTime === "string" ? new Date(endTime) : endTime;
4242

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

4545
if (minimumDuration && duration < minimumDuration) {
4646
return minimumDuration;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ async function recordRunEvent(
208208
runId: foundRun.friendlyId,
209209
...attributes,
210210
},
211-
startTime: BigInt((startTime?.getTime() ?? Date.now()) * 1_000_000),
211+
startTime: BigInt(startTime?.getTime() ?? Date.now()) * BigInt(1_000_000),
212212
...optionsRest,
213213
});
214214

‎apps/webapp/app/v3/runEngineHandlers.server.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ export function registerRunEngineEventBusHandlers() {
557557
);
558558

559559
await eventRepository.recordEvent(retryMessage, {
560-
startTime: BigInt(time.getTime() * 1000000),
560+
startTime: BigInt(time.getTime()) * BigInt(1_000_000),
561561
taskSlug: run.taskIdentifier,
562562
environment,
563563
attributes: {

0 commit comments

Comments
 (0)