Files
HealthLog/src/lib/monitoring/glitchtip.ts
T
Marc-André Bombeck 9d18b8fd25 fix(admin): let the error-reporting badge follow reports, not a parseable URL
The badge read the enabled flag plus a DSN that parses. Neither says anything
about the target accepting a report: a wrong public key, a wrong project id and
a host that stopped answering all parse, and every one of them showed green
with a line naming the host. That is the false reassurance an operator finds
out about when they go looking for a crash that was never reported.

Nothing in the tree recorded an outcome, so there was nothing to read. The
sender now stamps one next to the DSN it used — instant on a success, a short
classification on a failure. Never the response body: that is text from a
remote host and has no business in this database or on this page.

The badge is green only while a report has left inside the last fortnight,
amber while a target is set and nothing has come back, and it names the reason
underneath. The clock lives with the ledger, so the verdict is resolved in the
route and the card paints what it is handed. A failure that a later success
superseded is not shown at all. Pressing Test records like any other send, so
the button proves the target on demand.
2026-09-10 10:28:57 +02:00

284 lines
7.5 KiB
TypeScript

import { safeFetch } from "@/lib/safe-fetch";
interface ParsedGlitchtipDsn {
dsn: string;
publicKey: string;
envelopeUrl: string;
storeUrl: string;
}
export interface GlitchtipEventPayloadInput {
environment: string;
message: string;
level: "error" | "warning" | "info";
type?: string;
stack?: string;
url?: string;
userAgent?: string;
sourceTag: string;
requestId?: string;
}
export interface GlitchtipDeliveryResult {
ok: boolean;
method?: "envelope" | "store-query" | "store-header";
status?: number;
details?: string;
}
function createEventId(): string {
const chars = "abcdef0123456789";
let out = "";
for (let i = 0; i < 32; i += 1) {
out += chars[Math.floor(Math.random() * chars.length)];
}
return out;
}
function parseGlitchtipDsn(dsn: string): ParsedGlitchtipDsn | null {
try {
const parsed = new URL(dsn);
const publicKey = decodeURIComponent(parsed.username || "").trim();
if (!publicKey) return null;
const pathParts = parsed.pathname.split("/").filter(Boolean);
if (pathParts.length === 0) return null;
const projectId = pathParts[pathParts.length - 1];
if (!projectId) return null;
const pathPrefix =
pathParts.length > 1 ? `/${pathParts.slice(0, -1).join("/")}` : "";
return {
dsn,
publicKey,
envelopeUrl: `${parsed.protocol}//${parsed.host}${pathPrefix}/api/${projectId}/envelope/`,
storeUrl: `${parsed.protocol}//${parsed.host}${pathPrefix}/api/${projectId}/store/`,
};
} catch {
return null;
}
}
function buildEventPayload(
eventId: string,
input: GlitchtipEventPayloadInput,
): Record<string, unknown> {
return {
event_id: eventId,
platform: "javascript",
level: input.level,
environment: input.environment,
timestamp: Date.now() / 1000,
message: input.message,
exception: {
values: [
{
type: input.type ?? "Error",
value: input.message,
stacktrace: input.stack
? {
frames: [
{
filename: input.url ?? "unknown",
function: "<anonymous>",
in_app: true,
module: "healthlog",
lineno: 1,
colno: 1,
},
],
}
: undefined,
},
],
},
request: input.url ? { url: input.url } : undefined,
tags: {
source: input.sourceTag,
...(input.requestId ? { request_id: input.requestId } : {}),
},
contexts: {
browser: input.userAgent ? { name: input.userAgent } : undefined,
},
};
}
function buildEnvelopeBody(
config: ParsedGlitchtipDsn,
eventId: string,
payload: Record<string, unknown>,
): string {
const envelopeHeader = JSON.stringify({
event_id: eventId,
sent_at: new Date().toISOString(),
dsn: config.dsn,
});
const itemHeader = JSON.stringify({ type: "event" });
const itemPayload = JSON.stringify(payload);
return `${envelopeHeader}\n${itemHeader}\n${itemPayload}`;
}
async function tryEnvelope(
config: ParsedGlitchtipDsn,
body: string,
): Promise<GlitchtipDeliveryResult> {
const response = await safeFetch(
config.envelopeUrl,
{
method: "POST",
headers: {
"content-type": "application/x-sentry-envelope",
},
body,
cache: "no-store",
},
// Operator-configured GlitchTip host carrying the redacted wide-event
// payload — pin the connect-time IP against DNS rebinding.
{ requirePublicHost: true },
);
if (response.ok)
return { ok: true, method: "envelope", status: response.status };
const details = await response.text().catch(() => "");
return {
ok: false,
method: "envelope",
status: response.status,
details: details || undefined,
};
}
async function tryStoreQuery(
config: ParsedGlitchtipDsn,
payload: Record<string, unknown>,
): Promise<GlitchtipDeliveryResult> {
const target = new URL(config.storeUrl);
target.searchParams.set("sentry_key", config.publicKey);
target.searchParams.set("sentry_version", "7");
target.searchParams.set("sentry_client", "healthlog/1.0");
const response = await safeFetch(
target,
{
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(payload),
cache: "no-store",
},
// Operator-configured GlitchTip host — pin the connect-time IP.
{ requirePublicHost: true },
);
if (response.ok)
return { ok: true, method: "store-query", status: response.status };
const details = await response.text().catch(() => "");
return {
ok: false,
method: "store-query",
status: response.status,
details: details || undefined,
};
}
async function tryStoreHeader(
config: ParsedGlitchtipDsn,
payload: Record<string, unknown>,
): Promise<GlitchtipDeliveryResult> {
const sentryAuth = `Sentry sentry_version=7, sentry_key=${config.publicKey}, sentry_client=healthlog/1.0`;
const response = await safeFetch(
config.storeUrl,
{
method: "POST",
headers: {
"content-type": "application/json",
"x-sentry-auth": sentryAuth,
},
body: JSON.stringify(payload),
cache: "no-store",
},
// Operator-configured GlitchTip host — pin the connect-time IP.
{ requirePublicHost: true },
);
if (response.ok)
return { ok: true, method: "store-header", status: response.status };
const details = await response.text().catch(() => "");
return {
ok: false,
method: "store-header",
status: response.status,
details: details || undefined,
};
}
/**
* Record what became of one send, in the one place every caller passes
* through. Dynamically imported so this module stays free of a database
* dependency at import time, and awaited so a test can observe it; the
* recorder itself never throws.
*/
async function record(result: GlitchtipDeliveryResult): Promise<void> {
try {
const { recordGlitchtipDelivery } =
await import("@/lib/monitoring/glitchtip-delivery");
await recordGlitchtipDelivery(result);
} catch {
// The report is the point; its bookkeeping is not worth an exception.
}
}
export async function sendGlitchtipEvent(params: {
dsn: string;
input: GlitchtipEventPayloadInput;
}): Promise<GlitchtipDeliveryResult> {
const config = parseGlitchtipDsn(params.dsn);
if (!config) {
const invalid: GlitchtipDeliveryResult = {
ok: false,
details: "invalid_dsn",
};
await record(invalid);
return invalid;
}
const eventId = createEventId();
const payload = buildEventPayload(eventId, params.input);
const envelopeBody = buildEnvelopeBody(config, eventId, payload);
const envelopeResult = await tryEnvelope(config, envelopeBody);
if (envelopeResult.ok) {
await record(envelopeResult);
return envelopeResult;
}
const storeQueryResult = await tryStoreQuery(config, payload);
if (storeQueryResult.ok) {
await record(storeQueryResult);
return storeQueryResult;
}
const storeHeaderResult = await tryStoreHeader(config, payload);
if (storeHeaderResult.ok) {
await record(storeHeaderResult);
return storeHeaderResult;
}
const failure: GlitchtipDeliveryResult = {
ok: false,
method: storeHeaderResult.method,
status:
storeHeaderResult.status ??
storeQueryResult.status ??
envelopeResult.status,
details:
storeHeaderResult.details ??
storeQueryResult.details ??
envelopeResult.details,
};
await record(failure);
return failure;
}