fix(telemetry): send both telemetry ports through one collector transport (Q-6.9-4)

The application port (best-effort-telemetry) and the feature port
(feature-telemetry pipeline) each posted to TELEMETRY_ENDPOINT on their
own: 20+20 sends per minute, cookies on the application side, no
pagehide flush or dispose abort on the feature side, and no deadline on
any fetch, so a hung collector held activeFlush forever (6.3-10).

New src/adapters/telemetry/telemetry-collector.ts is the one transport:
shared send-rate window (createTelemetrySendRate, now in the platform
pipeline and injectable via `sendRate`), credentials "omit", cache
"no-store", a per-request deadline through the kernel
createAbortableOperation, and one pagehide fan-out. runtime-adapters
builds one collector and passes it to both ports. Body projections are
unchanged and pinned by characterization tests first.

Verified: new tests in tests/unit/telemetry.test.ts (5 RED before
wiring: credentials/cache, hung collector timeout, shared rate,
feature pagehide flush, feature dispose abort); all gates pass except
the known tests/unit/removal-fixture.test.ts (artifacts/ absent);
check:adapter-inventory 137/137; FE-RB-004 drill PASS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:58:35 +09:00
co-authored by Claude Opus 5.5
parent b35304dd48
commit a328776bf2
9 changed files with 528 additions and 98 deletions
+47 -15
View File
@@ -93,12 +93,53 @@ function assertPositive(name: string, value: number): void {
}
}
/**
* Spec §27. A rolling one-minute send budget. Several queues that post to the
* same collector share one instance, so the limit belongs to the collector
* rather than to each queue (Q-6.9-4).
*/
export interface TelemetrySendRate {
/**
* Takes a send slot and returns 0, or returns the milliseconds until a slot
* reopens. `force` always takes one: page exit may exceed the window once.
*/
take(force?: boolean): number;
}
export function createTelemetrySendRate(
maxSendsPerMinute: number,
now: () => number = () => performance.now(),
): TelemetrySendRate {
assertPositive("maxSendsPerMinute", maxSendsPerMinute);
/** Send instants inside the current window; never longer than the limit. */
const sendTimes: number[] = [];
return Object.freeze({
take(force = false): number {
const at = now();
while (sendTimes.length > 0 && at - (sendTimes[0] as number) >= RATE_WINDOW_MS) {
sendTimes.shift();
}
if (!force && sendTimes.length >= maxSendsPerMinute) {
return Math.max(1, (sendTimes[0] as number) + RATE_WINDOW_MS - at);
}
sendTimes.push(at);
if (sendTimes.length > maxSendsPerMinute) sendTimes.shift();
return 0;
},
});
}
export function createTelemetryPipeline(
options: Readonly<{
transport: TelemetryTransport;
budget?: TelemetryPipelineBudget;
/** Monotonic clock. */
now?: () => number;
/**
* A send-rate window shared with other queues posting to the same
* collector. Default: a private one from `budget.maxSendsPerMinute`.
*/
sendRate?: TelemetrySendRate;
/** Schedules a flush soon (default: microtask). */
schedule?: (callback: () => void) => void;
/** Schedules a flush after a rate window reopens (default: setTimeout). */
@@ -136,8 +177,8 @@ export function createTelemetryPipeline(
});
/** Duplicates counted but not yet reported, per fingerprint. */
const held = new Map<string, { event: TelemetryEvent<unknown>; count: number }>();
/** Send timestamps inside the current rate window; never above the limit. */
const sendTimes: number[] = [];
const sendRate =
options.sendRate ?? createTelemetrySendRate(budget.maxSendsPerMinute, now);
let disposed = false;
let flushScheduled = false;
@@ -156,16 +197,8 @@ export function createTelemetryPipeline(
peakBuffered: 0,
};
function rateAllows(at: number): boolean {
while (sendTimes.length > 0 && at - (sendTimes[0] as number) >= RATE_WINDOW_MS) {
sendTimes.shift();
}
return sendTimes.length < budget.maxSendsPerMinute;
}
function armRateTimer(at: number): void {
function armRateTimer(reopenIn: number): void {
if (rateTimer !== null || disposed) return;
const reopenIn = Math.max(1, (sendTimes[0] ?? at) + RATE_WINDOW_MS - at);
rateTimer = setTimer(() => {
rateTimer = null;
void flush();
@@ -227,13 +260,12 @@ export function createTelemetryPipeline(
async function drain(): Promise<void> {
while (!disposed && pending()) {
const at = now();
if (!rateAllows(at)) {
armRateTimer(at);
const reopenIn = sendRate.take();
if (reopenIn > 0) {
armRateTimer(reopenIn);
return;
}
const batch = takeBatch();
sendTimes.push(at);
counters.sends += 1;
try {
await options.transport.send(batch);