Some checks failed
CI/CD Pipeline - Build, Test, and Deploy / 🧪 Test & Lint (push) Successful in 9m31s
CI/CD Pipeline - Build, Test, and Deploy / 🔒 Security Scan (push) Successful in 9m29s
CI/CD Pipeline - Build, Test, and Deploy / 🏗️ Build & Push Image (push) Failing after 11m19s
CI/CD Pipeline - Build, Test, and Deploy / 🛡️ Image Security Scan (push) Has been skipped
CI/CD Pipeline - Build, Test, and Deploy / 🚀 Deploy to Development (push) Has been skipped
CI/CD Pipeline - Build, Test, and Deploy / 🏭 Deploy to Production (push) Has been skipped
CI/CD Pipeline - Build, Test, and Deploy / 🧹 Cleanup (push) Successful in 1s
122 lines
3.5 KiB
JavaScript
122 lines
3.5 KiB
JavaScript
const {
|
|
incrementCounter,
|
|
setGauge,
|
|
recordHistogram,
|
|
getMetrics,
|
|
resetMetrics
|
|
} = require('../src/utils/metrics');
|
|
|
|
describe('Metrics Utility', () => {
|
|
beforeEach(() => {
|
|
resetMetrics();
|
|
});
|
|
|
|
describe('incrementCounter', () => {
|
|
it('should increment a counter', () => {
|
|
incrementCounter('test_counter');
|
|
incrementCounter('test_counter');
|
|
|
|
const metrics = getMetrics();
|
|
const counter = metrics.counters.find(c => c.name === 'test_counter');
|
|
|
|
expect(counter).toBeDefined();
|
|
expect(counter.value).toBe(2);
|
|
});
|
|
|
|
it('should handle counters with labels', () => {
|
|
incrementCounter('http_requests', { method: 'GET' });
|
|
incrementCounter('http_requests', { method: 'POST' });
|
|
incrementCounter('http_requests', { method: 'GET' });
|
|
|
|
const metrics = getMetrics();
|
|
const getCounter = metrics.counters.find(c =>
|
|
c.name === 'http_requests' && c.labels.method === 'GET'
|
|
);
|
|
const postCounter = metrics.counters.find(c =>
|
|
c.name === 'http_requests' && c.labels.method === 'POST'
|
|
);
|
|
|
|
expect(getCounter.value).toBe(2);
|
|
expect(postCounter.value).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('setGauge', () => {
|
|
it('should set a gauge value', () => {
|
|
setGauge('memory_usage', 85.5);
|
|
|
|
const metrics = getMetrics();
|
|
const gauge = metrics.gauges.find(g => g.name === 'memory_usage');
|
|
|
|
expect(gauge).toBeDefined();
|
|
expect(gauge.value).toBe(85.5);
|
|
});
|
|
|
|
it('should update existing gauge values', () => {
|
|
setGauge('cpu_usage', 50);
|
|
setGauge('cpu_usage', 75);
|
|
|
|
const metrics = getMetrics();
|
|
const gauge = metrics.gauges.find(g => g.name === 'cpu_usage');
|
|
|
|
expect(gauge.value).toBe(75);
|
|
});
|
|
});
|
|
|
|
describe('recordHistogram', () => {
|
|
it('should record histogram values', () => {
|
|
recordHistogram('response_time', 100);
|
|
recordHistogram('response_time', 150);
|
|
recordHistogram('response_time', 200);
|
|
|
|
const metrics = getMetrics();
|
|
const histogram = metrics.histograms.find(h => h.name === 'response_time');
|
|
|
|
expect(histogram).toBeDefined();
|
|
expect(histogram.count).toBe(3);
|
|
expect(histogram.sum).toBe(450);
|
|
expect(histogram.average).toBe(150);
|
|
expect(histogram.min).toBe(100);
|
|
expect(histogram.max).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('getMetrics', () => {
|
|
it('should return all metrics with runtime info', () => {
|
|
incrementCounter('test_counter');
|
|
setGauge('test_gauge', 42);
|
|
recordHistogram('test_histogram', 100);
|
|
|
|
const metrics = getMetrics();
|
|
|
|
expect(metrics).toHaveProperty('counters');
|
|
expect(metrics).toHaveProperty('gauges');
|
|
expect(metrics).toHaveProperty('histograms');
|
|
expect(metrics).toHaveProperty('runtime');
|
|
expect(metrics).toHaveProperty('timestamp');
|
|
|
|
expect(metrics.counters).toHaveLength(1);
|
|
expect(metrics.gauges).toHaveLength(1);
|
|
expect(metrics.histograms).toHaveLength(1);
|
|
|
|
expect(metrics.runtime).toHaveProperty('uptime_seconds');
|
|
expect(metrics.runtime).toHaveProperty('memory_usage_bytes');
|
|
});
|
|
});
|
|
|
|
describe('resetMetrics', () => {
|
|
it('should clear all metrics', () => {
|
|
incrementCounter('test_counter');
|
|
setGauge('test_gauge', 42);
|
|
recordHistogram('test_histogram', 100);
|
|
|
|
resetMetrics();
|
|
|
|
const metrics = getMetrics();
|
|
expect(metrics.counters).toHaveLength(0);
|
|
expect(metrics.gauges).toHaveLength(0);
|
|
expect(metrics.histograms).toHaveLength(0);
|
|
});
|
|
});
|
|
});
|