metrics.ts41 lines · main
| 1 | import { createMetricsRegistry } from '@briven/shared/observability'; |
| 2 | |
| 3 | /** |
| 4 | * runtime's metrics registry. The registerPoolGauges helper is the |
| 5 | * runtime-specific binding to the IsolatePool — it stashes a pull-based |
| 6 | * provider that snapshots `describeForMetrics()` at scrape time, so the |
| 7 | * spawn / invoke hot paths stay free of gauge bookkeeping. |
| 8 | */ |
| 9 | const registry = createMetricsRegistry({ |
| 10 | help: { |
| 11 | briven_runtime_invocations_total: 'Total invocations completed', |
| 12 | briven_runtime_invocation_duration_ms: 'Invocation duration (ms)', |
| 13 | briven_runtime_isolate_spawns_total: 'Total isolate spawn attempts by outcome', |
| 14 | briven_runtime_cold_start_ms: 'Isolate cold-start latency (ms)', |
| 15 | briven_runtime_isolate_kills_total: 'Total isolate kills by reason', |
| 16 | briven_runtime_crash_loop_breaks_total: 'Crash-loop breaker trips', |
| 17 | briven_runtime_pool_size: 'Total isolates in pool', |
| 18 | briven_runtime_isolates_by_state: 'Isolates in each lifecycle state', |
| 19 | }, |
| 20 | }); |
| 21 | |
| 22 | export const incCounter = registry.incCounter; |
| 23 | export const observeHistogram = registry.observeHistogram; |
| 24 | export const renderPrometheus = registry.render; |
| 25 | export const resetMetrics = registry.reset; |
| 26 | |
| 27 | export function registerPoolGauges(pool: { |
| 28 | describeForMetrics(): { isolatesByState: Record<string, number>; poolSize: number }; |
| 29 | }): void { |
| 30 | registry.registerGauge('briven_runtime_pool_size', () => { |
| 31 | const snap = pool.describeForMetrics(); |
| 32 | return [{ labels: {}, value: snap.poolSize }]; |
| 33 | }); |
| 34 | registry.registerGauge('briven_runtime_isolates_by_state', () => { |
| 35 | const snap = pool.describeForMetrics(); |
| 36 | return Object.entries(snap.isolatesByState).map(([state, value]) => ({ |
| 37 | labels: { state }, |
| 38 | value, |
| 39 | })); |
| 40 | }); |
| 41 | } |