Skip to content

Commit

Permalink
feat: add option to disable stats snapshots (#799)
Browse files Browse the repository at this point in the history
* feat: add option to disable snapshots

---------

Co-authored-by: Gautam Jethwani <gjethwani@atlassian.com>
  • Loading branch information
gjethwani and Gautam Jethwani committed Jul 31, 2023
1 parent 3dffb78 commit a9c5935
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class CircuitBreaker extends EventEmitter {
this.options.cacheTTL = options.cacheTTL ?? 0;
this.options.cacheGetKey = options.cacheGetKey ??
((...args) => JSON.stringify(args));
this.options.enableSnapshots = options.enableSnapshots !== false;

// Set default cache transport if not provided
if (this.options.cache) {
Expand Down
19 changes: 13 additions & 6 deletions lib/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Status extends EventEmitter {
this.rollingPercentilesEnabled =
options.rollingPercentilesEnabled !== false;

// Default this value to true
this.enableSnapshots = options.enableSnapshots !== false;

// prime the window with buckets
for (let i = 0; i < this[BUCKETS]; i++) this[WINDOW][i] = bucket();

Expand All @@ -80,11 +83,13 @@ class Status extends EventEmitter {
* @event Status#snapshot
* @type {Object}
*/
this[SNAPSHOT_INTERVAL] = setInterval(
_ => this.emit('snapshot', this.stats),
bucketInterval);
if (typeof this[SNAPSHOT_INTERVAL].unref === 'function') {
this[SNAPSHOT_INTERVAL].unref();
if (this.enableSnapshots) {
this[SNAPSHOT_INTERVAL] = setInterval(
_ => this.emit('snapshot', this.stats),
bucketInterval);
if (typeof this[SNAPSHOT_INTERVAL].unref === 'function') {
this[SNAPSHOT_INTERVAL].unref();
}
}

if (options.stats) {
Expand Down Expand Up @@ -169,7 +174,9 @@ class Status extends EventEmitter {
shutdown () {
this.removeAllListeners();
clearInterval(this[BUCKET_INTERVAL]);
clearInterval(this[SNAPSHOT_INTERVAL]);
if (this.enableSnapshots) {
clearInterval(this[SNAPSHOT_INTERVAL]);
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions test/status-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,40 @@ test('CircuitBreaker status - import stats,but not a status object', t => {
t.fail();
}
});

test('CircuitBreaker status - enableSnapshots defaults to true', t => {
t.plan(1);

const breaker = new CircuitBreaker(passFail);

t.equal(breaker.status.enableSnapshots, true, 'enableSnapshots defaults to true');

breaker.shutdown();
t.end();
});

test('CircuitBreaker status - enableSnapshots is true in Status when set to true', t => {
t.plan(1);

const breaker = new CircuitBreaker(passFail, {
enableSnapshots: true
});

t.equal(breaker.status.enableSnapshots, true, 'enableSnapshots propagates as true');

breaker.shutdown();
t.end();
});

test('CircuitBreaker status - enableSnapshots is false in Status when set to false', t => {
t.plan(1);

const breaker = new CircuitBreaker(passFail, {
enableSnapshots: false
});

t.equal(breaker.status.enableSnapshots, false, 'enableSnapshots propagates as false');

breaker.shutdown();
t.end();
});

0 comments on commit a9c5935

Please sign in to comment.