Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { resetWebMetricsForTests, getWebMetrics } from "./registry.js";
|
||||
import { recordAiTokensConsumed, resetAiCostBucketsForTests } from "./ai-cost.js";
|
||||
|
||||
beforeEach(() => {
|
||||
resetWebMetricsForTests();
|
||||
resetAiCostBucketsForTests();
|
||||
});
|
||||
|
||||
describe("recordAiTokensConsumed", () => {
|
||||
it("increments the counter with provider / model / tenant_bucket labels", async () => {
|
||||
recordAiTokensConsumed({ provider: "openai", model: "gpt-4o", tenantId: "tenant-1", tokens: 100 });
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).toMatch(/ai_tokens_consumed_total\{[^}]*provider="openai"[^}]*model="gpt-4o"[^}]*tenant_bucket="tenant-1"[^}]*\} 100/);
|
||||
});
|
||||
|
||||
it("groups anonymous calls under tenant_bucket=anonymous", async () => {
|
||||
recordAiTokensConsumed({ provider: "scaleway", model: "mistral", tenantId: null, tokens: 50 });
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).toMatch(/ai_tokens_consumed_total\{[^}]*tenant_bucket="anonymous"[^}]*\} 50/);
|
||||
});
|
||||
|
||||
it("buckets new tenants past the cap into tenant_bucket=other", async () => {
|
||||
for (let i = 0; i < 100; i += 1) {
|
||||
recordAiTokensConsumed({ provider: "openai", model: "gpt-4", tenantId: `t-${i}`, tokens: 1 });
|
||||
}
|
||||
recordAiTokensConsumed({ provider: "openai", model: "gpt-4", tenantId: "t-overflow", tokens: 25 });
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).toMatch(/ai_tokens_consumed_total\{[^}]*tenant_bucket="other"[^}]*\} 25/);
|
||||
expect(text).not.toMatch(/tenant_bucket="t-overflow"/);
|
||||
});
|
||||
|
||||
it("ignores zero / negative / NaN token counts", async () => {
|
||||
recordAiTokensConsumed({ provider: "openai", model: "gpt-4", tenantId: "t1", tokens: 0 });
|
||||
recordAiTokensConsumed({ provider: "openai", model: "gpt-4", tenantId: "t1", tokens: -5 });
|
||||
recordAiTokensConsumed({ provider: "openai", model: "gpt-4", tenantId: "t1", tokens: Number.NaN });
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).not.toMatch(/ai_tokens_consumed_total\{/);
|
||||
});
|
||||
|
||||
it("sanitises control characters in label values", async () => {
|
||||
recordAiTokensConsumed({ provider: "openai", model: "gpt-4\nfoo", tenantId: "t1\tx", tokens: 7 });
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).toMatch(/ai_tokens_consumed_total\{[^}]*provider="openai"[^}]*model="gpt-4foo"[^}]*tenant_bucket="t1x"[^}]*\} 7/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* AI-token-cost metric hook with cardinality discipline.
|
||||
*
|
||||
* Calling `recordAiTokensConsumed({ provider, model, tenantId, tokens })`
|
||||
* after every successful AI call increments
|
||||
* `ai_tokens_consumed_total{provider, model, tenant_bucket}`.
|
||||
*
|
||||
* Cardinality strategy (deviation from spec acceptable per detail-MD §10):
|
||||
*
|
||||
* - The label is **tenant_bucket**, NOT tenant_id. The bucket is an opaque
|
||||
* string. Today: the literal tenant id when known, "anonymous" otherwise.
|
||||
* - A cap (`MAX_TENANT_BUCKETS`, default 100) keeps the unique label values
|
||||
* bounded. The first 100 distinct tenant ids observed become buckets
|
||||
* one-to-one; subsequent tenants are folded into the synthetic `"other"`
|
||||
* bucket. The bucket-set lives in a process-local LRU map and resets on
|
||||
* process restart. This matches the detail-MD acceptance criterion
|
||||
* "tenant_id-Bucketing dokumentiert" and prevents the documented
|
||||
* cardinality-explosion failure mode (a malicious user spamming new
|
||||
* tenant ids).
|
||||
* - When operators want raw per-tenant figures, they query the DB
|
||||
* aggregate (already populated by `createCredit`) — Prometheus is for
|
||||
* real-time, the DB is for accounting.
|
||||
*/
|
||||
import { getWebMetrics } from "./registry.js";
|
||||
|
||||
const MAX_TENANT_BUCKETS = 100;
|
||||
const OTHER_BUCKET = "other";
|
||||
const ANONYMOUS_BUCKET = "anonymous";
|
||||
|
||||
const trackedTenantBuckets = new Set<string>();
|
||||
|
||||
export type RecordAiTokensConsumedInput = {
|
||||
provider: string;
|
||||
model: string;
|
||||
tenantId: string | null | undefined;
|
||||
tokens: number;
|
||||
};
|
||||
|
||||
export function recordAiTokensConsumed(input: RecordAiTokensConsumedInput): void {
|
||||
const tokens = Number.isFinite(input.tokens) && input.tokens > 0 ? Math.round(input.tokens) : 0;
|
||||
if (tokens <= 0) {
|
||||
return;
|
||||
}
|
||||
const bucket = resolveTenantBucket(input.tenantId);
|
||||
try {
|
||||
getWebMetrics().metrics.aiTokensConsumedTotal
|
||||
.labels({ provider: sanitizeLabel(input.provider), model: sanitizeLabel(input.model), tenant_bucket: bucket })
|
||||
.inc(tokens);
|
||||
} catch {
|
||||
// Metric writes never fail business code.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test-only — clears the tenant-bucket LRU so each test sees a clean slate.
|
||||
*/
|
||||
export function resetAiCostBucketsForTests(): void {
|
||||
trackedTenantBuckets.clear();
|
||||
}
|
||||
|
||||
function resolveTenantBucket(tenantId: string | null | undefined): string {
|
||||
if (!tenantId) {
|
||||
return ANONYMOUS_BUCKET;
|
||||
}
|
||||
const sanitised = sanitizeLabel(tenantId);
|
||||
if (trackedTenantBuckets.has(sanitised)) {
|
||||
return sanitised;
|
||||
}
|
||||
if (trackedTenantBuckets.size < MAX_TENANT_BUCKETS) {
|
||||
trackedTenantBuckets.add(sanitised);
|
||||
return sanitised;
|
||||
}
|
||||
return OTHER_BUCKET;
|
||||
}
|
||||
|
||||
function sanitizeLabel(value: string): string {
|
||||
// Prom label values are arbitrary UTF-8 but must not exceed reasonable
|
||||
// bounds. Truncate to 128 chars and drop control characters.
|
||||
const cleaned = value.replace(/[\x00-\x1f\x7f]/g, "").slice(0, 128);
|
||||
return cleaned || "unknown";
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Web (SSR) service-scoped Prometheus registry singleton.
|
||||
*
|
||||
* The web process serves both UI HTML and a couple of resource routes —
|
||||
* `/api/status` and `/api/metrics` — so it gets its own service-tagged
|
||||
* registry. Mirrors apps/api/src/metrics.ts.
|
||||
*/
|
||||
import { createDefaultMetrics, createMetricsRegistry, type DefaultMetrics, type MetricsRegistry } from "@toolctrl/metrics";
|
||||
import { setEnqueueObserver } from "@toolctrl/queue";
|
||||
|
||||
type WebMetricsBundle = {
|
||||
registry: MetricsRegistry;
|
||||
metrics: DefaultMetrics;
|
||||
};
|
||||
|
||||
let cached: WebMetricsBundle | undefined;
|
||||
|
||||
export function getWebMetrics(): WebMetricsBundle {
|
||||
if (!cached) {
|
||||
cached = buildWebMetrics();
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
||||
export function resetWebMetricsForTests(): void {
|
||||
cached = undefined;
|
||||
}
|
||||
|
||||
function buildWebMetrics(): WebMetricsBundle {
|
||||
const revision = process.env.APP_REVISION?.trim() ?? "";
|
||||
const registry = createMetricsRegistry({
|
||||
serviceName: "web",
|
||||
collectDefaultProcessMetrics: true,
|
||||
...(revision ? { defaultLabels: { revision } } : {}),
|
||||
});
|
||||
const metrics = createDefaultMetrics(registry);
|
||||
// Wire BullMQ enqueue events into queue_jobs_enqueued_total. See
|
||||
// apps/api/src/metrics.ts for the same wiring.
|
||||
setEnqueueObserver((event) => {
|
||||
metrics.queueJobsEnqueuedTotal.labels({ queue: event.queueName, priority: event.priorityLabel }).inc();
|
||||
});
|
||||
return { registry, metrics };
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { resetWebMetricsForTests, getWebMetrics } from "./registry.js";
|
||||
import { withMetrics } from "./ssr.js";
|
||||
|
||||
beforeEach(() => {
|
||||
resetWebMetricsForTests();
|
||||
});
|
||||
|
||||
describe("withMetrics", () => {
|
||||
it("passes through and is a no-op when metrics are disabled", async () => {
|
||||
const loader = withMetrics("root", async () => "value", { enabledOverride: false });
|
||||
expect(await loader({})).toBe("value");
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).not.toContain('web_ssr_loader_duration_seconds_count{route="root"');
|
||||
});
|
||||
|
||||
it("observes loader latency with route + status=ok", async () => {
|
||||
let t = 0;
|
||||
const loader = withMetrics(
|
||||
"dashboard",
|
||||
async () => ({ ok: true }),
|
||||
{
|
||||
enabledOverride: true,
|
||||
now: () => {
|
||||
const next = t;
|
||||
t += 80; // 80ms — should hit the 0.1 bucket
|
||||
return next;
|
||||
},
|
||||
},
|
||||
);
|
||||
await loader({});
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).toMatch(/web_ssr_loader_duration_seconds_count\{[^}]*route="dashboard"[^}]*status="ok"[^}]*\} 1/);
|
||||
expect(text).toMatch(/web_ssr_loader_duration_seconds_bucket\{le="0.1",[^}]*route="dashboard"[^}]*status="ok"[^}]*\} 1/);
|
||||
});
|
||||
|
||||
it("labels redirect responses as status=redirect", async () => {
|
||||
const loader = withMetrics(
|
||||
"login",
|
||||
async () => new Response(null, { status: 302, headers: { location: "/dashboard" } }),
|
||||
{ enabledOverride: true },
|
||||
);
|
||||
await loader({});
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).toMatch(/web_ssr_loader_duration_seconds_count\{[^}]*route="login"[^}]*status="redirect"[^}]*\} 1/);
|
||||
});
|
||||
|
||||
it("labels thrown loaders as status=error and re-throws", async () => {
|
||||
const loader = withMetrics(
|
||||
"fail",
|
||||
async () => {
|
||||
throw new Error("boom");
|
||||
},
|
||||
{ enabledOverride: true },
|
||||
);
|
||||
await expect(loader({})).rejects.toThrow("boom");
|
||||
const text = await getWebMetrics().registry.metrics();
|
||||
expect(text).toMatch(/web_ssr_loader_duration_seconds_count\{[^}]*route="fail"[^}]*status="error"[^}]*\} 1/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* `withMetrics(routeId, loader)` — wraps a React Router v7 SSR loader so each
|
||||
* invocation observes `web_ssr_loader_duration_seconds_bucket{route, status}`.
|
||||
*
|
||||
* Apply it to the **highest-traffic** loaders only (root, dashboard, login,
|
||||
* tenant index, etc.) — wrapping every loader would multiply boilerplate
|
||||
* without paying off in extra signal. Operators can extend coverage later if
|
||||
* we identify hotspots.
|
||||
*
|
||||
* Self-noop when `METRICS_ENABLED=false` so dev runs stay zero-overhead.
|
||||
*/
|
||||
import { isMetricsEnabled } from "@toolctrl/metrics";
|
||||
import { getWebMetrics } from "./registry.js";
|
||||
|
||||
type AnyLoaderArgs = {
|
||||
request?: Request;
|
||||
params?: Record<string, string | undefined>;
|
||||
context?: unknown;
|
||||
};
|
||||
|
||||
export type SsrLoader<TArgs extends AnyLoaderArgs = AnyLoaderArgs, TResult = unknown> = (args: TArgs) => Promise<TResult> | TResult;
|
||||
|
||||
export type WithMetricsOptions = {
|
||||
/** Override the time source — tests inject a deterministic clock. */
|
||||
now?: () => number;
|
||||
/** Override the `METRICS_ENABLED` check — tests force enabled=true. */
|
||||
enabledOverride?: boolean;
|
||||
};
|
||||
|
||||
export function withMetrics<TArgs extends AnyLoaderArgs, TResult>(
|
||||
routeId: string,
|
||||
loader: SsrLoader<TArgs, TResult>,
|
||||
options: WithMetricsOptions = {},
|
||||
): SsrLoader<TArgs, TResult> {
|
||||
return async function wrappedLoader(args: TArgs): Promise<TResult> {
|
||||
const enabled = options.enabledOverride ?? isMetricsEnabled(process.env);
|
||||
if (!enabled) {
|
||||
return loader(args);
|
||||
}
|
||||
const now = options.now ?? (() => performance.now());
|
||||
const started = now();
|
||||
let status: "ok" | "error" | "redirect" = "ok";
|
||||
try {
|
||||
const result = await loader(args);
|
||||
status = classifyStatus(result);
|
||||
return result;
|
||||
} catch (error) {
|
||||
status = "error";
|
||||
throw error;
|
||||
} finally {
|
||||
const elapsedSeconds = (now() - started) / 1000;
|
||||
try {
|
||||
getWebMetrics().metrics.webSsrLoaderDurationSeconds.labels({ route: routeId, status }).observe(elapsedSeconds);
|
||||
} catch {
|
||||
// Metric writes never break loader execution.
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Inspects the loader return value for a Response that signals redirect /
|
||||
* client error. React Router treats thrown Response objects as the loader's
|
||||
* error path (handled in catch block), so anything returned here counts as
|
||||
* "ok" unless the status is a redirect.
|
||||
*/
|
||||
function classifyStatus(result: unknown): "ok" | "redirect" {
|
||||
if (result instanceof Response) {
|
||||
if (result.status >= 300 && result.status < 400) {
|
||||
return "redirect";
|
||||
}
|
||||
}
|
||||
return "ok";
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { reportWebVitals } from "./web-vitals";
|
||||
|
||||
describe("reportWebVitals", () => {
|
||||
it("registers handlers for CLS/LCP/INP/TTFB/FCP", async () => {
|
||||
const reporter = vi.fn();
|
||||
const onCLS = vi.fn();
|
||||
const onLCP = vi.fn();
|
||||
const onINP = vi.fn();
|
||||
const onTTFB = vi.fn();
|
||||
const onFCP = vi.fn();
|
||||
await reportWebVitals({
|
||||
reporter,
|
||||
loader: async () => ({ onCLS, onLCP, onINP, onTTFB, onFCP }) as unknown as typeof import("web-vitals"),
|
||||
});
|
||||
expect(onCLS).toHaveBeenCalledWith(reporter);
|
||||
expect(onLCP).toHaveBeenCalledWith(reporter);
|
||||
expect(onINP).toHaveBeenCalledWith(reporter);
|
||||
expect(onTTFB).toHaveBeenCalledWith(reporter);
|
||||
expect(onFCP).toHaveBeenCalledWith(reporter);
|
||||
});
|
||||
|
||||
it("silently no-ops when the library throws", async () => {
|
||||
await expect(
|
||||
reportWebVitals({
|
||||
reporter: vi.fn(),
|
||||
loader: async () => {
|
||||
throw new Error("nope");
|
||||
},
|
||||
})
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("delivers reported metric values via the reporter callback", async () => {
|
||||
const reporter = vi.fn();
|
||||
type CB = (m: { name: string; value: number; rating: string; delta: number; id: string; navigationType: string }) => void;
|
||||
let captured: CB | null = null;
|
||||
await reportWebVitals({
|
||||
reporter,
|
||||
loader: async () =>
|
||||
({
|
||||
onCLS: (cb: CB) => {
|
||||
captured = cb;
|
||||
},
|
||||
onLCP: () => {},
|
||||
onINP: () => {},
|
||||
onTTFB: () => {},
|
||||
}) as unknown as typeof import("web-vitals"),
|
||||
});
|
||||
captured?.({
|
||||
name: "CLS",
|
||||
value: 0.05,
|
||||
rating: "good",
|
||||
delta: 0.05,
|
||||
id: "v1-cls",
|
||||
navigationType: "navigate",
|
||||
});
|
||||
expect(reporter).toHaveBeenCalledTimes(1);
|
||||
expect(reporter.mock.calls[0]?.[0]).toMatchObject({ name: "CLS", value: 0.05 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Real-User-Monitoring (RUM) bridge for Core Web Vitals → Sentry.
|
||||
*
|
||||
* The browser's web-vitals library reports CLS / FID / LCP / INP / TTFB
|
||||
* once per page load (or per change for CLS). We forward those values to
|
||||
* Sentry via `captureMessage` with a structured tag-set so the metrics
|
||||
* surface as searchable events. The dashboard query is documented in
|
||||
* docs/architecture/lighthouse.md.
|
||||
*
|
||||
* Spec: docs/improvement-plan/10-compliance-polish/lighthouse.md (Step (f)).
|
||||
*/
|
||||
import type { Metric } from "web-vitals";
|
||||
|
||||
/**
|
||||
* Reporter signature is exposed for tests + the runtime injection. The
|
||||
* default reporter forwards to Sentry; tests pass a vi.fn() spy.
|
||||
*/
|
||||
export type WebVitalsReporter = (metric: Metric) => void;
|
||||
|
||||
/**
|
||||
* Sends a web-vitals Metric to the Sentry sink. We keep the integration
|
||||
* lazy and tolerant — when Sentry isn't initialised the call is a no-op.
|
||||
*/
|
||||
export const sentryReporter: WebVitalsReporter = (metric) => {
|
||||
void (async () => {
|
||||
try {
|
||||
const Sentry = await import("@sentry/react-router");
|
||||
Sentry.captureMessage(`web-vitals.${metric.name}`, {
|
||||
level: "info",
|
||||
tags: {
|
||||
"web.vital.name": metric.name,
|
||||
"web.vital.rating": metric.rating,
|
||||
},
|
||||
extra: {
|
||||
value: metric.value,
|
||||
delta: metric.delta,
|
||||
id: metric.id,
|
||||
navigationType: metric.navigationType,
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
// Sentry not loaded or import failed — silent drop.
|
||||
}
|
||||
})();
|
||||
};
|
||||
|
||||
export interface ReportWebVitalsOptions {
|
||||
reporter?: WebVitalsReporter;
|
||||
/**
|
||||
* Allows injecting the web-vitals module (e.g. a mock) in tests.
|
||||
* Production callers should let this default to dynamic import.
|
||||
*/
|
||||
loader?: () => Promise<typeof import("web-vitals")>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wires the web-vitals library callbacks. Safe to call on SSR (it bails
|
||||
* out when `window` is absent). Returns a promise that resolves once the
|
||||
* underlying library has been registered, useful for tests.
|
||||
*/
|
||||
export async function reportWebVitals(options: ReportWebVitalsOptions = {}): Promise<void> {
|
||||
if (typeof window === "undefined") return;
|
||||
|
||||
const reporter = options.reporter ?? sentryReporter;
|
||||
const loader = options.loader ?? (() => import("web-vitals"));
|
||||
|
||||
try {
|
||||
const mod = await loader();
|
||||
mod.onCLS(reporter);
|
||||
mod.onLCP(reporter);
|
||||
mod.onINP(reporter);
|
||||
mod.onTTFB(reporter);
|
||||
if (typeof mod.onFCP === "function") {
|
||||
mod.onFCP(reporter);
|
||||
}
|
||||
} catch {
|
||||
// Library missing or load error — never break the page.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { registerServiceWorker } from "./registerServiceWorker";
|
||||
|
||||
describe("registerServiceWorker", () => {
|
||||
it("returns null when navigator has no service worker", async () => {
|
||||
const result = await registerServiceWorker({ navigatorRef: {} as Navigator });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null in dev mode without calling register", async () => {
|
||||
const register = vi.fn();
|
||||
const navigatorRef = { serviceWorker: { register } } as unknown as Navigator;
|
||||
const result = await registerServiceWorker({ navigatorRef, isDev: true });
|
||||
expect(result).toBeNull();
|
||||
expect(register).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("registers /sw.js by default", async () => {
|
||||
const fakeRegistration = { scope: "/" } as ServiceWorkerRegistration;
|
||||
const register = vi.fn().mockResolvedValue(fakeRegistration);
|
||||
const navigatorRef = { serviceWorker: { register } } as unknown as Navigator;
|
||||
const result = await registerServiceWorker({ navigatorRef });
|
||||
expect(register).toHaveBeenCalledWith("/sw.js", { scope: "/" });
|
||||
expect(result).toBe(fakeRegistration);
|
||||
});
|
||||
|
||||
it("swallows registration errors and returns null", async () => {
|
||||
const register = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const navigatorRef = { serviceWorker: { register } } as unknown as Navigator;
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const result = await registerServiceWorker({ navigatorRef });
|
||||
expect(result).toBeNull();
|
||||
expect(warn).toHaveBeenCalled();
|
||||
warn.mockRestore();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Service-worker registration helper used by `root.tsx`. Kept in its own
|
||||
* module so it can be unit-tested without rendering a React tree.
|
||||
*
|
||||
* The function is a no-op when:
|
||||
* - `navigator.serviceWorker` is unavailable (SSR, older browsers).
|
||||
* - `window.location.protocol` is not http(s) — file:// previews would
|
||||
* otherwise throw SecurityError.
|
||||
* - `import.meta.env.DEV` is true — the dev server hot-reloads, and
|
||||
* a cached SW would shadow new bundles.
|
||||
*
|
||||
* Refs: docs/improvement-plan/10-compliance-polish/lighthouse.md (Step (d)).
|
||||
*/
|
||||
export type RegisterServiceWorkerOptions = {
|
||||
scriptUrl?: string;
|
||||
scope?: string;
|
||||
/** Inject an override container so tests can supply a fake navigator. */
|
||||
navigatorRef?: Navigator;
|
||||
/** Inject an override container so tests can disable DEV gating. */
|
||||
isDev?: boolean;
|
||||
};
|
||||
|
||||
export async function registerServiceWorker(options: RegisterServiceWorkerOptions = {}): Promise<ServiceWorkerRegistration | null> {
|
||||
const navigatorRef = options.navigatorRef ?? (typeof navigator === "undefined" ? undefined : navigator);
|
||||
if (!navigatorRef || !("serviceWorker" in navigatorRef)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (options.isDev) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return await navigatorRef.serviceWorker.register(options.scriptUrl ?? "/sw.js", {
|
||||
scope: options.scope ?? "/",
|
||||
});
|
||||
} catch (err) {
|
||||
// Swallow but log — a failed SW must never break the app render.
|
||||
if (typeof console !== "undefined") {
|
||||
console.warn("[pwa] service worker registration failed", err);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { render, waitFor } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("@sentry/react-router", () => {
|
||||
const sentryOnError = vi.fn();
|
||||
return {
|
||||
__esModule: true,
|
||||
sentryOnError,
|
||||
};
|
||||
});
|
||||
|
||||
import * as Sentry from "@sentry/react-router";
|
||||
|
||||
import { RouteTracingProvider, sentryHydrationErrorHandler } from "./routeTracing";
|
||||
|
||||
const mockedOnError = Sentry.sentryOnError as unknown as ReturnType<typeof vi.fn>;
|
||||
|
||||
describe("sentryHydrationErrorHandler", () => {
|
||||
it("delegates to Sentry.sentryOnError", async () => {
|
||||
const err = new Error("hydration-boom");
|
||||
const info = { errorInfo: { componentStack: "test-stack" } as React.ErrorInfo };
|
||||
sentryHydrationErrorHandler(err, info);
|
||||
await waitFor(() => expect(mockedOnError).toHaveBeenCalledWith(err, info));
|
||||
});
|
||||
|
||||
it("swallows exceptions thrown by the SDK", () => {
|
||||
mockedOnError.mockImplementationOnce(() => {
|
||||
throw new Error("sdk-down");
|
||||
});
|
||||
expect(() => sentryHydrationErrorHandler(new Error("boom"), {})).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("RouteTracingProvider", () => {
|
||||
it("renders its children unchanged", () => {
|
||||
const { getByText } = render(
|
||||
<RouteTracingProvider>
|
||||
<span>route-tracing-content</span>
|
||||
</RouteTracingProvider>
|
||||
);
|
||||
expect(getByText("route-tracing-content")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* React Router v7 Sentry tracing glue.
|
||||
*
|
||||
* `@sentry/react-router` v10 supports the React Router v7 Framework Mode
|
||||
* natively. The auto-instrumentation is set up by `reactRouterTracingIntegration()`
|
||||
* which is registered in `apps/web/app/sentry.client.ts` — no manual
|
||||
* `wrapCreateBrowserRouter` is required.
|
||||
*
|
||||
* This module exposes two thin helpers:
|
||||
*
|
||||
* - {@link sentryHydrationErrorHandler} — to be passed to `<HydratedRouter onError={...}>`.
|
||||
* Delegates to `Sentry.sentryOnError` when the SDK is loaded; falls
|
||||
* back to a no-op so non-Sentry builds stay green.
|
||||
* - {@link RouteTracingProvider} — a tiny provider that exists so the
|
||||
* manual fallback path is wired uniformly. Today it is a passthrough
|
||||
* because the official integration handles navigation spans
|
||||
* internally; the wrapping component gives us a stable seam for
|
||||
* bug fixes that need to capture extra navigation context without
|
||||
* editing `root.tsx` again.
|
||||
*
|
||||
* @see docs/improvement-plan/02-observability/sentry-wiring-web.md (Step 7)
|
||||
*/
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
export function sentryHydrationErrorHandler(error: unknown, info: { errorInfo?: React.ErrorInfo }): void {
|
||||
void import("@sentry/react-router")
|
||||
.then((Sentry) => {
|
||||
Sentry.sentryOnError(error, info);
|
||||
})
|
||||
.catch(() => {
|
||||
// If the SDK has not been initialized (DSN unset) or the export is
|
||||
// missing on older versions, fall back silently. The framework's
|
||||
// built-in default error handler still runs because React Router's
|
||||
// `onError` is purely additive — it does not replace the default
|
||||
// logging.
|
||||
});
|
||||
}
|
||||
|
||||
export interface RouteTracingProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Passthrough provider that documents the integration point. Wrapping
|
||||
* `<Outlet />` (or the entire app tree) in this component gives us a
|
||||
* stable seam to add navigation-context enrichment later without
|
||||
* needing to edit `root.tsx` again.
|
||||
*/
|
||||
export function RouteTracingProvider({ children }: RouteTracingProviderProps) {
|
||||
return <>{children}</>;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { type ClassValue, clsx } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export function removeAllNulls(resultObject: any | undefined | null) {
|
||||
if (!resultObject) return;
|
||||
|
||||
for (const key in resultObject) {
|
||||
if (resultObject[key] === null || resultObject[key] === undefined) {
|
||||
delete resultObject[key];
|
||||
} else if (typeof resultObject[key] === "object") {
|
||||
removeAllNulls(resultObject[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return resultObject;
|
||||
}
|
||||
Reference in new issue
Block a user