Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing metric prefix for circuit metrics #32

Merged
merged 2 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});