|
| 1 | +import { |
| 2 | + startUpstreamTimer, |
| 3 | + isProfilingEnabled, |
| 4 | + resetUpstreamMetrics, |
| 5 | +} from '../metrics.js'; |
| 6 | +import client from 'prom-client'; |
| 7 | + |
| 8 | +// ── Helpers ────────────────────────────────────────────────────────────────── |
| 9 | + |
| 10 | +/** A single value entry from prom-client (includes metricName at runtime). */ |
| 11 | +interface MetricEntry { |
| 12 | + value: number; |
| 13 | + labels: Record<string, string>; |
| 14 | + metricName?: string; |
| 15 | +} |
| 16 | + |
| 17 | +/** Retrieve a single metric's collected values from the default registry. */ |
| 18 | +async function getMetricValues(name: string) { |
| 19 | + const metrics = await client.register.getMetricsAsJSON(); |
| 20 | + const found = metrics.find((m) => m.name === name); |
| 21 | + if (!found) return undefined; |
| 22 | + return { ...found, values: found.values as MetricEntry[] }; |
| 23 | +} |
| 24 | + |
| 25 | +// ── Setup / teardown ───────────────────────────────────────────────────────── |
| 26 | + |
| 27 | +const originalEnv = process.env.GATEWAY_PROFILING_ENABLED; |
| 28 | + |
| 29 | +afterEach(() => { |
| 30 | + // Restore env var to its original value between tests |
| 31 | + if (originalEnv === undefined) { |
| 32 | + delete process.env.GATEWAY_PROFILING_ENABLED; |
| 33 | + } else { |
| 34 | + process.env.GATEWAY_PROFILING_ENABLED = originalEnv; |
| 35 | + } |
| 36 | + resetUpstreamMetrics(); |
| 37 | +}); |
| 38 | + |
| 39 | +// ── Tests ──────────────────────────────────────────────────────────────────── |
| 40 | + |
| 41 | +describe('isProfilingEnabled', () => { |
| 42 | + it('returns false when GATEWAY_PROFILING_ENABLED is unset', () => { |
| 43 | + delete process.env.GATEWAY_PROFILING_ENABLED; |
| 44 | + expect(isProfilingEnabled()).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns false for arbitrary truthy strings', () => { |
| 48 | + process.env.GATEWAY_PROFILING_ENABLED = 'yes'; |
| 49 | + expect(isProfilingEnabled()).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns true only when set to "true"', () => { |
| 53 | + process.env.GATEWAY_PROFILING_ENABLED = 'true'; |
| 54 | + expect(isProfilingEnabled()).toBe(true); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('startUpstreamTimer (profiling disabled)', () => { |
| 59 | + beforeEach(() => { |
| 60 | + delete process.env.GATEWAY_PROFILING_ENABLED; |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns a no-op timer that does not throw', () => { |
| 64 | + const timer = startUpstreamTimer('api_1', 'GET'); |
| 65 | + expect(() => timer.stop(200, 'success')).not.toThrow(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('does not record any histogram observations', async () => { |
| 69 | + const timer = startUpstreamTimer('api_1', 'POST'); |
| 70 | + timer.stop(200, 'success'); |
| 71 | + |
| 72 | + const metric = await getMetricValues('gateway_upstream_duration_seconds'); |
| 73 | + // When profiling is off the metric exists but has no observed values |
| 74 | + const values = metric?.values ?? []; |
| 75 | + expect(values.filter((v) => v.labels.api_id === 'api_1')).toHaveLength(0); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('startUpstreamTimer (profiling enabled)', () => { |
| 80 | + beforeEach(() => { |
| 81 | + process.env.GATEWAY_PROFILING_ENABLED = 'true'; |
| 82 | + resetUpstreamMetrics(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('records a histogram observation on success', async () => { |
| 86 | + const timer = startUpstreamTimer('api_abc', 'GET'); |
| 87 | + // Simulate a short delay |
| 88 | + await new Promise((r) => setTimeout(r, 15)); |
| 89 | + timer.stop(200, 'success'); |
| 90 | + |
| 91 | + const metric = await getMetricValues('gateway_upstream_duration_seconds'); |
| 92 | + expect(metric).toBeDefined(); |
| 93 | + |
| 94 | + const countEntry = (metric?.values ?? []).find( |
| 95 | + (v) => |
| 96 | + v.metricName === 'gateway_upstream_duration_seconds_count' && |
| 97 | + v.labels.api_id === 'api_abc' && |
| 98 | + v.labels.method === 'GET' && |
| 99 | + v.labels.status_code === '200' && |
| 100 | + v.labels.outcome === 'success', |
| 101 | + ); |
| 102 | + expect(countEntry).toBeDefined(); |
| 103 | + expect(countEntry!.value).toBe(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it('increments the upstream requests counter', async () => { |
| 107 | + const timer = startUpstreamTimer('api_xyz', 'POST'); |
| 108 | + timer.stop(201, 'success'); |
| 109 | + |
| 110 | + const metric = await getMetricValues('gateway_upstream_requests_total'); |
| 111 | + expect(metric).toBeDefined(); |
| 112 | + |
| 113 | + const entry = (metric?.values ?? []).find( |
| 114 | + (v) => |
| 115 | + v.labels.api_id === 'api_xyz' && |
| 116 | + v.labels.method === 'POST' && |
| 117 | + v.labels.status_code === '201' && |
| 118 | + v.labels.outcome === 'success', |
| 119 | + ); |
| 120 | + expect(entry).toBeDefined(); |
| 121 | + expect(entry!.value).toBe(1); |
| 122 | + }); |
| 123 | + |
| 124 | + it('records timeout outcome correctly', async () => { |
| 125 | + const timer = startUpstreamTimer('api_slow', 'GET'); |
| 126 | + timer.stop(504, 'timeout'); |
| 127 | + |
| 128 | + const metric = await getMetricValues('gateway_upstream_requests_total'); |
| 129 | + const entry = (metric?.values ?? []).find( |
| 130 | + (v) => |
| 131 | + v.labels.api_id === 'api_slow' && |
| 132 | + v.labels.outcome === 'timeout' && |
| 133 | + v.labels.status_code === '504', |
| 134 | + ); |
| 135 | + expect(entry).toBeDefined(); |
| 136 | + expect(entry!.value).toBe(1); |
| 137 | + }); |
| 138 | + |
| 139 | + it('records error outcome correctly', async () => { |
| 140 | + const timer = startUpstreamTimer('api_down', 'POST'); |
| 141 | + timer.stop(502, 'error'); |
| 142 | + |
| 143 | + const metric = await getMetricValues('gateway_upstream_requests_total'); |
| 144 | + const entry = (metric?.values ?? []).find( |
| 145 | + (v) => |
| 146 | + v.labels.api_id === 'api_down' && |
| 147 | + v.labels.outcome === 'error' && |
| 148 | + v.labels.status_code === '502', |
| 149 | + ); |
| 150 | + expect(entry).toBeDefined(); |
| 151 | + expect(entry!.value).toBe(1); |
| 152 | + }); |
| 153 | + |
| 154 | + it('normalises method to uppercase', async () => { |
| 155 | + const timer = startUpstreamTimer('api_case', 'post'); |
| 156 | + timer.stop(200, 'success'); |
| 157 | + |
| 158 | + const metric = await getMetricValues('gateway_upstream_requests_total'); |
| 159 | + const entry = (metric?.values ?? []).find( |
| 160 | + (v) => v.labels.api_id === 'api_case' && v.labels.method === 'POST', |
| 161 | + ); |
| 162 | + expect(entry).toBeDefined(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('accumulates multiple observations for the same label set', async () => { |
| 166 | + for (let i = 0; i < 3; i++) { |
| 167 | + const timer = startUpstreamTimer('api_multi', 'GET'); |
| 168 | + timer.stop(200, 'success'); |
| 169 | + } |
| 170 | + |
| 171 | + const metric = await getMetricValues('gateway_upstream_requests_total'); |
| 172 | + const entry = (metric?.values ?? []).find( |
| 173 | + (v) => v.labels.api_id === 'api_multi' && v.labels.outcome === 'success', |
| 174 | + ); |
| 175 | + expect(entry).toBeDefined(); |
| 176 | + expect(entry!.value).toBe(3); |
| 177 | + }); |
| 178 | + |
| 179 | + it('records a positive duration value', async () => { |
| 180 | + const timer = startUpstreamTimer('api_dur', 'GET'); |
| 181 | + await new Promise((r) => setTimeout(r, 10)); |
| 182 | + timer.stop(200, 'success'); |
| 183 | + |
| 184 | + const metric = await getMetricValues('gateway_upstream_duration_seconds'); |
| 185 | + const sumEntry = (metric?.values ?? []).find( |
| 186 | + (v) => |
| 187 | + v.metricName === 'gateway_upstream_duration_seconds_sum' && |
| 188 | + v.labels.api_id === 'api_dur', |
| 189 | + ); |
| 190 | + expect(sumEntry).toBeDefined(); |
| 191 | + expect(sumEntry!.value).toBeGreaterThan(0); |
| 192 | + }); |
| 193 | +}); |
| 194 | + |
| 195 | +describe('metric registration', () => { |
| 196 | + it('gateway_upstream_duration_seconds is registered with correct buckets', async () => { |
| 197 | + process.env.GATEWAY_PROFILING_ENABLED = 'true'; |
| 198 | + const timer = startUpstreamTimer('api_bucket', 'GET'); |
| 199 | + timer.stop(200, 'success'); |
| 200 | + |
| 201 | + const metric = await getMetricValues('gateway_upstream_duration_seconds'); |
| 202 | + expect(metric).toBeDefined(); |
| 203 | + expect(metric!.type).toBe('histogram'); |
| 204 | + |
| 205 | + // Verify bucket boundaries exist in the values |
| 206 | + const bucketValues = (metric?.values ?? []).filter( |
| 207 | + (v) => v.metricName === 'gateway_upstream_duration_seconds_bucket', |
| 208 | + ); |
| 209 | + const bucketLe = bucketValues.map((v) => Number(v.labels.le)).filter((n) => isFinite(n)); |
| 210 | + expect(bucketLe).toEqual(expect.arrayContaining([0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10])); |
| 211 | + }); |
| 212 | + |
| 213 | + it('gateway_upstream_requests_total is registered as a counter', async () => { |
| 214 | + process.env.GATEWAY_PROFILING_ENABLED = 'true'; |
| 215 | + const timer = startUpstreamTimer('api_type', 'GET'); |
| 216 | + timer.stop(200, 'success'); |
| 217 | + |
| 218 | + const metric = await getMetricValues('gateway_upstream_requests_total'); |
| 219 | + expect(metric).toBeDefined(); |
| 220 | + expect(metric!.type).toBe('counter'); |
| 221 | + }); |
| 222 | +}); |
| 223 | + |
| 224 | +describe('resetUpstreamMetrics', () => { |
| 225 | + it('clears previously recorded observations', async () => { |
| 226 | + process.env.GATEWAY_PROFILING_ENABLED = 'true'; |
| 227 | + |
| 228 | + const timer = startUpstreamTimer('api_reset', 'GET'); |
| 229 | + timer.stop(200, 'success'); |
| 230 | + |
| 231 | + resetUpstreamMetrics(); |
| 232 | + |
| 233 | + const metric = await getMetricValues('gateway_upstream_requests_total'); |
| 234 | + const entry = (metric?.values ?? []).find( |
| 235 | + (v) => v.labels.api_id === 'api_reset', |
| 236 | + ); |
| 237 | + expect(entry).toBeUndefined(); |
| 238 | + }); |
| 239 | +}); |
0 commit comments