Skip to content

Commit

Permalink
feat: adds option for performance metrics so they can be disabled, de…
Browse files Browse the repository at this point in the history
…fault is enabled (nodeshift#20)

* adds the exposePerformanceMetrics option
  • Loading branch information
HarryEMartland authored Apr 23, 2020
1 parent b353a59 commit 2437eca
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ const options = {};
new PrometheusMetrics(options)
```

|Name |Description |Default |
|----------|----------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
|`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|
|Name |Description |Default |
|--------------------------|------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
|`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 |
24 changes: 17 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ class PrometheusMetrics {
constructor (options = {}) {
this._registry = options.registry || client.register;
this._client = client;
this._options = options;
this._counter = new this._client.Counter({
name: `circuit`,
help: `A count of all circuit' events`,
registers: [this._registry],
labelNames: ['name', 'event']
});

this._summary = new this._client.Summary({
name: `circuit_perf`,
help: `A summary of all circuit's events`,
registers: [this._registry],
labelNames: ['name', 'event']
});
if (this.exposePerformanceMetrics()) {
this._summary = new this._client.Summary({
name: `circuit_perf`,
help: `A summary of all circuit's events`,
registers: [this._registry],
labelNames: ['name', 'event']
});
}

if (!options.registry) {
this.interval = this._client
Expand All @@ -38,6 +41,12 @@ class PrometheusMetrics {
}
}

exposePerformanceMetrics () {
return this._options === undefined ||
this._options.exposePerformanceMetrics === undefined ||
this._options.exposePerformanceMetrics;
}

add (circuits) {
if (!circuits) {
return;
Expand All @@ -50,7 +59,8 @@ class PrometheusMetrics {
this._counter.labels(circuit.name, eventName).inc();
});

if (eventName === 'success' || eventName === 'failure') {
if (this.exposePerformanceMetrics() &&
(eventName === 'success' || eventName === 'failure')) {
// not the timeout event because runtime == timeout
circuit.on(eventName, (result, runTime) => {
this._summary.labels(circuit.name, eventName).observe(runTime);
Expand Down
45 changes: 45 additions & 0 deletions test/prometheus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,48 @@ test('Node.js specific metrics are enabled', t => {
prometheus.clear();
t.end();
});

test('Performance metrics are not created when disabled',
async t => {
t.plan(3);
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
const prometheus = new PrometheusMetrics({
circuits: [c1],
exposePerformanceMetrics: false
});
await c1.fire(1);
t.equal(c1.name, 'fred');
t.ok(/circuit.*fred/.test(prometheus.metrics));
t.notOk(/circuit_perf.*fred/.test(prometheus.metrics));
prometheus.clear();
t.end();
});

test('Performance metrics are created when not configured in options',
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('Performance metrics are created when enabled in options',
async t => {
t.plan(3);
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
const prometheus = new PrometheusMetrics({
circuits: [c1],
exposePerformanceMetrics: true
});
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();
});

0 comments on commit 2437eca

Please sign in to comment.