Skip to content

Commit

Permalink
feat: Use options object for all configuration (nodeshift#19)
Browse files Browse the repository at this point in the history
* BREAKING CHANGE: Options object is now used to configure custom registry and initial circuits

* docs: Updates and adds documentation for options object configuration
  • Loading branch information
HarryEMartland authored Apr 22, 2020
1 parent 4f3c258 commit b353a59
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Example:
const c2 = new CircuitBreaker(someOtherfunction);

// Provide them to the constructor
const prometheus = new PrometheusMetrics([c1, c2]);
const prometheus = new PrometheusMetrics({ circuits: [c1, c2] });

//...
// Provide other circuit breaker later
Expand Down Expand Up @@ -58,6 +58,18 @@ const registry = new Registry();

// create a circuit
const circuit = new CircuitBreaker(functionThatMightFail);
const metrics = new PrometheusMetrics(circuit, registry)
const metrics = new PrometheusMetrics({ circuits: [circuit], registry: registry })
```

## Options
The `PrometheusMetrics` constructor takes an options object as detailed below.

```js
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|
15 changes: 5 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ const client = require('prom-client');
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels

class PrometheusMetrics {
constructor (circuits, registry) {
if (circuits instanceof client.Registry) {
registry = circuits;
circuits = undefined;
}

this._registry = registry || client.register;
constructor (options = {}) {
this._registry = options.registry || client.register;
this._client = client;
this._counter = new this._client.Counter({
name: `circuit`,
Expand All @@ -33,13 +28,13 @@ class PrometheusMetrics {
labelNames: ['name', 'event']
});

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

if (circuits) {
this.add(circuits);
if (options.circuits) {
this.add(options.circuits);
}
}

Expand Down
26 changes: 16 additions & 10 deletions test/prometheus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('The factory function accept no parameter', 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);
const prometheus = new PrometheusMetrics({ circuits: c1 });
await c1.fire(1);
t.equal(c1.name, 'fred');
t.ok(/circuit.*fred/.test(prometheus.metrics));
Expand All @@ -47,7 +47,7 @@ test('The factory function provides access to metrics for all circuits',
t.plan(6);
const c1 = new CircuitBreaker(passFail, { name: 'fred' });
const c2 = new CircuitBreaker(passFail, { name: 'bob' });
const prometheus = new PrometheusMetrics([c1, c2]);
const prometheus = new PrometheusMetrics({ circuits: [c1, c2] });
await c1.fire(1);
await c2.fire(1);
t.equal(c1.name, 'fred');
Expand All @@ -69,7 +69,10 @@ test('The factory function uses a custom prom-client registry', async t => {
const c2 = new CircuitBreaker(passFail, {
name: 'bob'
});
const prometheus = new PrometheusMetrics([c1, c2], registry);
const prometheus = new PrometheusMetrics({
circuits: [c1, c2],
registry: registry
});
await c1.fire(1);
await c2.fire(1);
t.equal(c1.name, 'fred');
Expand Down Expand Up @@ -105,7 +108,7 @@ test('The add function provides access to metrics for all circuits',
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]);
const prometheus = new PrometheusMetrics({ circuits: [c1] });
prometheus.add([c2, c3]);
await c1.fire(1);
await c2.fire(1);
Expand Down Expand Up @@ -137,7 +140,7 @@ test('Circuit fire/success/failure are counted', t => {
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]);
const prometheus = new PrometheusMetrics({ circuits: [circuit] });
t.plan(3);
circuit.fire(1)
.then(_ => circuit.fire(-1))
Expand All @@ -156,7 +159,7 @@ test('Metrics are enabled for all circuit events', t => {
circuit.on = (event, callback) => {
callback(null, 1);
};
const prometheus = new PrometheusMetrics([circuit]);
const prometheus = new PrometheusMetrics({ circuits: [circuit] });
const metrics = prometheus.metrics;
t.plan(circuit.eventNames().length);
for (const name of circuit.eventNames()) {
Expand All @@ -169,7 +172,7 @@ test('Metrics are enabled for all circuit events', t => {

test('Default prometheus metrics are enabled', t => {
const circuit = new CircuitBreaker(passFail);
const prometheus = new PrometheusMetrics([circuit]);
const prometheus = new PrometheusMetrics({ circuits: [circuit] });
const metrics = prometheus.metrics;
const names = [
'process_cpu_seconds_total',
Expand All @@ -192,7 +195,10 @@ test('Default prometheus metrics are enabled', t => {
test('Should not add default metrics to custom registry', t => {
const registry = new Registry();
const circuit = new CircuitBreaker(passFail);
const prometheus = new PrometheusMetrics([circuit], registry);
const prometheus = new PrometheusMetrics({
circuits: [circuit],
registry: registry
});
const metrics = prometheus.metrics;
const names = [
'process_cpu_seconds_total',
Expand All @@ -214,7 +220,7 @@ test('Should not add default metrics to custom registry', t => {

test('Default prometheus metrics are enabled without circuit', t => {
const registry = new Registry();
const prometheus = new PrometheusMetrics(registry);
const prometheus = new PrometheusMetrics({ registry: registry });
const metrics = prometheus.metrics;
const names = [
'nodejs_eventloop_lag',
Expand Down Expand Up @@ -246,7 +252,7 @@ test('Default prometheus metrics are enabled without circuit', t => {

test('Node.js specific metrics are enabled', t => {
const circuit = new CircuitBreaker(passFail);
const prometheus = new PrometheusMetrics([circuit]);
const prometheus = new PrometheusMetrics({ circuits: [circuit] });
const metrics = prometheus.metrics;
const names = [
'nodejs_eventloop_lag',
Expand Down

0 comments on commit b353a59

Please sign in to comment.