From d537bf1f23038b9e202312e2cf6ede2d36ad978d Mon Sep 17 00:00:00 2001 From: Meena Alfons Date: Sat, 15 Apr 2023 15:41:58 +0200 Subject: [PATCH 1/2] Filter old metrics using maxMetricAgeMs --- lib/gateway.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/gateway.js b/lib/gateway.js index 435331b..df41a29 100644 --- a/lib/gateway.js +++ b/lib/gateway.js @@ -210,10 +210,22 @@ Gateway.prototype.reportMetrics = function reportMetrics( ) { for (i=0; i= 0 ? (now - this.maxMetricAgeMs) : 0; + const filterOldMetrics = this.maxMetricAgeMs >= 0; + 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; From 4be9aa61d7643555c1d56c4ae2b894570ed68fcb Mon Sep 17 00:00:00 2001 From: Meena Alfons Date: Sat, 15 Apr 2023 15:45:14 +0200 Subject: [PATCH 2/2] Filter old metrics using maxMetricAgeMs --- .gitignore | 1 + lib/gateway.js | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/lib/gateway.js b/lib/gateway.js index df41a29..9b2b851 100644 --- a/lib/gateway.js +++ b/lib/gateway.js @@ -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 0) { Gateway.trace("discarding %d samples older than %d ms, from %d to %d", @@ -214,9 +215,6 @@ Gateway.prototype.reportMetrics = function reportMetrics( ) { // except for metrics older than the configured cutoff. // This allows /metrics to be called more frequently than the /push interval. var allValues = {}; - const now = this.getTimestamp(); - const oldestKeepTs = this.maxMetricAgeMs >= 0 ? (now - this.maxMetricAgeMs) : 0; - const filterOldMetrics = this.maxMetricAgeMs >= 0; for (id in this.previousValues) { if (!filterOldMetrics || this.previousValues[id].ts >= oldestKeepTs) { allValues[id] = this.previousValues[id];