|
| 1 | +import { IS_DEV, IS_TEST, IS_CI } from '@codebuff/common/env' |
| 2 | + |
| 3 | +import { getApiClient } from './codebuff-api' |
| 4 | +import { getCliEnv } from './env' |
| 5 | + |
| 6 | +import type { LogRecordInput } from '@codebuff/common/schemas/logs' |
| 7 | + |
| 8 | +/** |
| 9 | + * Client-side shipper that mirrors CLI logs/events into the server's Axiom |
| 10 | + * logs sink via POST /api/logs. Runs alongside PostHog (it does not replace |
| 11 | + * it). Fully best-effort: batched, fire-and-forget, never throws, never logs |
| 12 | + * through the app logger (which would recurse). |
| 13 | + * |
| 14 | + * Tuning via env: |
| 15 | + * - CODEBUFF_SHIP_LOGS 'true' | 'false' (default: on outside dev/test) |
| 16 | + */ |
| 17 | + |
| 18 | +const MAX_BATCH = 50 |
| 19 | +const FLUSH_INTERVAL_MS = 10_000 |
| 20 | +const MAX_BUFFER = 1_000 |
| 21 | + |
| 22 | +let buffer: LogRecordInput[] = [] |
| 23 | +let timer: ReturnType<typeof setInterval> | null = null |
| 24 | +let flushing = false |
| 25 | +let shutdownRegistered = false |
| 26 | + |
| 27 | +function enabled(): boolean { |
| 28 | + const flag = getCliEnv().CODEBUFF_SHIP_LOGS |
| 29 | + if (flag === 'true') return true |
| 30 | + if (flag === 'false') return false |
| 31 | + return !IS_DEV && !IS_TEST && !IS_CI |
| 32 | +} |
| 33 | + |
| 34 | +function ensureTimer(): void { |
| 35 | + if (timer) return |
| 36 | + timer = setInterval(() => { |
| 37 | + void flushClientLogs() |
| 38 | + }, FLUSH_INTERVAL_MS) |
| 39 | + ;(timer as { unref?: () => void }).unref?.() |
| 40 | +} |
| 41 | + |
| 42 | +function registerShutdown(): void { |
| 43 | + if (shutdownRegistered) return |
| 44 | + shutdownRegistered = true |
| 45 | + const onExit = () => { |
| 46 | + void flushClientLogs() |
| 47 | + } |
| 48 | + process.once('beforeExit', onExit) |
| 49 | + process.once('SIGTERM', onExit) |
| 50 | + process.once('SIGINT', onExit) |
| 51 | +} |
| 52 | + |
| 53 | +/** Buffer one record for shipping. Cheap, synchronous, never throws. */ |
| 54 | +export function enqueueClientLog(record: LogRecordInput): void { |
| 55 | + if (!enabled()) return |
| 56 | + if (buffer.length >= MAX_BUFFER) { |
| 57 | + buffer.shift() |
| 58 | + } |
| 59 | + buffer.push(record) |
| 60 | + ensureTimer() |
| 61 | + registerShutdown() |
| 62 | + if (buffer.length >= MAX_BATCH) { |
| 63 | + void flushClientLogs() |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** Flush a batch to /api/logs. Requeues if not yet authenticated. */ |
| 68 | +export async function flushClientLogs(): Promise<void> { |
| 69 | + if (flushing || buffer.length === 0) return |
| 70 | + flushing = true |
| 71 | + const batch = buffer.splice(0, MAX_BATCH) |
| 72 | + try { |
| 73 | + const client = getApiClient() |
| 74 | + if (!client.authToken) { |
| 75 | + // Not logged in yet — put the batch back (bounded by MAX_BUFFER) so we |
| 76 | + // can ship it once auth is available. |
| 77 | + buffer.unshift(...batch) |
| 78 | + return |
| 79 | + } |
| 80 | + await client.post( |
| 81 | + '/api/logs', |
| 82 | + { records: batch }, |
| 83 | + { includeAuth: true, retry: false, timeoutMs: 5_000 }, |
| 84 | + ) |
| 85 | + } catch { |
| 86 | + // Best-effort: drop on error rather than risk unbounded growth. |
| 87 | + } finally { |
| 88 | + flushing = false |
| 89 | + } |
| 90 | +} |
0 commit comments