Files
webmail/lib/error-reporting/settings.ts
T
Linus Rath 481c4b6030 feat: serve Prometheus metrics and split health into live and ready
- `/api/metrics` in Prometheus text format, off until `METRICS_TOKEN`
  or `METRICS_TOKEN_FILE` is set (404 before, 401 without the bearer
  token): requests per route group with status class and latency,
  sign-in outcomes, calls to Stalwart, WOPI, translation, the plugin
  proxy and OAuth, browser errors, errors today, process memory,
  event-loop lag, uptime and build info. Labels come from fixed lists.
  Each replica serves its own.
- `/api/health/live` answers while the process runs;
  `/api/health/ready` checks the config, the shared store, writable
  data dirs and Stalwart's session resource. `/api/health` and
  `/api/ready` are unchanged.
- the Docker image gets a HEALTHCHECK on the live endpoint
- adds prom-client, and the OpenTelemetry packages the tracing step
  loads on demand
2026-11-01 02:59:40 +01:00

55 lines
2.0 KiB
TypeScript

import { configManager } from '@/lib/admin/config-manager';
import { parseDsn } from './envelope';
import { metricsEnabled } from '@/lib/metrics';
import type { ClientSettings } from './client';
/**
* The error-reporting settings as the server resolves them: admin config,
* then ERROR_REPORTING_DSN(_FILE) / ERROR_REPORTING_SAMPLE_RATE /
* ERROR_REPORTING_ENVIRONMENT, then off. Bulwark Lite reads the same three
* from policy.json (lib/error-reporting/lite.ts).
*/
export interface ErrorReportingSettings {
/** A valid DSN, or '' when none is set or the value doesn't parse. */
dsn: string;
sampleRate: number;
environment: string;
}
export function parseSampleRate(value: unknown): number {
const n = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN;
if (!Number.isFinite(n)) return 1;
return Math.min(1, Math.max(0, n));
}
export function parseEnvironment(value: unknown): string {
const text = typeof value === 'string' ? value.trim() : '';
return /^[\w.-]{1,64}$/.test(text) ? text : 'production';
}
export function errorReportingSettings(): ErrorReportingSettings {
const raw = configManager.get<string>('errorReportingDsn', '');
return {
dsn: parseDsn(raw) ? raw.trim() : '',
sampleRate: parseSampleRate(configManager.get<string>('errorReportingSampleRate', '1')),
environment: parseEnvironment(configManager.get<string>('errorReportingEnvironment', 'production')),
};
}
/**
* Whether browsers should post their errors to /api/errors: when there is a
* DSN to report to, a metrics endpoint that counts them, or consent to send
* error counts with the telemetry heartbeat.
*/
export async function shouldCollectErrors(): Promise<boolean> {
await configManager.ensureLoaded();
return !!errorReportingSettings().dsn || metricsEnabled();
}
/** What /api/config hands the browser. */
export async function browserErrorSettings(): Promise<ClientSettings> {
const settings = errorReportingSettings();
return { ...settings, collect: await shouldCollectErrors() };
}