Skip to content

Commit

Permalink
Merge pull request #4 from pusher/filter-old-metrics
Browse files Browse the repository at this point in the history
Filter old metrics using maxMetricAgeMs
  • Loading branch information
andrasq authored Apr 20, 2023
2 parents f8e56cd + 4be9aa6 commit a8502a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
20 changes: 15 additions & 5 deletions lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ Gateway.prototype.reportMetrics = function reportMetrics( ) {
samples = samples.splice(0);

// discard samples older than the configured cutoff
if (this.maxMetricAgeMs >= 0) {
var now = this.getTimestamp();
var oldestKeepTs = now - this.maxMetricAgeMs;
const now = this.getTimestamp();
const oldestKeepTs = this.maxMetricAgeMs >= 0 ? (now - this.maxMetricAgeMs) : 0;
const filterOldMetrics = this.maxMetricAgeMs >= 0;
if (filterOldMetrics) {
for (i=0; i<samples.length && samples[i].ts < oldestKeepTs; i++) ;
if (i > 0) {
Gateway.trace("discarding %d samples older than %d ms, from %d to %d",
Expand All @@ -210,10 +211,19 @@ Gateway.prototype.reportMetrics = function reportMetrics( ) {

for (i=0; i<averages.length; i++) averages[i].value /= averages[i].count;

// never omit any known metrics, report the previous value if no change
// Don't omit any known metrics, report the previous value if no change
// except for metrics older than the configured cutoff.
// This allows /metrics to be called more frequently than the /push interval.
var allValues = {};
for (id in this.previousValues) allValues[id] = this.previousValues[id];
for (id in this.previousValues) {
if (!filterOldMetrics || this.previousValues[id].ts >= oldestKeepTs) {
allValues[id] = this.previousValues[id];
}
}
const discardedMetrics = Object.keys(this.previousValues).length - Object.keys(allValues).length;
if (discardedMetrics > 0) {
Gateway.trace("discarding %d metrics older than %d ms", discardedMetrics, this.maxMetricAgeMs);
}
for (id in map) allValues[id] = map[id];
this.previousValues = allValues;

Expand Down

0 comments on commit a8502a6

Please sign in to comment.