opossum-prometheus
Advanced tools
Comparing version 0.0.4 to 0.1.0
@@ -5,2 +5,16 @@ # Changelog | ||
## [0.1.0](https://github.com/lholmquist/opossum-prometheus/compare/v0.0.4...v0.1.0) (2020-03-13) | ||
### ⚠ BREAKING CHANGES | ||
* Changes event metrics to use labels rather than seporate metrics | ||
* Changes metrics to use labels for circuit name rather than seporate metrics | ||
Co-authored-by: martlandh <HarryMartland@Rentalcars.com> | ||
### Features | ||
* Change event name to be a label ([#15](https://github.com/lholmquist/opossum-prometheus/issues/15)) ([075b033](https://github.com/lholmquist/opossum-prometheus/commit/075b033)) | ||
### [0.0.4](https://github.com/lholmquist/opossum-prometheus/compare/v0.0.3...v0.0.4) (2019-10-02) | ||
@@ -7,0 +21,0 @@ |
38
index.js
@@ -12,5 +12,2 @@ 'use strict'; | ||
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels | ||
function normalizePrefix (prefixName) { | ||
return `circuit_${prefixName.replace(/[ |-]/g, '_')}_`; | ||
} | ||
@@ -26,5 +23,16 @@ class PrometheusMetrics { | ||
this._client = client; | ||
this.counters = []; | ||
this.summaries = []; | ||
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 (!registry) { | ||
@@ -45,28 +53,14 @@ this.interval = this._client | ||
circuits = Array.isArray(circuits) ? circuits : [circuits]; | ||
let prefix; | ||
circuits.forEach(circuit => { | ||
prefix = normalizePrefix(circuit.name); | ||
for (const eventName of circuit.eventNames()) { | ||
const counter = new this._client.Counter({ | ||
name: `${prefix}${eventName}`, | ||
help: `A count of the ${circuit.name} circuit's ${eventName} event`, | ||
registers: [this._registry] | ||
}); | ||
circuit.on(eventName, _ => { | ||
counter.inc(); | ||
this._counter.labels(circuit.name, eventName).inc(); | ||
}); | ||
this.counters.push(counter); | ||
if (eventName === 'success' || eventName === 'failure') { | ||
// not the timeout event because runtime == timeout | ||
const summary = new this._client.Summary({ | ||
name: `${prefix}${eventName}_perf`, | ||
help: | ||
`A summary of the ${circuit.name} circuit's ${eventName} event`, | ||
registers: [this._registry] | ||
}); | ||
circuit.on(eventName, (result, runTime) => { | ||
summary.observe(runTime); | ||
this._summary.labels(circuit.name, eventName).observe(runTime); | ||
}); | ||
this.summaries.push(summary); | ||
} | ||
@@ -73,0 +67,0 @@ } |
{ | ||
"name": "opossum-prometheus", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Prometheus metrics for opossum circuit breaker", | ||
@@ -30,4 +30,4 @@ "main": "index.js", | ||
"codacy-coverage": "^3.4.0", | ||
"nyc": "^14.1.1", | ||
"opossum": "^4.0.0", | ||
"nyc": "^15.0.0", | ||
"opossum": "^5.0.0", | ||
"standard-version": "^7.0.0", | ||
@@ -39,4 +39,4 @@ "standardx": "^5.0.0", | ||
"dependencies": { | ||
"prom-client": "^11.5.3" | ||
"prom-client": "^12.0.0" | ||
} | ||
} |
@@ -33,9 +33,10 @@ 'use strict'; | ||
test('The factory function takes an object instead of just an Array', t => { | ||
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(c1); | ||
await c1.fire(1); | ||
t.equal(c1.name, 'fred'); | ||
t.ok(/circuit_fred_/.test(prometheus.metrics)); | ||
t.ok(/circuit_fred_.*perf/.test(prometheus.metrics)); | ||
t.ok(/circuit.*fred/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*fred/.test(prometheus.metrics)); | ||
prometheus.clear(); | ||
@@ -45,18 +46,21 @@ t.end(); | ||
test('The factory function provides access to metrics for all circuits', t => { | ||
t.plan(6); | ||
const c1 = new CircuitBreaker(passFail, { name: 'fred' }); | ||
const c2 = new CircuitBreaker(passFail, { name: 'bob' }); | ||
const prometheus = new PrometheusMetrics([c1, c2]); | ||
t.equal(c1.name, 'fred'); | ||
t.equal(c2.name, 'bob'); | ||
t.ok(/circuit_fred_/.test(prometheus.metrics)); | ||
t.ok(/circuit_fred_.*perf/.test(prometheus.metrics)); | ||
t.ok(/circuit_bob_/.test(prometheus.metrics)); | ||
t.ok(/circuit_bob_.*perf/.test(prometheus.metrics)); | ||
prometheus.clear(); | ||
t.end(); | ||
}); | ||
test('The factory function provides access to metrics for all circuits', | ||
async t => { | ||
t.plan(6); | ||
const c1 = new CircuitBreaker(passFail, { name: 'fred' }); | ||
const c2 = new CircuitBreaker(passFail, { name: 'bob' }); | ||
const prometheus = new PrometheusMetrics([c1, c2]); | ||
await c1.fire(1); | ||
await c2.fire(1); | ||
t.equal(c1.name, 'fred'); | ||
t.equal(c2.name, 'bob'); | ||
t.ok(/circuit.*fred/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*fred/.test(prometheus.metrics)); | ||
t.ok(/circuit.*bob/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*bob/.test(prometheus.metrics)); | ||
prometheus.clear(); | ||
t.end(); | ||
}); | ||
test('The factory function uses a custom prom-client registry', t => { | ||
test('The factory function uses a custom prom-client registry', async t => { | ||
t.plan(10); | ||
@@ -71,12 +75,14 @@ const registry = new Registry(); | ||
const prometheus = new PrometheusMetrics([c1, c2], registry); | ||
await c1.fire(1); | ||
await c2.fire(1); | ||
t.equal(c1.name, 'fred'); | ||
t.equal(c2.name, 'bob'); | ||
t.ok(/circuit_fred_/.test(registry.metrics())); | ||
t.ok(/circuit_fred_.*perf/.test(registry.metrics())); | ||
t.ok(/circuit_bob_/.test(registry.metrics())); | ||
t.ok(/circuit_bob_.*perf/.test(registry.metrics())); | ||
t.ok(/circuit_fred_/.test(prometheus.metrics)); | ||
t.ok(/circuit_fred_.*perf/.test(prometheus.metrics)); | ||
t.ok(/circuit_bob_/.test(prometheus.metrics)); | ||
t.ok(/circuit_bob_.*perf/.test(prometheus.metrics)); | ||
t.ok(/circuit.*fred/.test(registry.metrics())); | ||
t.ok(/circuit_perf.*fred/.test(registry.metrics())); | ||
t.ok(/circuit.*bob/.test(registry.metrics())); | ||
t.ok(/circuit_perf.*bob/.test(registry.metrics())); | ||
t.ok(/circuit.*bob/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*fred/.test(prometheus.metrics)); | ||
t.ok(/circuit.*bob/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*bob/.test(prometheus.metrics)); | ||
prometheus.clear(); | ||
@@ -86,3 +92,3 @@ t.end(); | ||
test('The add function takes an object instead of just an Array', t => { | ||
test('The add function takes an object instead of just an Array', async t => { | ||
t.plan(3); | ||
@@ -92,5 +98,6 @@ const c1 = new CircuitBreaker(passFail, { name: 'fred' }); | ||
prometheus.add(c1); | ||
await c1.fire(1); | ||
t.equal(c1.name, 'fred'); | ||
t.ok(/circuit_fred_/.test(prometheus.metrics)); | ||
t.ok(/circuit_fred_.*perf/.test(prometheus.metrics)); | ||
t.ok(/circuit.*fred.*/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*fred.*/.test(prometheus.metrics)); | ||
prometheus.clear(); | ||
@@ -100,21 +107,25 @@ t.end(); | ||
test('The add function provides access to metrics for all circuits', t => { | ||
t.plan(9); | ||
const c1 = new CircuitBreaker(passFail, { name: 'fred' }); | ||
const c2 = new CircuitBreaker(passFail, { name: 'bob' }); | ||
const c3 = new CircuitBreaker(passFail, { name: 'foo' }); | ||
const prometheus = new PrometheusMetrics([c1]); | ||
prometheus.add([c2, c3]); | ||
t.equal(c1.name, 'fred'); | ||
t.equal(c2.name, 'bob'); | ||
t.equal(c3.name, 'foo'); | ||
t.ok(/circuit_fred_/.test(prometheus.metrics)); | ||
t.ok(/circuit_fred_.*perf/.test(prometheus.metrics)); | ||
t.ok(/circuit_bob_/.test(prometheus.metrics)); | ||
t.ok(/circuit_bob_.*perf/.test(prometheus.metrics)); | ||
t.ok(/circuit_foo_/.test(prometheus.metrics)); | ||
t.ok(/circuit_foo_.*perf/.test(prometheus.metrics)); | ||
prometheus.clear(); | ||
t.end(); | ||
}); | ||
test('The add function provides access to metrics for all circuits', | ||
async t => { | ||
t.plan(9); | ||
const c1 = new CircuitBreaker(passFail, { name: 'fred' }); | ||
const c2 = new CircuitBreaker(passFail, { name: 'bob' }); | ||
const c3 = new CircuitBreaker(passFail, { name: 'foo' }); | ||
const prometheus = new PrometheusMetrics([c1]); | ||
prometheus.add([c2, c3]); | ||
await c1.fire(1); | ||
await c2.fire(1); | ||
await c3.fire(1); | ||
t.equal(c1.name, 'fred'); | ||
t.equal(c2.name, 'bob'); | ||
t.equal(c3.name, 'foo'); | ||
t.ok(/circuit.*fred/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*fred/.test(prometheus.metrics)); | ||
t.ok(/circuit.*bob/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*bob/.test(prometheus.metrics)); | ||
t.ok(/circuit.*foo/.test(prometheus.metrics)); | ||
t.ok(/circuit_perf.*foo/.test(prometheus.metrics)); | ||
prometheus.clear(); | ||
t.end(); | ||
}); | ||
@@ -125,3 +136,3 @@ test('The add function called without parameter do nothing', t => { | ||
prometheus.add(); | ||
t.notOk(/circuit_/.test(prometheus.metrics)); | ||
t.ok(/circuit/.test(prometheus.metrics)); | ||
prometheus.clear(); | ||
@@ -133,5 +144,5 @@ t.end(); | ||
const circuit = new CircuitBreaker(passFail); | ||
const fire = /circuit_passFail_fire 2/; | ||
const success = /circuit_passFail_success 1/; | ||
const failure = /circuit_passFail_failure 1/; | ||
const fire = /circuit\{name="passFail",event="fire"\} 2/; | ||
const success = /circuit\{name="passFail",event="success"\} 1/; | ||
const failure = /circuit\{name="passFail",event="failure"\} 1/; | ||
const prometheus = new PrometheusMetrics([circuit]); | ||
@@ -153,2 +164,5 @@ t.plan(3); | ||
const circuit = new CircuitBreaker(passFail); | ||
circuit.on = (event, callback) => { | ||
callback(null, 1); | ||
}; | ||
const prometheus = new PrometheusMetrics([circuit]); | ||
@@ -158,3 +172,3 @@ const metrics = prometheus.metrics; | ||
for (const name of circuit.eventNames()) { | ||
const match = new RegExp(`circuit_passFail_${name}`); | ||
const match = new RegExp(`circuit{name="passFail",event="${name}"}`); | ||
t.ok(match.test(metrics), name); | ||
@@ -161,0 +175,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21522
320
+ Addedprom-client@12.0.0(transitive)
- Removedprom-client@11.5.3(transitive)
Updatedprom-client@^12.0.0