Skip to content

Commit

Permalink
feat: Providing metric prefix for circuit metrics (nodeshift#32)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
danilodeLuca and Danilo De Luca authored Sep 2, 2020
1 parent ff0ef7b commit 0d85a86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ new PrometheusMetrics(options)
|--------------------------|------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
|`circuits` |A list or individual circuit breaker to create metrics for |No circuits |
|`registry` |An existing registry to use for prometheus metrics |`undefined` - The default prometheus registry will be used and default system metrics will be collected|
|`exposePerformanceMetrics`|Measure the performance of breakers and report them through the registry|true |
|`exposePerformanceMetrics`|Measure the performance of breakers and report them through the registry|true |
|`metricPrefix`|Prefix for circuit breakers metrics name|any string |
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ const client = require('prom-client');
class PrometheusMetrics {
constructor (options = {}) {
this._registry = options.registry || client.register;
this._metricPrefix = options.metricPrefix || ``;
this._client = client;
this._options = options;
this._counter = new this._client.Counter({
name: `circuit`,
name: `${this._metricPrefix}circuit`,
help: `A count of all circuit' events`,
registers: [this._registry],
labelNames: ['name', 'event']
});

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

if (!options.registry) {
this.interval = this._client
.collectDefaultMetrics({ prefix: 'opossum_', timeout: 5000 });
.collectDefaultMetrics({
prefix: `${this._metricPrefix}opossum_`,
timeout: 5000
});
}

if (options.circuits) {
Expand Down
44 changes: 33 additions & 11 deletions test/prometheus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ test('The factory function accept no parameter', t => {
t.end();
});

test('The factory function takes an object instead of just an Array', async t => {
t.plan(3);
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
const prometheus = new PrometheusMetrics({ circuits: c1 });
await c1.fire(1);
t.equal(c1.name, 'fred');
t.ok(/circuit.*fred/.test(prometheus.metrics));
t.ok(/circuit_perf.*fred/.test(prometheus.metrics));
prometheus.clear();
t.end();
});
test('The factory function takes an object instead of just an Array',
async t => {
t.plan(3);
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
const prometheus = new PrometheusMetrics({ circuits: c1 });
await c1.fire(1);
t.equal(c1.name, 'fred');
t.ok(/circuit.*fred/.test(prometheus.metrics));
t.ok(/circuit_perf.*fred/.test(prometheus.metrics));
prometheus.clear();
t.end();
});

test('The factory function provides access to metrics for all circuits',
async t => {
Expand Down Expand Up @@ -319,3 +320,24 @@ test('Performance metrics are created when enabled in options',
prometheus.clear();
t.end();
});

test('The factory function provides metric prefix and it append to metric name',
async t => {
t.plan(6);
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
const c2 = new CircuitBreaker(passFail, { name: 'bob' });
const prometheus = new PrometheusMetrics({
circuits: [c1, c2],
metricPrefix: 'some_prefix_'
});
await c1.fire(1);
await c2.fire(1);
t.equal(c1.name, 'fred');
t.equal(c2.name, 'bob');
t.ok(/some_prefix_circuit.*fred/.test(prometheus.metrics));
t.ok(/some_prefix_circuit_perf.*fred/.test(prometheus.metrics));
t.ok(/some_prefix_circuit.*bob/.test(prometheus.metrics));
t.ok(/some_prefix_circuit_perf.*bob/.test(prometheus.metrics));
prometheus.clear();
t.end();
});

0 comments on commit 0d85a86

Please sign in to comment.