Skip to content

Commit

Permalink
Merge pull request #4 from intellifylearning/optimize
Browse files Browse the repository at this point in the history
chunk up into 20 max
  • Loading branch information
Prashant Nayak authored Sep 17, 2016
2 parents 6b28252 + 42637ee commit 0613935
Showing 1 changed file with 39 additions and 60 deletions.
99 changes: 39 additions & 60 deletions lib/aws-cloudwatch-statsd-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@ CloudwatchBackend.prototype.processKey = function(key) {
};
};

CloudwatchBackend.prototype.chunk = function(arr, chunkSize) {

var groups = [],
i;
for (i = 0; i < arr.length; i += chunkSize) {
groups.push(arr.slice(i, i + chunkSize));
}
return groups;
};

CloudwatchBackend.prototype.batchSend = function(currentMetricsBatch, namespace) {

// send off the array (instead of one at a time)
if (currentMetricsBatch.length > 0) {

// Chunk into groups of 20
var chunkedGroups = this.chunk(currentMetricsBatch, 20);

for (var i = 0, len = chunkedGroups.length; i < len; i++) {
this.cloudwatch.putMetricData({
MetricData: chunkedGroups[i],
Namespace: namespace
}, function(err, data) {
if (err) {
// log an error
console.log(util.inspect(err));
} else {
// Success
// console.log(util.inspect(data));
}
});
}
}
};

CloudwatchBackend.prototype.flush = function(timestamp, metrics) {

console.log('Flushing metrics at ' + new Date(timestamp * 1000).toISOString());
Expand Down Expand Up @@ -80,21 +115,7 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) {
});
}

// send off the array (instead of one at a time)
if (currentCounterMetrics.length > 0) {
this.cloudwatch.putMetricData({
MetricData: currentCounterMetrics,
Namespace: namespace
}, function(err, data) {
if (err) {
// log an error
console.log(util.inspect(err));
} else {
// Success
// console.log(util.inspect(data));
}
});
}
this.batchSend(currentCounterMetrics, namespace);

// put all currently accumulated timer metrics into an array
var currentTimerMetrics = [];
Expand Down Expand Up @@ -147,21 +168,7 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) {
}
}

// send off the array (instead of one at a time)
if (currentTimerMetrics.length > 0) {
this.cloudwatch.putMetricData({
MetricData: currentTimerMetrics,
Namespace: namespace
}, function(err, data) {
if (err) {
// log an error
console.log(util.inspect(err));
} else {
// Success
// console.log(util.inspect(data));
}
});
}
this.batchSend(currentTimerMetrics, namespace);

// put all currently accumulated gauge metrics into an array
var currentGaugeMetrics = [];
Expand All @@ -184,21 +191,7 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) {
});
}

// send off the array (instead of one at a time)
if (currentGaugeMetrics.length > 0) {
this.cloudwatch.putMetricData({
MetricData: currentGaugeMetrics,
Namespace: namespace
}, function(err, data) {
if (err) {
// log an error
console.log(util.inspect(err));
} else {
// Success
// console.log(util.inspect(data));
}
});
}
this.batchSend(currentGaugeMetrics, namespace);

// put all currently accumulated set metrics into an array
var currentSetMetrics = [];
Expand All @@ -221,21 +214,7 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) {
});
}

// send off the array (instead of one at a time)
if (currentSetMetrics.length > 0) {
this.cloudwatch.putMetricData({
MetricData: currentSetMetrics,
Namespace: namespace
}, function(err, data) {
if (err) {
// log an error
console.log(util.inspect(err));
} else {
// Success
// console.log(util.inspect(data));
}
});
}
this.batchSend(currentSetMetrics, namespace);
};

exports.init = function(startupTime, config, events) {
Expand Down

0 comments on commit 0613935

Please sign in to comment.