Skip to content

Commit 0d85a86

Browse files
danilodeLucaDanilo De Luca
and
Danilo De Luca
authored
feat: Providing metric prefix for circuit metrics (#32)
* This adds a new option `metricPrefix` to add a new base prefix to the generated metrics Co-authored-by: Danilo De Luca <danilo.deluca@ifood.com.br>
1 parent ff0ef7b commit 0d85a86

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ new PrometheusMetrics(options)
7373
|--------------------------|------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
7474
|`circuits` |A list or individual circuit breaker to create metrics for |No circuits |
7575
|`registry` |An existing registry to use for prometheus metrics |`undefined` - The default prometheus registry will be used and default system metrics will be collected|
76-
|`exposePerformanceMetrics`|Measure the performance of breakers and report them through the registry|true |
76+
|`exposePerformanceMetrics`|Measure the performance of breakers and report them through the registry|true |
77+
|`metricPrefix`|Prefix for circuit breakers metrics name|any string |

index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ const client = require('prom-client');
1313
class PrometheusMetrics {
1414
constructor (options = {}) {
1515
this._registry = options.registry || client.register;
16+
this._metricPrefix = options.metricPrefix || ``;
1617
this._client = client;
1718
this._options = options;
1819
this._counter = new this._client.Counter({
19-
name: `circuit`,
20+
name: `${this._metricPrefix}circuit`,
2021
help: `A count of all circuit' events`,
2122
registers: [this._registry],
2223
labelNames: ['name', 'event']
2324
});
2425

2526
if (this.exposePerformanceMetrics()) {
2627
this._summary = new this._client.Summary({
27-
name: `circuit_perf`,
28+
name: `${this._metricPrefix}circuit_perf`,
2829
help: `A summary of all circuit's events`,
2930
registers: [this._registry],
3031
labelNames: ['name', 'event']
@@ -33,7 +34,10 @@ class PrometheusMetrics {
3334

3435
if (!options.registry) {
3536
this.interval = this._client
36-
.collectDefaultMetrics({ prefix: 'opossum_', timeout: 5000 });
37+
.collectDefaultMetrics({
38+
prefix: `${this._metricPrefix}opossum_`,
39+
timeout: 5000
40+
});
3741
}
3842

3943
if (options.circuits) {

test/prometheus-test.js

+33-11
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ test('The factory function accept no parameter', t => {
3030
t.end();
3131
});
3232

33-
test('The factory function takes an object instead of just an Array', async t => {
34-
t.plan(3);
35-
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
36-
const prometheus = new PrometheusMetrics({ circuits: c1 });
37-
await c1.fire(1);
38-
t.equal(c1.name, 'fred');
39-
t.ok(/circuit.*fred/.test(prometheus.metrics));
40-
t.ok(/circuit_perf.*fred/.test(prometheus.metrics));
41-
prometheus.clear();
42-
t.end();
43-
});
33+
test('The factory function takes an object instead of just an Array',
34+
async t => {
35+
t.plan(3);
36+
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
37+
const prometheus = new PrometheusMetrics({ circuits: c1 });
38+
await c1.fire(1);
39+
t.equal(c1.name, 'fred');
40+
t.ok(/circuit.*fred/.test(prometheus.metrics));
41+
t.ok(/circuit_perf.*fred/.test(prometheus.metrics));
42+
prometheus.clear();
43+
t.end();
44+
});
4445

4546
test('The factory function provides access to metrics for all circuits',
4647
async t => {
@@ -319,3 +320,24 @@ test('Performance metrics are created when enabled in options',
319320
prometheus.clear();
320321
t.end();
321322
});
323+
324+
test('The factory function provides metric prefix and it append to metric name',
325+
async t => {
326+
t.plan(6);
327+
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
328+
const c2 = new CircuitBreaker(passFail, { name: 'bob' });
329+
const prometheus = new PrometheusMetrics({
330+
circuits: [c1, c2],
331+
metricPrefix: 'some_prefix_'
332+
});
333+
await c1.fire(1);
334+
await c2.fire(1);
335+
t.equal(c1.name, 'fred');
336+
t.equal(c2.name, 'bob');
337+
t.ok(/some_prefix_circuit.*fred/.test(prometheus.metrics));
338+
t.ok(/some_prefix_circuit_perf.*fred/.test(prometheus.metrics));
339+
t.ok(/some_prefix_circuit.*bob/.test(prometheus.metrics));
340+
t.ok(/some_prefix_circuit_perf.*bob/.test(prometheus.metrics));
341+
prometheus.clear();
342+
t.end();
343+
});

0 commit comments

Comments
 (0)