Skip to content

Commit

Permalink
feat: (instrumentation): increase timestamp precision (#234)
Browse files Browse the repository at this point in the history
Signed-off-by: GALLLASMILAN <[email protected]>
  • Loading branch information
GALLLASMILAN authored Dec 6, 2024
1 parent cdb3731 commit 72c8717
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/instrumentation/create-telemetry-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ export function createTelemetryMiddleware() {

const eventsIterationsMap = new Map<string, Map<string, string>>();

const startTime = performance.now();
const startTimeDate = new Date().getTime();
const startTimePerf = performance.now();
function convertDateToPerformance(date: Date) {
return date.getTime() - startTimeDate + startTimePerf;
}

const serializer = traceSerializer({ ignored_keys: INSTRUMENTATION_IGNORED_KEYS });

function cleanSpanSources({ spanId }: { spanId: string }) {
Expand Down Expand Up @@ -140,7 +145,7 @@ export function createTelemetryMiddleware() {
traceId,
version: Version,
runErrorSpanKey: `${basePath}.run.${errorEventName}`,
startTime,
startTime: startTimePerf,
endTime: performance.now(),
source: activeTracesMap.get(traceId)!,
});
Expand Down Expand Up @@ -180,7 +185,7 @@ export function createTelemetryMiddleware() {
id: meta.groupId,
name: meta.groupId,
target: "groupId",
startedAt: meta.createdAt,
startedAt: convertDateToPerformance(meta.createdAt),
}),
);
groupIterations.push(meta.groupId);
Expand Down Expand Up @@ -209,7 +214,7 @@ export function createTelemetryMiddleware() {
ctx: getSerializedObjectSafe(meta.context),
data: serializedData,
error: getErrorSafe(data),
startedAt: meta.createdAt,
startedAt: convertDateToPerformance(meta.createdAt),
});

const lastIteration = groupIterations[groupIterations.length - 1];
Expand Down Expand Up @@ -296,7 +301,7 @@ export function createTelemetryMiddleware() {
id: spanId,
name: `${meta.name}Custom`,
target: path,
startedAt: meta.createdAt,
startedAt: convertDateToPerformance(meta.createdAt),
...(parentSpanId && { parent: { id: parentSpanId } }),
data: {
rawPrompt,
Expand Down
6 changes: 3 additions & 3 deletions src/instrumentation/helpers/create-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

import { SpanStatusCode } from "@opentelemetry/api";
import { SpanStatusCode, TimeInput } from "@opentelemetry/api";
import { FrameworkSpan } from "@/instrumentation/types.js";
import { isEmpty } from "remeda";

interface CreateSpanProps {
id: string;
name: string;
target: string;
startedAt: Date;
startedAt: TimeInput;
ctx?: any;
data?: any;
error?: string;
Expand Down Expand Up @@ -55,6 +55,6 @@ export function createSpan({
message: error ? error : "",
},
start_time: startedAt,
end_time: new Date(),
end_time: performance.now(),
};
}

0 comments on commit 72c8717

Please sign in to comment.