From d68c089f739d80f8af801e4f6330f83aa132aa8e Mon Sep 17 00:00:00 2001 From: Sebastien Melki Date: Sat, 25 Apr 2026 17:30:38 +0300 Subject: [PATCH] fix(usage): add 'live' cache tier + revert preview debug logs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Sync UsageCacheTier with the local CacheTier in gateway.ts (main added 'live' in PR #3402 — synthetic merge with main was failing typecheck:api). - Revert temporary unconditional debug logs in sendToAxiom now that Axiom delivery is verified end-to-end on preview (event landed with all fields populated, including the new auth_401 reason from the koala #3403 fix). Co-Authored-By: Claude Opus 4.7 (1M context) --- server/_shared/usage.ts | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/server/_shared/usage.ts b/server/_shared/usage.ts index 7c15ea2e1..c06590eb0 100644 --- a/server/_shared/usage.ts +++ b/server/_shared/usage.ts @@ -23,6 +23,7 @@ const TELEMETRY_TIMEOUT_MS = 1_500; const CB_WINDOW_MS = 5 * 60 * 1_000; const CB_TRIP_FAILURE_RATIO = 0.05; const CB_MIN_SAMPLES = 20; +const SAMPLED_DROP_LOG_RATE = 0.01; function isUsageEnabled(): boolean { return process.env.USAGE_TELEMETRY === '1'; @@ -41,7 +42,8 @@ export type CacheTier = | 'slow-browser' | 'static' | 'daily' - | 'no-store'; + | 'no-store' + | 'live'; export type CacheStatus = 'miss' | 'fresh' | 'stale-while-revalidate' | 'neg-sentinel'; @@ -353,35 +355,25 @@ export function getUsageScope(): UsageScope | undefined { // ---------- Sink ---------- export async function sendToAxiom(events: UsageEvent[]): Promise { - // TEMP DEBUG: unconditional entry log to verify codepath in preview. - // Remove before merging to main. - console.log('[usage-telemetry][debug] sendToAxiom enter', { - enabled: isUsageEnabled(), - eventCount: events.length, - tokenPresent: Boolean(process.env.AXIOM_API_TOKEN), - tokenLen: (process.env.AXIOM_API_TOKEN ?? '').length, - breakerTripped, - url: AXIOM_INGEST_URL, - }); - if (!isUsageEnabled()) { - console.log('[usage-telemetry][debug] drop: not enabled'); - return; - } + if (!isUsageEnabled()) return; if (events.length === 0) return; const token = process.env.AXIOM_API_TOKEN; if (!token) { - console.warn('[usage-telemetry][debug] drop: no-token'); + if (Math.random() < SAMPLED_DROP_LOG_RATE) { + console.warn('[usage-telemetry] drop', { reason: 'no-token' }); + } return; } if (breakerTripped) { - console.warn('[usage-telemetry][debug] drop: breaker-open'); + if (Math.random() < SAMPLED_DROP_LOG_RATE) { + console.warn('[usage-telemetry] drop', { reason: 'breaker-open' }); + } return; } const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), TELEMETRY_TIMEOUT_MS); try { - console.log('[usage-telemetry][debug] POSTing', { eventCount: events.length, firstEventType: events[0]?.event_type }); const resp = await fetch(AXIOM_INGEST_URL, { method: 'POST', headers: { @@ -391,18 +383,20 @@ export async function sendToAxiom(events: UsageEvent[]): Promise { body: JSON.stringify(events), signal: controller.signal, }); - console.log('[usage-telemetry][debug] response', { status: resp.status, ok: resp.ok }); if (!resp.ok) { - const bodyText = await resp.text().catch(() => ''); - console.warn('[usage-telemetry][debug] axiom rejected', { status: resp.status, body: bodyText.slice(0, 500) }); recordSample(false); + if (Math.random() < SAMPLED_DROP_LOG_RATE) { + console.warn('[usage-telemetry] drop', { reason: `http-${resp.status}` }); + } return; } recordSample(true); } catch (err) { recordSample(false); - const reason = err instanceof Error && err.name === 'AbortError' ? 'timeout' : 'fetch-error'; - console.warn('[usage-telemetry][debug] fetch failed', { reason, message: err instanceof Error ? err.message : String(err) }); + if (Math.random() < SAMPLED_DROP_LOG_RATE) { + const reason = err instanceof Error && err.name === 'AbortError' ? 'timeout' : 'fetch-error'; + console.warn('[usage-telemetry] drop', { reason }); + } } finally { clearTimeout(timer); }