|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 2 | + |
| 3 | +import statsd from '@/observability/lib/statsd' |
| 4 | +import { |
| 5 | + startRuntimeMetrics, |
| 6 | + _resetForTesting, |
| 7 | + INTERVAL_MS, |
| 8 | +} from '@/observability/lib/runtime-metrics' |
| 9 | + |
| 10 | +vi.mock('@/observability/lib/statsd', () => ({ |
| 11 | + default: { |
| 12 | + gauge: vi.fn(), |
| 13 | + histogram: vi.fn(), |
| 14 | + }, |
| 15 | +})) |
| 16 | + |
| 17 | +describe('startRuntimeMetrics', () => { |
| 18 | + beforeEach(() => { |
| 19 | + _resetForTesting() |
| 20 | + vi.useFakeTimers() |
| 21 | + vi.clearAllMocks() |
| 22 | + }) |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + vi.unstubAllEnvs() |
| 26 | + vi.useRealTimers() |
| 27 | + }) |
| 28 | + |
| 29 | + it('is a no-op in test / non-prod environments', () => { |
| 30 | + vi.stubEnv('MODA_PROD_SERVICE_ENV', 'false') |
| 31 | + startRuntimeMetrics() |
| 32 | + vi.advanceTimersByTime(INTERVAL_MS + 1) |
| 33 | + expect(statsd.gauge).not.toHaveBeenCalled() |
| 34 | + }) |
| 35 | + |
| 36 | + it('is idempotent — second call does nothing extra', () => { |
| 37 | + vi.stubEnv('MODA_PROD_SERVICE_ENV', 'true') |
| 38 | + vi.stubEnv('NODE_ENV', 'production') |
| 39 | + startRuntimeMetrics() |
| 40 | + // Second call without reset — should be a no-op |
| 41 | + startRuntimeMetrics() |
| 42 | + vi.advanceTimersByTime(INTERVAL_MS + 1) |
| 43 | + const callCount = (statsd.gauge as ReturnType<typeof vi.fn>).mock.calls.length |
| 44 | + |
| 45 | + vi.clearAllMocks() |
| 46 | + vi.advanceTimersByTime(INTERVAL_MS) |
| 47 | + const secondTickCount = (statsd.gauge as ReturnType<typeof vi.fn>).mock.calls.length |
| 48 | + // Same number of calls each tick — no duplicate timers registered |
| 49 | + expect(secondTickCount).toBe(callCount) |
| 50 | + }) |
| 51 | + |
| 52 | + it('emits heap gauges when enabled', () => { |
| 53 | + vi.stubEnv('MODA_PROD_SERVICE_ENV', 'true') |
| 54 | + vi.stubEnv('NODE_ENV', 'production') |
| 55 | + startRuntimeMetrics() |
| 56 | + vi.advanceTimersByTime(INTERVAL_MS + 1) |
| 57 | + |
| 58 | + const gaugeNames = (statsd.gauge as ReturnType<typeof vi.fn>).mock.calls.map( |
| 59 | + (c: unknown[]) => c[0], |
| 60 | + ) |
| 61 | + expect(gaugeNames).toContain('node.heap.used') |
| 62 | + expect(gaugeNames).toContain('node.heap.total') |
| 63 | + expect(gaugeNames).toContain('node.heap.limit') |
| 64 | + expect(gaugeNames).toContain('node.heap.external') |
| 65 | + expect(gaugeNames).toContain('node.heap.used_pct') |
| 66 | + }) |
| 67 | + |
| 68 | + it('emits event-loop delay gauges when enabled', () => { |
| 69 | + vi.stubEnv('MODA_PROD_SERVICE_ENV', 'true') |
| 70 | + vi.stubEnv('NODE_ENV', 'production') |
| 71 | + startRuntimeMetrics() |
| 72 | + vi.advanceTimersByTime(INTERVAL_MS + 1) |
| 73 | + |
| 74 | + const gaugeNames = (statsd.gauge as ReturnType<typeof vi.fn>).mock.calls.map( |
| 75 | + (c: unknown[]) => c[0], |
| 76 | + ) |
| 77 | + expect(gaugeNames).toContain('node.eventloop.delay.p50') |
| 78 | + expect(gaugeNames).toContain('node.eventloop.delay.p99') |
| 79 | + expect(gaugeNames).toContain('node.eventloop.delay.max') |
| 80 | + }) |
| 81 | + |
| 82 | + it('emits heap values that are positive numbers', () => { |
| 83 | + vi.stubEnv('MODA_PROD_SERVICE_ENV', 'true') |
| 84 | + vi.stubEnv('NODE_ENV', 'production') |
| 85 | + startRuntimeMetrics() |
| 86 | + vi.advanceTimersByTime(INTERVAL_MS + 1) |
| 87 | + |
| 88 | + const heapUsedCall = (statsd.gauge as ReturnType<typeof vi.fn>).mock.calls.find( |
| 89 | + (c: unknown[]) => c[0] === 'node.heap.used', |
| 90 | + ) |
| 91 | + expect(heapUsedCall).toBeDefined() |
| 92 | + expect(heapUsedCall![1]).toBeGreaterThan(0) |
| 93 | + |
| 94 | + const heapPctCall = (statsd.gauge as ReturnType<typeof vi.fn>).mock.calls.find( |
| 95 | + (c: unknown[]) => c[0] === 'node.heap.used_pct', |
| 96 | + ) |
| 97 | + expect(heapPctCall).toBeDefined() |
| 98 | + expect(heapPctCall![1]).toBeGreaterThan(0) |
| 99 | + expect(heapPctCall![1]).toBeLessThan(100) |
| 100 | + }) |
| 101 | +}) |
0 commit comments