|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { registry } from './promClient'; |
| 3 | + |
| 4 | +const metricNames = (output: string): Set<string> => { |
| 5 | + return new Set( |
| 6 | + output |
| 7 | + .split('\n') |
| 8 | + .filter(line => line.length > 0 && !line.startsWith('#')) |
| 9 | + .map(line => line.split(/[ {]/)[0]) |
| 10 | + ); |
| 11 | +}; |
| 12 | + |
| 13 | +describe('web promClient', () => { |
| 14 | + it('exposes the metrics needed to diagnose heap pressure', async () => { |
| 15 | + const names = metricNames(await registry.metrics()); |
| 16 | + |
| 17 | + expect(names).toContain('nodejs_heap_size_limit_bytes'); |
| 18 | + expect(names).toContain('nodejs_heap_size_used_bytes'); |
| 19 | + expect(names).toContain('nodejs_eventloop_lag_p99_seconds'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('registers the gc duration histogram', () => { |
| 23 | + // Asserted via the registry rather than the rendered output: the histogram |
| 24 | + // emits no series until a garbage collection has actually been observed. |
| 25 | + expect(registry.getSingleMetric('nodejs_gc_duration_seconds')).toBeDefined(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('reports a plausible heap size limit', async () => { |
| 29 | + const output = await registry.metrics(); |
| 30 | + const line = output.split('\n').find(l => l.startsWith('nodejs_heap_size_limit_bytes ')); |
| 31 | + |
| 32 | + expect(line).toBeDefined(); |
| 33 | + |
| 34 | + const limit = Number(line!.split(' ')[1]); |
| 35 | + expect(Number.isFinite(limit)).toBe(true); |
| 36 | + // Any real V8 heap limit is well above 100MB and well below 100GB. |
| 37 | + expect(limit).toBeGreaterThan(100 * 1024 * 1024); |
| 38 | + expect(limit).toBeLessThan(100 * 1024 * 1024 * 1024); |
| 39 | + }); |
| 40 | + |
| 41 | + it('can be collected repeatedly', async () => { |
| 42 | + const first = await registry.metrics(); |
| 43 | + const second = await registry.metrics(); |
| 44 | + |
| 45 | + expect(metricNames(first)).toEqual(metricNames(second)); |
| 46 | + }); |
| 47 | +}); |
0 commit comments