import { describe, expect, it, vi } from "vitest"; import { createTelemetryPipeline, DEFAULT_TELEMETRY_PIPELINE_BUDGET, type TelemetryBatchItem, type TelemetryPipelineBudget, } from "../../../src/platform/telemetry/telemetry-pipeline.ts"; import type { TelemetryEvent } from "../../../src/platform/telemetry/telemetry-event.ts"; const TEST_TELEMETRY_BUDGET: TelemetryPipelineBudget = { ...DEFAULT_TELEMETRY_PIPELINE_BUDGET, maxBufferedEvents: 50, maxSendsPerMinute: 10, }; const TEST_EVENT: TelemetryEvent = { kind: "catalog.load.failed", severity: "error", featureId: "catalog", payload: { reason: "REQUEST_TIMEOUT" }, }; const REPEATED_ERROR = TEST_EVENT; /** Runs scheduled work synchronously-ish and keeps timers in the test's hand. */ function manualScheduling() { const timers: Array<() => void> = []; return { schedule: (callback: () => void) => queueMicrotask(callback), setTimer: (callback: () => void) => { timers.push(callback); return timers.length; }, clearTimer: () => {}, runTimers() { for (const callback of timers.splice(0)) callback(); }, }; } describe("createTelemetryPipeline", () => { it("does not reject business flow when telemetry transport fails", async () => { const pipeline = createTelemetryPipeline({ transport: { async send() { throw new Error("collector unavailable"); }, }, budget: TEST_TELEMETRY_BUDGET, }); await expect(pipeline.emit(TEST_EVENT)).resolves.toBeUndefined(); await expect(pipeline.flush()).resolves.toBeUndefined(); expect(pipeline.stats().failedSends).toBe(1); }); it("does not throw when the transport throws synchronously", async () => { const pipeline = createTelemetryPipeline({ transport: { send() { throw new Error("sdk crashed"); }, }, budget: TEST_TELEMETRY_BUDGET, }); await expect(pipeline.emit(TEST_EVENT)).resolves.toBeUndefined(); await expect(pipeline.flush()).resolves.toBeUndefined(); }); it("bounds network sends during a repeated error storm", async () => { const send = vi.fn().mockResolvedValue(undefined); const pipeline = createTelemetryPipeline({ transport: { send }, budget: { ...TEST_TELEMETRY_BUDGET, maxSendsPerMinute: 10 }, }); for (let i = 0; i < 100_000; i += 1) { await pipeline.emit(REPEATED_ERROR); } await pipeline.flush(); expect(send.mock.calls.length).toBeLessThanOrEqual(10); expect(pipeline.stats().deduplicated).toBe(99_999); }); it("reports how many occurrences a delivered fingerprint collapsed", async () => { const batches: TelemetryBatchItem[][] = []; let now = 0; const pipeline = createTelemetryPipeline({ transport: { send: (batch) => void batches.push([...batch]) }, budget: TEST_TELEMETRY_BUDGET, now: () => now, schedule: () => {}, }); for (let i = 0; i < 25; i += 1) { now = i; await pipeline.emit(REPEATED_ERROR); } await pipeline.flush(); expect(batches.flat()).toHaveLength(1); expect(batches[0]?.[0]?.occurrences).toBe(25); }); it("delivers every duplicate as occurrences with the default microtask scheduler", async () => { const batches: TelemetryBatchItem[][] = []; const pipeline = createTelemetryPipeline({ transport: { send: (batch) => void batches.push([...batch]) }, budget: TEST_TELEMETRY_BUDGET, }); for (let i = 0; i < 1_000; i += 1) { await pipeline.emit(REPEATED_ERROR); } await pipeline.flush(); const items = batches.flat(); expect(items.reduce((sum, item) => sum + item.occurrences, 0)).toBe(1_000); expect(new Set(items.map((item) => item.fingerprint)).size).toBe(1); expect(batches.length).toBeLessThanOrEqual(2); pipeline.dispose(); }); it("reports held duplicates when the dedup window closes without another flush", async () => { const batches: TelemetryBatchItem[][] = []; const timers: Array void; ms: number }>> = []; const pipeline = createTelemetryPipeline({ transport: { send: (batch) => void batches.push([...batch]) }, budget: TEST_TELEMETRY_BUDGET, setTimer: (callback, ms) => timers.push({ callback, ms }), clearTimer: () => {}, }); for (let i = 0; i < 10; i += 1) { await pipeline.emit(REPEATED_ERROR); } expect(timers.map((timer) => timer.ms)).toEqual([TEST_TELEMETRY_BUDGET.dedupWindowMs]); for (const timer of timers.splice(0)) timer.callback(); await new Promise((resolve) => setTimeout(resolve, 0)); expect(batches.flat().reduce((sum, item) => sum + item.occurrences, 0)).toBe(10); pipeline.dispose(); }); it("does not let a rejected or evicted event suppress its fingerprint", async () => { const batches: TelemetryBatchItem[][] = []; const pipeline = createTelemetryPipeline({ transport: { send: (batch) => void batches.push([...batch]) }, budget: { ...TEST_TELEMETRY_BUDGET, maxBufferedEvents: 1 }, schedule: () => {}, }); const info: TelemetryEvent = { ...TEST_EVENT, kind: "catalog.load.slow", severity: "info" }; // Rejected on arrival: the buffer holds a more important event. await pipeline.emit(TEST_EVENT); await pipeline.emit(info); await pipeline.flush(); await pipeline.emit(info); expect(pipeline.stats()).toMatchObject({ buffered: 1, deduplicated: 0 }); await pipeline.flush(); // Evicted after admission: a more important event takes its slot. await pipeline.emit({ ...info, payload: { reason: "evicted" } }); await pipeline.emit({ ...TEST_EVENT, payload: { reason: "other" } }); await pipeline.flush(); await pipeline.emit({ ...info, payload: { reason: "evicted" } }); expect(pipeline.stats()).toMatchObject({ buffered: 1, deduplicated: 0 }); await pipeline.flush(); const kinds = batches.flat().map((item) => `${item.event.kind}:${item.occurrences}`); expect(kinds).toEqual([ "catalog.load.failed:1", "catalog.load.slow:1", "catalog.load.failed:1", "catalog.load.slow:1", ]); }); it("never reports a sampled-out event through a duplicate summary", async () => { const send = vi.fn(); const pipeline = createTelemetryPipeline({ transport: { send }, budget: { ...TEST_TELEMETRY_BUDGET, sampleRate: 0 }, }); for (let i = 0; i < 3; i += 1) { await pipeline.emit({ kind: "a.info", severity: "info" }); } await pipeline.flush(); expect(send).not.toHaveBeenCalled(); expect(pipeline.stats().sampledOut).toBe(3); pipeline.dispose(); }); it("rate-limits distinct events and resumes after the window reopens", async () => { let now = 0; const scheduling = manualScheduling(); const send = vi.fn().mockResolvedValue(undefined); const pipeline = createTelemetryPipeline({ transport: { send }, budget: { ...TEST_TELEMETRY_BUDGET, maxSendsPerMinute: 2, maxBatchEvents: 1, maxBufferedEvents: 100, }, now: () => now, ...scheduling, }); for (let i = 0; i < 5; i += 1) { await pipeline.emit({ ...TEST_EVENT, payload: { reason: `r${i}` } }); } await pipeline.flush(); expect(send).toHaveBeenCalledTimes(2); expect(pipeline.stats().buffered).toBe(3); now = 60_000; scheduling.runTimers(); await pipeline.flush(); expect(send).toHaveBeenCalledTimes(4); }); it("keeps memory bounded when events are all distinct and the collector is down", async () => { const pipeline = createTelemetryPipeline({ transport: { send: () => Promise.reject(new Error("down")) }, budget: { ...TEST_TELEMETRY_BUDGET, maxBufferedEvents: 20, maxFingerprints: 30 }, schedule: () => {}, }); for (let i = 0; i < 5_000; i += 1) { await pipeline.emit({ ...TEST_EVENT, payload: { reason: `distinct-${i}` } }); } const stats = pipeline.stats(); expect(stats.buffered).toBeLessThanOrEqual(20); expect(stats.peakBuffered).toBeLessThanOrEqual(20); expect(stats.fingerprints).toBeLessThanOrEqual(30); }); it("redacts before anything is buffered or sent", async () => { const batches: TelemetryBatchItem[][] = []; const pipeline = createTelemetryPipeline({ transport: { send: (batch) => void batches.push([...batch]) }, budget: TEST_TELEMETRY_BUDGET, }); await pipeline.emit({ ...TEST_EVENT, payload: { reason: "x", accessToken: "secret-token", headers: { cookie: "sid=1" } }, }); await pipeline.flush(); expect(JSON.stringify(batches)).not.toMatch(/secret-token|sid=1/); }); it("samples low-severity events deterministically but always keeps errors", async () => { const pipeline = createTelemetryPipeline({ transport: { send: () => {} }, budget: { ...TEST_TELEMETRY_BUDGET, sampleRate: 0 }, schedule: () => {}, }); await pipeline.emit({ kind: "a.info", severity: "info", interactionId: "i-1" }); await pipeline.emit({ kind: "a.error", severity: "error", interactionId: "i-1" }); expect(pipeline.stats()).toMatchObject({ sampledOut: 1, buffered: 1 }); }); it("drops invalid envelopes and stops admitting after dispose", async () => { const send = vi.fn(); const pipeline = createTelemetryPipeline({ transport: { send }, budget: TEST_TELEMETRY_BUDGET, }); await pipeline.emit({ kind: "NOT VALID", severity: "error" }); expect(pipeline.stats().invalid).toBe(1); pipeline.dispose(); await pipeline.emit(TEST_EVENT); await pipeline.flush(); expect(send).not.toHaveBeenCalled(); }); });