Files
HealthLog/src/lib/logging/sampler.ts
T
Marc-André BombeckandClaude Opus 4.7 02b99553b9 chore(style): repo-wide prettier sweep before v1.4.6 tag
Long-overdue prettier sweep; prettier had drifted vs. checked-in code
across ~130 files (mostly long-argument line-break wrapping in TS and
table-padding in markdown). Pure formatting — no semantic changes —
and required to clear the v1.4.6 release gate's `pnpm format:check`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 02:27:17 +02:00

31 lines
1023 B
TypeScript

import type { WideEvent } from "./types";
import { LOG_LEVEL_PRIORITY } from "./types";
import { getLoggingConfig, isLevelEnabled } from "./config";
/**
* Tail Sampling: Entscheidung ob ein Event emittiert werden soll.
*
* Immer behalten: Fehler, Warnungen, langsame Requests, Background-Tasks.
* Sampling nur fuer erfolgreiche, schnelle HTTP-Requests.
*/
export function shouldEmit(event: WideEvent): boolean {
if (!isLevelEnabled(event.level)) return false;
// Fehler und Warnungen immer behalten
if (LOG_LEVEL_PRIORITY[event.level] >= LOG_LEVEL_PRIORITY["warn"])
return true;
// Langsame Requests immer behalten
const config = getLoggingConfig();
if (event.duration_ms >= config.slowThresholdMs) return true;
// Hintergrundaufgaben immer behalten
if (event.kind === "background") return true;
// Fuer normale erfolgreiche Requests: Sampling anwenden
if (config.sampleRate >= 1.0) return true;
if (config.sampleRate <= 0.0) return false;
return Math.random() < config.sampleRate;
}