|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { Counter, Histogram, Registry } from 'prom-client'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +import WsMetricRegistry from '../../src/metrics/wsMetricRegistry'; |
| 7 | +import { WS_CONSTANTS } from '../../src/utils/constants'; |
| 8 | + |
| 9 | +describe('WsMetricRegistry', function () { |
| 10 | + let wsMetricRegistry: WsMetricRegistry; |
| 11 | + let mockRegistry: Registry; |
| 12 | + let removeSingleMetricStub: sinon.SinonStub; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + mockRegistry = new Registry(); |
| 16 | + removeSingleMetricStub = sinon.stub(mockRegistry, 'removeSingleMetric'); |
| 17 | + |
| 18 | + sinon.stub(process, 'hrtime').returns([1, 0]); // 1 second |
| 19 | + sinon.stub(process, 'memoryUsage').returns({ |
| 20 | + rss: 100000000, |
| 21 | + heapTotal: 50000000, |
| 22 | + heapUsed: 30000000, |
| 23 | + external: 5000000, |
| 24 | + arrayBuffers: 1000000, |
| 25 | + }); |
| 26 | + |
| 27 | + wsMetricRegistry = new WsMetricRegistry(mockRegistry); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + sinon.restore(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('constructor', function () { |
| 35 | + it('should initialize all counter metrics', function () { |
| 36 | + const counterMetrics = [ |
| 37 | + 'methodsCounter', |
| 38 | + 'methodsCounterByIp', |
| 39 | + 'totalMessageCounter', |
| 40 | + 'totalOpenedConnections', |
| 41 | + 'totalClosedConnections', |
| 42 | + ]; |
| 43 | + |
| 44 | + counterMetrics.forEach((metric) => { |
| 45 | + sinon.assert.calledWith(removeSingleMetricStub, WS_CONSTANTS[metric].name); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should initialize all histogram metrics', function () { |
| 50 | + const histogramMetrics = ['connectionDuration', 'messageDuration']; |
| 51 | + |
| 52 | + histogramMetrics.forEach((metric) => { |
| 53 | + sinon.assert.calledWith(removeSingleMetricStub, WS_CONSTANTS[metric].name); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should initialize all gauge metrics', function () { |
| 58 | + const gaugeMetrics = ['cpuUsageGauge', 'memoryUsageGauge']; |
| 59 | + |
| 60 | + gaugeMetrics.forEach((metric) => { |
| 61 | + sinon.assert.calledWith(removeSingleMetricStub, WS_CONSTANTS[metric].name); |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('getCounter', function () { |
| 67 | + it('should return methodsCounter', function () { |
| 68 | + const counter = wsMetricRegistry.getCounter('methodsCounter'); |
| 69 | + expect(counter).to.be.instanceOf(Counter); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return methodsCounterByIp', function () { |
| 73 | + const counter = wsMetricRegistry.getCounter('methodsCounterByIp'); |
| 74 | + expect(counter).to.be.instanceOf(Counter); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return totalMessageCounter', function () { |
| 78 | + const counter = wsMetricRegistry.getCounter('totalMessageCounter'); |
| 79 | + expect(counter).to.be.instanceOf(Counter); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return totalOpenedConnections', function () { |
| 83 | + const counter = wsMetricRegistry.getCounter('totalOpenedConnections'); |
| 84 | + expect(counter).to.be.instanceOf(Counter); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return totalClosedConnections', function () { |
| 88 | + const counter = wsMetricRegistry.getCounter('totalClosedConnections'); |
| 89 | + expect(counter).to.be.instanceOf(Counter); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should allow incrementing counters', function () { |
| 93 | + const methodsCounter = wsMetricRegistry.getCounter('methodsCounter'); |
| 94 | + |
| 95 | + expect(() => methodsCounter.inc()).to.not.throw(); |
| 96 | + expect(() => methodsCounter.labels('eth_call').inc()).to.not.throw(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('getHistogram', function () { |
| 101 | + it('should return connectionDuration histogram', function () { |
| 102 | + const histogram = wsMetricRegistry.getHistogram('connectionDuration'); |
| 103 | + expect(histogram).to.be.instanceOf(Histogram); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should return messageDuration histogram', function () { |
| 107 | + const histogram = wsMetricRegistry.getHistogram('messageDuration'); |
| 108 | + expect(histogram).to.be.instanceOf(Histogram); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should allow observing histogram values', function () { |
| 112 | + const connectionDuration = wsMetricRegistry.getHistogram('connectionDuration'); |
| 113 | + const messageDuration = wsMetricRegistry.getHistogram('messageDuration'); |
| 114 | + |
| 115 | + // These should not throw errors |
| 116 | + expect(() => connectionDuration.observe(1.5)).to.not.throw(); |
| 117 | + expect(() => messageDuration.labels('eth_call').observe(100)).to.not.throw(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('metric properties', function () { |
| 122 | + it('should have correct metric names from WS_CONSTANTS', function () { |
| 123 | + const methodsCounter = wsMetricRegistry.getCounter('methodsCounter'); |
| 124 | + const connectionDuration = wsMetricRegistry.getHistogram('connectionDuration'); |
| 125 | + |
| 126 | + expect((methodsCounter as any).name).to.equal(WS_CONSTANTS.methodsCounter.name); |
| 127 | + expect((connectionDuration as any).name).to.equal(WS_CONSTANTS.connectionDuration.name); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should have correct buckets for histograms', function () { |
| 131 | + const connectionDuration = wsMetricRegistry.getHistogram('connectionDuration'); |
| 132 | + const messageDuration = wsMetricRegistry.getHistogram('messageDuration'); |
| 133 | + |
| 134 | + // Check that buckets are properly configured |
| 135 | + expect((connectionDuration as any).buckets).to.deep.equal(WS_CONSTANTS.connectionDuration.buckets); |
| 136 | + expect((messageDuration as any).buckets).to.deep.equal(WS_CONSTANTS.messageDuration.buckets); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should have correct label names for metrics with labels', function () { |
| 140 | + const methodsCounter = wsMetricRegistry.getCounter('methodsCounter'); |
| 141 | + const methodsCounterByIp = wsMetricRegistry.getCounter('methodsCounterByIp'); |
| 142 | + |
| 143 | + expect((methodsCounter as any).labelNames).to.deep.equal(WS_CONSTANTS.methodsCounter.labelNames); |
| 144 | + expect((methodsCounterByIp as any).labelNames).to.deep.equal(WS_CONSTANTS.methodsCounterByIp.labelNames); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('error handling', function () { |
| 149 | + it('should handle registry errors gracefully', function () { |
| 150 | + const faultyRegistry = new Registry(); |
| 151 | + sinon.stub(faultyRegistry, 'removeSingleMetric').throws(new Error('Registry error')); |
| 152 | + |
| 153 | + expect(() => { |
| 154 | + new WsMetricRegistry(faultyRegistry); |
| 155 | + }).to.throw('Registry error'); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments