|
| 1 | +import { test } from 'node:test' |
| 2 | +import { strictEqual, deepStrictEqual, ok } from 'node:assert' |
| 3 | +import { initMetrics } from '../lib/metrics.js' |
| 4 | +import promClient from 'prom-client' |
| 5 | + |
| 6 | +test('initMetrics should return null when prometheus is not provided', async () => { |
| 7 | + const result = initMetrics() |
| 8 | + strictEqual(result, null) |
| 9 | +}) |
| 10 | + |
| 11 | +test('initMetrics should return null when prometheus is null', async () => { |
| 12 | + const result = initMetrics(null) |
| 13 | + strictEqual(result, null) |
| 14 | +}) |
| 15 | + |
| 16 | +test('initMetrics should return null when prometheus.registry is missing', async () => { |
| 17 | + const prometheus = { client: promClient } |
| 18 | + const result = initMetrics(prometheus) |
| 19 | + strictEqual(result, null) |
| 20 | +}) |
| 21 | + |
| 22 | +test('initMetrics should return null when prometheus.client is missing', async () => { |
| 23 | + const prometheus = { registry: new promClient.Registry() } |
| 24 | + const result = initMetrics(prometheus) |
| 25 | + strictEqual(result, null) |
| 26 | +}) |
| 27 | + |
| 28 | +test('initMetrics should return metrics object when prometheus is properly configured', async () => { |
| 29 | + const registry = new promClient.Registry() |
| 30 | + const prometheus = { registry, client: promClient } |
| 31 | + const metrics = initMetrics(prometheus) |
| 32 | + |
| 33 | + ok(metrics, 'Metrics object should be created') |
| 34 | + ok(metrics.messagesInFlight, 'messagesInFlight metric should exist') |
| 35 | + ok(metrics.httpRequestDuration, 'httpRequestDuration metric should exist') |
| 36 | + ok(metrics.dlqMessages, 'dlqMessages metric should exist') |
| 37 | +}) |
| 38 | + |
| 39 | +test('messagesInFlight should be configured as Gauge with correct properties', async () => { |
| 40 | + const registry = new promClient.Registry() |
| 41 | + const prometheus = { registry, client: promClient } |
| 42 | + const metrics = initMetrics(prometheus) |
| 43 | + |
| 44 | + const gauge = metrics.messagesInFlight |
| 45 | + strictEqual(gauge.name, 'kafka_hooks_messages_in_flight') |
| 46 | + strictEqual(gauge.help, 'Number of messages currently being processed') |
| 47 | + deepStrictEqual(gauge.labelNames, ['topic']) |
| 48 | +}) |
| 49 | + |
| 50 | +test('httpRequestDuration should be configured as Histogram with correct properties', async () => { |
| 51 | + const registry = new promClient.Registry() |
| 52 | + const prometheus = { registry, client: promClient } |
| 53 | + const metrics = initMetrics(prometheus) |
| 54 | + |
| 55 | + const histogram = metrics.httpRequestDuration |
| 56 | + strictEqual(histogram.name, 'kafka_hooks_http_request_duration_seconds') |
| 57 | + strictEqual(histogram.help, 'HTTP request duration for webhook deliveries') |
| 58 | + deepStrictEqual(histogram.labelNames, ['topic', 'method', 'status_code']) |
| 59 | + deepStrictEqual(histogram.buckets, [0.1, 0.5, 1, 2, 5, 10]) |
| 60 | +}) |
| 61 | + |
| 62 | +test('dlqMessages should be configured as Counter with correct properties', async () => { |
| 63 | + const registry = new promClient.Registry() |
| 64 | + const prometheus = { registry, client: promClient } |
| 65 | + const metrics = initMetrics(prometheus) |
| 66 | + |
| 67 | + const counter = metrics.dlqMessages |
| 68 | + strictEqual(counter.name, 'kafka_hooks_dlq_messages_total') |
| 69 | + strictEqual(counter.help, 'Total number of messages sent to the DLQ (Dead Letter Queue)') |
| 70 | + deepStrictEqual(counter.labelNames, ['topic', 'reason']) |
| 71 | +}) |
| 72 | + |
| 73 | +test('metrics should be functional - Counter operations', async () => { |
| 74 | + const registry = new promClient.Registry() |
| 75 | + const prometheus = { registry, client: promClient } |
| 76 | + const metrics = initMetrics(prometheus) |
| 77 | + |
| 78 | + metrics.dlqMessages.inc({ topic: 'test-topic', reason: 'http_500' }) |
| 79 | + metrics.dlqMessages.inc({ topic: 'test-topic', reason: 'network_error' }, 3) |
| 80 | + |
| 81 | + const updatedMetrics = await registry.getMetricsAsJSON() |
| 82 | + const dlqMetric = updatedMetrics.find(m => m.name === 'kafka_hooks_dlq_messages_total') |
| 83 | + |
| 84 | + const http500Value = dlqMetric.values.find(v => |
| 85 | + v.labels.topic === 'test-topic' && v.labels.reason === 'http_500' |
| 86 | + ).value |
| 87 | + const networkErrorValue = dlqMetric.values.find(v => |
| 88 | + v.labels.topic === 'test-topic' && v.labels.reason === 'network_error' |
| 89 | + ).value |
| 90 | + |
| 91 | + strictEqual(http500Value, 1) |
| 92 | + strictEqual(networkErrorValue, 3) |
| 93 | +}) |
| 94 | + |
| 95 | +test('metrics should be functional - Gauge operations', async () => { |
| 96 | + const registry = new promClient.Registry() |
| 97 | + const prometheus = { registry, client: promClient } |
| 98 | + const metrics = initMetrics(prometheus) |
| 99 | + |
| 100 | + metrics.messagesInFlight.inc({ topic: 'test-topic' }) |
| 101 | + metrics.messagesInFlight.inc({ topic: 'test-topic' }, 2) |
| 102 | + |
| 103 | + let registryMetrics = await registry.getMetricsAsJSON() |
| 104 | + let gaugeMetric = registryMetrics.find(m => m.name === 'kafka_hooks_messages_in_flight') |
| 105 | + let value = gaugeMetric.values.find(v => v.labels.topic === 'test-topic').value |
| 106 | + strictEqual(value, 3) |
| 107 | + |
| 108 | + metrics.messagesInFlight.dec({ topic: 'test-topic' }) |
| 109 | + |
| 110 | + registryMetrics = await registry.getMetricsAsJSON() |
| 111 | + gaugeMetric = registryMetrics.find(m => m.name === 'kafka_hooks_messages_in_flight') |
| 112 | + value = gaugeMetric.values.find(v => v.labels.topic === 'test-topic').value |
| 113 | + strictEqual(value, 2) |
| 114 | + |
| 115 | + metrics.messagesInFlight.set({ topic: 'test-topic' }, 5) |
| 116 | + |
| 117 | + registryMetrics = await registry.getMetricsAsJSON() |
| 118 | + gaugeMetric = registryMetrics.find(m => m.name === 'kafka_hooks_messages_in_flight') |
| 119 | + value = gaugeMetric.values.find(v => v.labels.topic === 'test-topic').value |
| 120 | + strictEqual(value, 5) |
| 121 | +}) |
| 122 | + |
| 123 | +test('metrics should be functional - Histogram operations', async () => { |
| 124 | + const registry = new promClient.Registry() |
| 125 | + const prometheus = { registry, client: promClient } |
| 126 | + const metrics = initMetrics(prometheus) |
| 127 | + |
| 128 | + metrics.httpRequestDuration.observe({ topic: 'test-topic', method: 'POST', status_code: '200' }, 0.5) |
| 129 | + metrics.httpRequestDuration.observe({ topic: 'test-topic', method: 'POST', status_code: '200' }, 1.2) |
| 130 | + metrics.httpRequestDuration.observe({ topic: 'test-topic', method: 'POST', status_code: '500' }, 2.1) |
| 131 | + |
| 132 | + const registryMetrics = await registry.getMetricsAsJSON() |
| 133 | + const histogramMetric = registryMetrics.find(m => m.name === 'kafka_hooks_http_request_duration_seconds') |
| 134 | + |
| 135 | + const countValue = histogramMetric.values.find(v => |
| 136 | + v.labels.topic === 'test-topic' && |
| 137 | + v.labels.method === 'POST' && |
| 138 | + v.labels.status_code === '200' && |
| 139 | + v.metricName === 'kafka_hooks_http_request_duration_seconds_count' |
| 140 | + ).value |
| 141 | + |
| 142 | + const sumValue = histogramMetric.values.find(v => |
| 143 | + v.labels.topic === 'test-topic' && |
| 144 | + v.labels.method === 'POST' && |
| 145 | + v.labels.status_code === '200' && |
| 146 | + v.metricName === 'kafka_hooks_http_request_duration_seconds_sum' |
| 147 | + ).value |
| 148 | + |
| 149 | + strictEqual(countValue, 2) // Two observations for 200 status code |
| 150 | + strictEqual(sumValue, 1.7) // 0.5 + 1.2 = 1.7 |
| 151 | +}) |
0 commit comments