From 67e61828b2aa5553be1b7858c384d083a98efb2e Mon Sep 17 00:00:00 2001 From: "Lenehan, John (ELS-DAY)" Date: Mon, 16 Apr 2018 13:13:58 -0400 Subject: [PATCH] Update for multi-namespace --- lib/aws-cloudwatch-statsd-backend.js | 37 ++++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/aws-cloudwatch-statsd-backend.js b/lib/aws-cloudwatch-statsd-backend.js index f0de42f..7e6bc6e 100644 --- a/lib/aws-cloudwatch-statsd-backend.js +++ b/lib/aws-cloudwatch-statsd-backend.js @@ -113,7 +113,7 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) { var sets = metrics.sets; // put all currently accumulated counter metrics into an array - var currentCounterMetrics = []; + var currentNamespaces = {}; var namespace = "AwsCloudWatchStatsdBackend"; for (key in counters) { if (key.indexOf('statsd.') == 0) @@ -126,19 +126,21 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) { var names = this.config.processKeyForNamespace ? this.processKey(key) : {}; namespace = this.config.namespace || names.namespace || "AwsCloudWatchStatsdBackend"; var metricName = this.config.metricName || names.metricName || key; + if (!currentNamespaces.hasOwnProperty(namespace)) currentNamespaces[namespace] = [] - currentCounterMetrics.push({ + currentNamespaces[namespace].push({ MetricName: metricName, Unit: 'Count', Timestamp: new Date(timestamp * 1000).toISOString(), Value: counters[key] }); } - - this.batchSend(currentCounterMetrics, namespace); + for (namespace in currentNamespaces){ + this.batchSend(currentNamespaces[namespace], namespace); + } // put all currently accumulated timer metrics into an array - var currentTimerMetrics = []; + currentNamespaces = {}; for (key in timers) { if (timers[key].length > 0) { @@ -172,8 +174,9 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) { var names = this.config.processKeyForNamespace ? this.processKey(key) : {}; namespace = this.config.namespace || names.namespace || "AwsCloudWatchStatsdBackend"; var metricName = this.config.metricName || names.metricName || key; + if (!currentNamespaces.hasOwnProperty(namespace)) currentNamespaces[namespace] = [] - currentTimerMetrics.push({ + currentNamespaces[namespace].push({ MetricName: metricName, Unit: 'Milliseconds', Timestamp: new Date(timestamp * 1000).toISOString(), @@ -187,10 +190,12 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) { } } - this.batchSend(currentTimerMetrics, namespace); + for (namespace in currentNamespaces){ + this.batchSend(currentNamespaces[namespace], namespace); + } // put all currently accumulated gauge metrics into an array - var currentGaugeMetrics = []; + currentNamespaces = {}; for (key in gauges) { if (this.isBlacklisted(key)) { @@ -200,8 +205,9 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) { var names = this.config.processKeyForNamespace ? this.processKey(key) : {}; namespace = this.config.namespace || names.namespace || "AwsCloudWatchStatsdBackend"; var metricName = this.config.metricName || names.metricName || key; + if (!currentNamespaces.hasOwnProperty(namespace)) currentNamespaces[namespace] = [] - currentGaugeMetrics.push({ + currentNamespaces[namespace].push({ MetricName: metricName, Unit: 'None', Timestamp: new Date(timestamp * 1000).toISOString(), @@ -209,10 +215,12 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) { }); } - this.batchSend(currentGaugeMetrics, namespace); + for (namespace in currentNamespaces){ + this.batchSend(currentNamespaces[namespace], namespace); + } // put all currently accumulated set metrics into an array - var currentSetMetrics = []; + currentNamespaces = {}; for (key in sets) { if (this.isBlacklisted(key)) { @@ -222,8 +230,9 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) { var names = this.config.processKeyForNamespace ? this.processKey(key) : {}; namespace = this.config.namespace || names.namespace || "AwsCloudWatchStatsdBackend"; var metricName = this.config.metricName || names.metricName || key; + if (!currentNamespaces.hasOwnProperty(namespace)) currentNamespaces[namespace] = [] - currentSetMetrics.push({ + currentNamespaces[namespace].push({ MetricName: metricName, Unit: 'None', Timestamp: new Date(timestamp * 1000).toISOString(), @@ -231,7 +240,9 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) { }); } - this.batchSend(currentSetMetrics, namespace); + for (namespace in currentNamespaces){ + this.batchSend(currentNamespaces[namespace], namespace); + } }; exports.init = function(startupTime, config, events) {