Skip to content

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

fix(traces): set latency on root to fix timeline (#12697)

Thorsten Spieker335d3d6445f2
diff --git a/web/src/components/trace2/lib/tree-building.clienttest.ts b/web/src/components/trace2/lib/tree-building.clienttest.ts
index e068147e7c..3f707fb496 100644
--- a/web/src/components/trace2/lib/tree-building.clienttest.ts
+++ b/web/src/components/trace2/lib/tree-building.clienttest.ts
@@ -289,6 +289,36 @@ describe("buildTraceUiData", () => {
expect(result.roots[0].name).toBe("Generation");
});
+ it("propagates trace.latency to primary root observation node", () => {
+ const trace = createMockTrace({
+ id: "trace-1",
+ rootObservationType: "SPAN",
+ rootObservationId: "root-obs",
+ latency: 2.5,
+ });
+ const observations: ObservationReturnType[] = [
+ createMockObservation({
+ id: "root-obs",
+ name: "RootSpan",
+ parentObservationId: null,
+ startTime: new Date("2024-01-01T00:00:00.000Z"),
+ endTime: null, // root span without explicit endTime
+ }),
+ createMockObservation({
+ id: "child-obs",
+ parentObservationId: "root-obs",
+ startTime: new Date("2024-01-01T00:00:00.500Z"),
+ endTime: new Date("2024-01-01T00:00:02.500Z"),
+ }),
+ ];
+
+ const result = buildTraceUiData(trace, observations);
+
+ expect(result.roots).toHaveLength(1);
+ expect(result.roots[0].id).toBe("root-obs");
+ expect(result.roots[0].latency).toBe(2.5);
+ });
+
it("returns empty roots array for events-based trace with no observations", () => {
const trace = createMockTrace({
id: "trace-1",
diff --git a/web/src/components/trace2/lib/tree-building.ts b/web/src/components/trace2/lib/tree-building.ts
index a4465812b6..4a5975e952 100644
--- a/web/src/components/trace2/lib/tree-building.ts
+++ b/web/src/components/trace2/lib/tree-building.ts
@@ -384,6 +384,17 @@ function buildTraceTree(
// Events-based traces (rootObservationType set): return observations as roots directly
if (trace.rootObservationType) {
+ // In v3, trace.latency lives on the TRACE wrapper node. In v4 there's no
+ // wrapper, so propagate it to the primary root observation for timeline
+ // duration calculation.
+ if (trace.latency != null && trace.rootObservationId) {
+ const primaryRoot = rootTreeNodes.find(
+ (r) => r.id === trace.rootObservationId,
+ );
+ if (primaryRoot) {
+ primaryRoot.latency = trace.latency;
+ }
+ }
return { roots: rootTreeNodes, nodeMap };
}
diff --git a/web/src/components/trace2/lib/types.ts b/web/src/components/trace2/lib/types.ts
index 8654cd426e..6aeb35ba07 100644
--- a/web/src/components/trace2/lib/types.ts
+++ b/web/src/components/trace2/lib/types.ts
@@ -31,7 +31,7 @@ export type TreeNode = {
// Pre-computed cost for this node + all descendants
// Calculated bottom-up during tree construction for O(1) access
totalCost?: Decimal;
- // Trace-specific properties (when type === 'TRACE')
+ // Duration in seconds. Set on TRACE wrapper nodes (v3) or root observation nodes (v4 events-based traces).
latency?: number;
// Observation-specific properties (when type !== 'TRACE')
parentObservationId?: string | null;