Skip to content

Commit e25ffbd

Browse files
committed
fix(diagnostics): include browser trace failures
1 parent 3b95e78 commit e25ffbd

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

‎apps/server/src/diagnostics/TraceDiagnostics.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,35 @@ function readTraces(fileSystem: Partial<FileSystem.FileSystem>) {
6161
}
6262

6363
describe("TraceDiagnostics", () => {
64+
it("includes browser failures and interruptions in diagnostics", () => {
65+
const diagnostics = aggregateLines(
66+
[
67+
{ status: { code: "2", message: "Browser request failed" }, attributes: {} },
68+
{ status: { code: "0" }, attributes: { "effect.fiber.interrupted": true } },
69+
{ status: { code: "1", message: "Interrupted" }, attributes: {} },
70+
{ status: { code: "1" }, attributes: {} },
71+
].map((outcome, index) =>
72+
JSON.stringify({
73+
type: "otlp-span",
74+
name: "browser.request",
75+
traceId: `trace-${index}`,
76+
spanId: `span-${index}`,
77+
startTimeUnixNano: ns(index * 100),
78+
endTimeUnixNano: ns(index * 100 + 10),
79+
durationMs: 10,
80+
...outcome,
81+
}),
82+
),
83+
);
84+
85+
assert.equal(diagnostics.recordCount, 4);
86+
assert.equal(diagnostics.failureCount, 1);
87+
assert.equal(diagnostics.interruptionCount, 2);
88+
assert.equal(diagnostics.topSpansByCount[0]?.failureCount, 1);
89+
assert.equal(diagnostics.commonFailures[0]?.cause, "Browser request failed");
90+
assert.equal(diagnostics.latestFailures[0]?.cause, "Browser request failed");
91+
});
92+
6493
it.effect("aggregates failures, slow spans, log levels, and parse errors", () =>
6594
Effect.sync(() => {
6695
const diagnostics = aggregateLines([

‎apps/server/src/diagnostics/TraceDiagnostics.ts‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ import * as Result from "effect/Result";
1818
import * as Schema from "effect/Schema";
1919
import * as Stream from "effect/Stream";
2020

21-
interface TraceRecordLike {
22-
readonly name?: unknown;
23-
readonly traceId?: unknown;
24-
readonly spanId?: unknown;
25-
readonly startTimeUnixNano?: unknown;
26-
readonly endTimeUnixNano?: unknown;
27-
readonly durationMs?: unknown;
28-
readonly exit?: unknown;
29-
readonly events?: unknown;
30-
}
31-
3221
interface TraceEventLike {
3322
readonly name?: unknown;
3423
readonly timeUnixNano?: unknown;
@@ -85,7 +74,7 @@ export function toRotatedTracePaths(
8574
return [...backups, traceFilePath];
8675
}
8776

88-
function isRecordObject(value: unknown): value is TraceRecordLike {
77+
function isRecordObject(value: unknown): value is Readonly<Record<string, unknown>> {
8978
return typeof value === "object" && value !== null;
9079
}
9180

@@ -253,8 +242,13 @@ export function makeTraceDiagnosticsAggregator(
253242
lastSpanAt === null || DateTime.isGreaterThan(endedAt, lastSpanAt) ? endedAt : lastSpanAt;
254243

255244
const exitTag = readExitTag(parsed.exit);
256-
const isFailure = exitTag === "Failure";
257-
const isInterrupted = exitTag === "Interrupted";
245+
const status = isRecordObject(parsed.status) ? parsed.status : {};
246+
const attributes = isRecordObject(parsed.attributes) ? parsed.attributes : {};
247+
const isFailure = exitTag === "Failure" || status.code === "2";
248+
const isInterrupted =
249+
exitTag === "Interrupted" ||
250+
attributes["effect.fiber.interrupted"] === true ||
251+
status.message === "Interrupted";
258252
if (isFailure) failureCount += 1;
259253
if (isInterrupted) interruptionCount += 1;
260254

@@ -277,7 +271,10 @@ export function makeTraceDiagnosticsAggregator(
277271
insertBounded(slowestSpans, spanItem, TOP_LIMIT, slowestFirst);
278272

279273
if (isFailure) {
280-
const cause = readExitCause(parsed.exit);
274+
const cause =
275+
exitTag === "Failure"
276+
? readExitCause(parsed.exit)
277+
: (toStringValue(status.message)?.trim() ?? "Failure");
281278
insertBounded(latestFailures, { ...spanItem, cause }, RECENT_LIMIT, latestEndedFirst);
282279

283280
const failureKey = `${name}\0${cause}`;

0 commit comments

Comments
 (0)