Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .server-changes/fix-bigint-nanosecond-precision.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
area: webapp
type: fix
---

Timestamp precision is no longer lost when recording nanosecond-resolution span times. Previously, multiplying milliseconds by 1,000,000 in float-land before converting to BigInt produced slightly wrong timestamps for spans and run events.
4 changes: 2 additions & 2 deletions apps/webapp/app/v3/eventRepository/common.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function extractContextFromCarrier(carrier: Record<string, unknown>) {
}

export function getNowInNanoseconds(): bigint {
return BigInt(new Date().getTime() * 1_000_000);
return convertDateToNanoseconds(new Date());
}

export function getDateFromNanoseconds(nanoseconds: bigint): Date {
Expand All @@ -39,7 +39,7 @@ export function calculateDurationFromStart(
) {
const $endtime = typeof endTime === "string" ? new Date(endTime) : endTime;

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

if (minimumDuration && duration < minimumDuration) {
return minimumDuration;
Expand Down
3 changes: 2 additions & 1 deletion apps/webapp/app/v3/eventRepository/index.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { env } from "~/env.server";
import { eventRepository } from "./eventRepository.server";
import { type IEventRepository, type TraceEventOptions } from "./eventRepository.types";
import { convertDateToNanoseconds } from "./common.server";
import { prisma } from "~/db.server";
import { runStore } from "../runStore.server";
import { controlPlaneResolver } from "~/v3/runOpsMigration/controlPlaneResolver.server";
Expand Down Expand Up @@ -240,7 +241,7 @@ async function recordRunEvent(
runId: foundRun.friendlyId,
...attributes,
},
startTime: BigInt((startTime?.getTime() ?? Date.now()) * 1_000_000),
startTime: convertDateToNanoseconds(startTime ?? new Date()),
...optionsRest,
});

Expand Down