fix(admin): split uncoded errors by crash site, label signed-in visitors, scope external calls

- Errors without a code are grouped by their first app stack frame (or the
  logging module), so unrelated TypeErrors no longer share one issue.
- "visitor" is a signed-in user with no role on the repository; it is no
  longer shown as "not signed in". Anonymous viewers are counted apart.
- The issue page no longer offers "Open user" next to "Open repository":
  users and repositories are counted from different records.
- With a service, repository or user filter, failed external calls are looked
  up by the faults' request IDs instead of the newest 2000 calls overall.
- The page says when the 2000-failure cap was reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-29 16:35:34 +00:00
1 parent d3860f29ad
commit fdd8f58944
7 files changed
+74 -22

No files matched your search

+22 -8
View File
@@ -45,7 +45,7 @@ const NETWORK_CODES = new Set(["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "ENOTF
const IGNORED = new Set(["process_heartbeat", "queue_heartbeat", "process_started", "job_started", "job_completed", "deployment"]);
// Request envelopes repeat a failure that a more specific record of the same request describes.
const ENVELOPES = new Set(["edge_request", "request_completed"]);
const SIGNED_IN = new Set(["owner", "coauthor", "admin"]);
const ACCOUNT_ROLES = new Set(["owner", "coauthor", "admin"]);
export const statusOf = (row: Row) => Number(row.status || row.httpStatus) || 0;
const codeOf = (row: Row) => String(row.errorCode || row.errorType || (row.event === "edge_request" ? `edge_${statusOf(row) || "5xx"}` : row.event) || "unknown");
@@ -64,18 +64,30 @@ export function recordKind(row: Row): RecordKind {
return "issue";
}
/**
* Without an error code, the code or type alone says little: every TypeError in
* the API would share one issue. The first app stack frame (or the module that
* logged it) tells those crash sites apart.
*/
function uncodedSite(row: Row) {
if (row.errorCode || row.event === "edge_request") return "";
return firstFrame(row.errorStack) || String(row.module || "");
}
export function issueTitle(row: Row) {
const code = codeOf(row);
if (TITLES[code]) return TITLES[code];
if (row.event === "edge_request") return `Edge proxy returned ${statusOf(row) || "5xx"}`;
if (!row.errorCode && row.errorType) return `Code crashed: ${row.errorType}`;
if (row.event === "job_failed" && !row.errorCode) return "Background job failed";
return code.replace(/_/g, " ").replace(/^./, c => c.toUpperCase());
const site = uncodedSite(row);
const at = site ? ` in ${site}` : "";
if (!row.errorCode && row.errorType) return `Code crashed: ${row.errorType}${at}`;
if (row.event === "job_failed" && !row.errorCode) return `Background job failed${at}`;
return code.replace(/_/g, " ").replace(/^./, c => c.toUpperCase()) + at;
}
export function issueId(row: Row) {
// One cause (a GitHub limit, an outage) hits many routes: routes are listed inside the issue.
return createHash("sha256").update(JSON.stringify([codeOf(row), String(row.service || "")])).digest("hex").slice(0, 16);
return createHash("sha256").update(JSON.stringify([codeOf(row), String(row.service || ""), uncodedSite(row)])).digest("hex").slice(0, 16);
}
const time = (row: Row) => Date.parse(String(row.timestamp));
@@ -84,7 +96,8 @@ const seconds = (ms: number) => ms >= 1000 ? `${(ms / 1000).toFixed(1).replace(/
const plural = (n: number, word: string) => `${n.toLocaleString("en-US")} ${word}${n === 1 ? "" : "s"}`;
const hostLabel = (host: string) => host === "storage" ? "Storage" : /github/.test(host) ? "GitHub" : host === "other" ? "An external service" : host;
function firstFrame(stack: unknown) {
const line = String(stack || "").split("\n").find(value => /\(?[\w./-]+\.[jt]s:\d+/.test(value));
// The first frame in our code: dependencies and Node internals say little about the crash site.
const line = String(stack || "").split("\n").find(value => /\(?[\w./-]+\.[jt]s:\d+/.test(value) && !/node_modules|node:/.test(value));
const match = line?.match(/([\w-]+\.[jt]s):(\d+)/);
return match ? `${match[1]}:${match[2]}` : "";
}
@@ -207,8 +220,9 @@ export function buildIssues(rows: Row[], options: IssueOptions) {
const jobs = new Set(sorted.map(row => row.jobId).filter(validTraceId));
const repos = countBy(sorted, "repoId", validProjectId);
const users = countBy(sorted, "userId", validUserId);
const viewers = { signedIn: 0, visitors: 0, notRecorded: 0 };
for (const row of sorted) viewers[SIGNED_IN.has(String(row.viewer)) ? "signedIn" : row.viewer ? "visitors" : "notRecorded"]++;
// "visitor" is signed in without a role on the repository; "anonymous" has no session.
const viewers = { signedIn: 0, visitors: 0, anonymous: 0, notRecorded: 0 };
for (const row of sorted) viewers[ACCOUNT_ROLES.has(String(row.viewer)) ? "signedIn" : row.viewer === "visitor" ? "visitors" : row.viewer === "anonymous" ? "anonymous" : "notRecorded"]++;
const routeCounts = new Map<string, { route: string; method: string; count: number; last: string }>();
for (const row of sorted) {
const route = String(row.route || (row.queue ? `job ${row.queue}` : ""));
+15 -3
View File
@@ -232,15 +232,27 @@ export async function issueRecords(options: IssueSource) {
try {
const scope = `${range(o)}${o.service ? ` service:${o.service}` : ""}${identity(o)}`;
const noise = "(event:request_slow OR event:request_cancelled)";
const [faults, expected, external, counts] = await Promise.all([
const failedCalls = `${range(o)} event:external_request (upstreamStatus:>=400 OR errorCode:*)`;
const filtered = Boolean(o.service || o.repoId || o.userId);
const [faults, expected, unscoped, counts] = await Promise.all([
rows(base, `${scope} ${FAULTS} NOT ${noise} NOT event:external_request | sort by (_time) desc`, 2001, controller.signal),
rows(base, `${scope} severity:info errorCode:* NOT event:external_request | sort by (_time) desc`, 501, controller.signal),
rows(base, `${range(o)} event:external_request (upstreamStatus:>=400 OR errorCode:*) | sort by (_time) desc`, 2001, controller.signal).catch(() => ({ records: [], partial: true })),
filtered ? Promise.resolve(null) : rows(base, `${failedCalls} | sort by (_time) desc`, 2001, controller.signal).catch(() => ({ records: [], partial: true })),
noiseCounts(base, `${scope} ${noise} | stats by (event) count() n`, controller.signal).catch(() => null),
]);
// With a filter, the newest 2000 failed calls overall can miss the ones behind
// the filtered faults: external calls carry no user and often no repository,
// so look them up by the faults' request IDs instead.
let external = unscoped;
if (!external) {
const ids = [...new Set(faults.records.map(row => row.requestId).filter(validTraceId))].slice(0, 200);
external = ids.length
? await rows(base, `${failedCalls} requestId:in(${ids.map(id => `"${id}"`).join(",")}) | sort by (_time) desc`, 2001, controller.signal).catch(() => ({ records: [], partial: true }))
: { records: [], partial: false };
}
const records = [...faults.records.slice(0, 2000), ...expected.records.slice(0, 500)];
return { ...common, state: faults.partial || expected.partial ? "partial" : records.length ? "ready" : "empty", records, external: external.records.slice(0, 2000),
externalPartial: external.partial, noise: counts, captured: records.length, limit: 2500 };
externalPartial: external.partial, noise: counts, captured: records.length, limit: 2500, faultLimit: 2000, faultLimitReached: faults.records.length >= 2000 };
} catch { return { ...common, state: "unreachable", records: [], external: [], noise: null }; }
finally { controller.abort(); clearTimeout(timer); active--; }
}
+1 -1
View File
@@ -161,7 +161,7 @@ router.get("/errors/issues", async (req, res) => {
source: useCentral ? "central" : "history", state: useCentral ? central.state : history!.state,
centralState: central?.state || "skipped", grafanaUrl: central?.grafanaUrl || null,
window: query.window, start: query.start, end: query.end, filters: { service: query.service, repoId: query.repoId, userId: query.userId },
captured: rows.length, redisCollecting: process.env.LOG_REDIS_MIRROR !== "off", ...built, names,
captured: rows.length, faultLimitReached: Boolean(useCentral && "faultLimitReached" in central && central.faultLimitReached), redisCollecting: process.env.LOG_REDIS_MIRROR !== "off", ...built, names,
});
});