Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export {
export {
getDetailedTrace,
listTransactions,
normalizeTraceSpan,
} from "./api/traces.js";

export {
Expand Down
19 changes: 18 additions & 1 deletion src/lib/api/traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,24 @@ export async function getDetailedTrace(
},
}
);
return data;
return data.map(normalizeTraceSpan);
}

/**
* The trace detail API (`/trace/{id}/`) returns each span's unique identifier
* as `event_id` rather than `span_id`. The value is the same 16-hex-char span
* ID that `parent_span_id` references on child spans. We copy it to `span_id`
* so the rest of the codebase can use a single, predictable field name.
*/
export function normalizeTraceSpan(span: TraceSpan): TraceSpan {
const normalized = { ...span };
if (!normalized.span_id && normalized.event_id) {
normalized.span_id = normalized.event_id;
}
if (normalized.children) {
normalized.children = normalized.children.map(normalizeTraceSpan);
}
return normalized;
}

/** Fields to request from the transactions API */
Expand Down
2 changes: 2 additions & 0 deletions src/lib/formatters/human.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,8 @@ function formatSpanSimple(span: TraceSpan, opts: FormatSpanOptions): void {
line += ` ${muted(`(${prettyMs(durationMs)})`)}`;
}

line += ` ${muted(span.span_id ?? "")}`;

lines.push(line);

if (currentDepth < maxDepth) {
Expand Down
30 changes: 30 additions & 0 deletions test/lib/api-client.normalize-trace-span.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, test } from "bun:test";
import { normalizeTraceSpan } from "../../src/lib/api-client.js";

describe("normalizeTraceSpan", () => {
test("copies event_id to span_id when span_id is missing", () => {
const span = { event_id: "abc123", start_timestamp: 0 } as Parameters<
typeof normalizeTraceSpan
>[0];
const result = normalizeTraceSpan(span);
expect(result.span_id).toBe("abc123");
});

test("preserves existing span_id", () => {
const result = normalizeTraceSpan({
span_id: "existing",
event_id: "other",
start_timestamp: 0,
});
expect(result.span_id).toBe("existing");
});

test("recurses into children", () => {
const result = normalizeTraceSpan({
span_id: "parent",
start_timestamp: 0,
children: [{ event_id: "child1", start_timestamp: 1 } as any],
});
expect(result.children?.[0]?.span_id).toBe("child1");
});
});
Loading