metrics-endpoint.test.ts54 lines · main
| 1 | import { beforeEach, describe, expect, test } from 'bun:test'; |
| 2 | |
| 3 | import { |
| 4 | incCounter, |
| 5 | observeHistogram, |
| 6 | registerPoolGauges, |
| 7 | renderPrometheus, |
| 8 | resetMetrics, |
| 9 | } from '../src/metrics.js'; |
| 10 | |
| 11 | describe('metrics', () => { |
| 12 | beforeEach(() => { |
| 13 | resetMetrics(); |
| 14 | }); |
| 15 | |
| 16 | test('renders counters in Prometheus format', () => { |
| 17 | incCounter('test_counter', { label: 'a' }); |
| 18 | incCounter('test_counter', { label: 'a' }); |
| 19 | incCounter('test_counter', { label: 'b' }); |
| 20 | const out = renderPrometheus(); |
| 21 | expect(out).toContain('test_counter{label="a"} 2'); |
| 22 | expect(out).toContain('test_counter{label="b"} 1'); |
| 23 | expect(out).toContain('# TYPE test_counter counter'); |
| 24 | }); |
| 25 | |
| 26 | test('renders histogram with buckets', () => { |
| 27 | observeHistogram('test_histogram', 5); |
| 28 | observeHistogram('test_histogram', 25); |
| 29 | observeHistogram('test_histogram', 150); |
| 30 | const out = renderPrometheus(); |
| 31 | expect(out).toContain('test_histogram_bucket{le="10"} 1'); |
| 32 | expect(out).toContain('test_histogram_bucket{le="50"} 2'); |
| 33 | expect(out).toContain('test_histogram_bucket{le="200"} 3'); |
| 34 | expect(out).toContain('test_histogram_bucket{le="+Inf"} 3'); |
| 35 | expect(out).toMatch(/test_histogram_count\s+3/); |
| 36 | expect(out).toMatch(/test_histogram_sum\s+180/); |
| 37 | expect(out).toContain('# TYPE test_histogram histogram'); |
| 38 | }); |
| 39 | |
| 40 | test('exposes pool gauges from the registered provider', () => { |
| 41 | const fakePool = { |
| 42 | describeForMetrics: () => ({ |
| 43 | isolatesByState: { ready: 2, in_flight: 1, spawning: 0, retiring: 0, dead: 0 }, |
| 44 | poolSize: 3, |
| 45 | }), |
| 46 | }; |
| 47 | registerPoolGauges(fakePool); |
| 48 | const out = renderPrometheus(); |
| 49 | expect(out).toContain('# TYPE briven_runtime_pool_size gauge'); |
| 50 | expect(out).toContain('briven_runtime_pool_size 3'); |
| 51 | expect(out).toContain('briven_runtime_isolates_by_state{state="ready"} 2'); |
| 52 | expect(out).toContain('briven_runtime_isolates_by_state{state="in_flight"} 1'); |
| 53 | }); |
| 54 | }); |