Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of dimensions #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ The following demonstrates the minimum config for the CloudWatch backend.

{
backends: [ "aws-cloudwatch-statsd-backend" ],
cloudwatch:
cloudwatch:
{
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey:'YOUR_SECRET_ACCESS_KEY',
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey:'YOUR_SECRET_ACCESS_KEY',
region:"YOUR_REGION"
}
}
Expand Down Expand Up @@ -62,32 +62,33 @@ The following overrides the default and any provided namespace or metric name wi

{
backends: [ "aws-cloudwatch-statsd-backend" ],
cloudwatch:
cloudwatch:
{
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
region: 'YOUR_REGION',
namespace: 'App/Controller/Action',
metricName: 'Request'
namespace: 'App/Controller/Action',
metricName: 'Request',
dimensions: [{ Name: 'InstanceId', Value: 'i-123' }]
}
}

Using the option *processKeyForNamespace* (default is false) you can parse the bucket name for namespace in addition to metric name. The backend will use the last component of a bucket name comprised of slash (/), dot (.) or dash (-) separated parts as the metric name. The remaining leading parts will be used as namespace. Separators will be replaced with slashes (/).
Using the option *processKeyForNames* (default is false) you can parse the bucket name for namespace in addition to metric name. The backend will use the last component of a bucket name comprised of slash (/), dot (.) or dash (-) separated parts as the metric name. The remaining leading parts will be used as namespace. Separators will be replaced with slashes (/).

{
backends: [ "aws-cloudwatch-statsd-backend" ],
cloudwatch:
cloudwatch:
{
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
region: 'YOUR_REGION',
processKeyForNames:true
}
}

For example, sending StatsD the following

App.Controller.Action.Request:1|c
App.Controller.Action.Request--InstanceId.i-123:1|c

is will produce the equivalent to the former configuration example. Note that both will be suppressed if overriden as in the former configuration example.

Expand All @@ -97,10 +98,10 @@ Using cloudwatch will incur a cost for each metric sent. In order to control you

{
backends: [ "aws-cloudwatch-statsd-backend" ],
cloudwatch:
cloudwatch:
{
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
region: 'YOUR_REGION',
whitelist: ['YOUR_FULL_METRIC_NAME']
}
Expand All @@ -124,18 +125,18 @@ A preferable approach to obtaining account credentials is instead to query the M

## Multi-region support

If you wish to send cloudwatch metrics to multiple regions at once, instead of
If you wish to send cloudwatch metrics to multiple regions at once, instead of

{
backends: [ "aws-cloudwatch-statsd-backend" ],
cloudwatch:
cloudwatch:
{
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey:'YOUR_SECRET_ACCESS_KEY',
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey:'YOUR_SECRET_ACCESS_KEY',
region:"YOUR_REGION"
}
}

you can use the `instances` key under `cloudwatch` to configure a list of configurations.

{
Expand Down
37 changes: 28 additions & 9 deletions lib/aws-cloudwatch-statsd-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,24 @@ function CloudwatchBackend(startupTime, config, emitter) {
};

CloudwatchBackend.prototype.processKey = function(key) {
var parts = key.split(/[\.\/-]/);
var parts = key.split('--');
var namesParts = parts[0].split(/[\.\/]/);
var dimensionsStrs = parts.length > 1 ? parts.splice(1, parts.length) : [];
var dimensions = [];
for (var i in dimensionsStrs) {
var dimParts = dimensionsStrs[i].split(/[\.\/]/);
if (dimParts.length > 1) {
dimensions.push({
Name: dimParts.splice(0, dimParts.length - 1).join('/'),
Value: dimParts[dimParts.length - 1]
});
}
};

return {
metricName: parts[parts.length - 1],
namespace: parts.length > 1 ? parts.splice(0, parts.length - 1).join("/") : null
metricName: namesParts[namesParts.length - 1],
namespace: namesParts.length > 1 ? namesParts.splice(0, namesParts.length - 1).join('/') : null,
dimensions: dimensions.length > 0 ? dimensions : undefined
};
};

Expand Down Expand Up @@ -123,12 +136,14 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) {
continue;
}

var names = this.config.processKeyForNamespace ? this.processKey(key) : {};
var names = this.config.processKeyForNames ? this.processKey(key) : {};
namespace = this.config.namespace || names.namespace || "AwsCloudWatchStatsdBackend";
var metricName = this.config.metricName || names.metricName || key;
var dimensions = this.config.dimensions || names.dimensions || undefined;

currentCounterMetrics.push({
MetricName: metricName,
Dimensions: dimensions,
Unit: 'Count',
Timestamp: new Date(timestamp * 1000).toISOString(),
Value: counters[key]
Expand Down Expand Up @@ -164,17 +179,17 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) {

var message = "";

var key2;

sum = cumulativeValues[count - 1];
mean = sum / count;

var names = this.config.processKeyForNamespace ? this.processKey(key) : {};
var names = this.config.processKeyForNames ? this.processKey(key) : {};
namespace = this.config.namespace || names.namespace || "AwsCloudWatchStatsdBackend";
var metricName = this.config.metricName || names.metricName || key;
var dimensions = this.config.dimensions || names.dimensions || undefined;

currentTimerMetrics.push({
MetricName: metricName,
Dimensions: dimensions,
Unit: 'Milliseconds',
Timestamp: new Date(timestamp * 1000).toISOString(),
StatisticValues: {
Expand All @@ -197,12 +212,14 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) {
continue;
}

var names = this.config.processKeyForNamespace ? this.processKey(key) : {};
var names = this.config.processKeyForNames ? this.processKey(key) : {};
namespace = this.config.namespace || names.namespace || "AwsCloudWatchStatsdBackend";
var metricName = this.config.metricName || names.metricName || key;
var dimensions = this.config.dimensions || names.dimensions || undefined;

currentGaugeMetrics.push({
MetricName: metricName,
Dimensions: dimensions,
Unit: 'None',
Timestamp: new Date(timestamp * 1000).toISOString(),
Value: gauges[key]
Expand All @@ -219,12 +236,14 @@ CloudwatchBackend.prototype.flush = function(timestamp, metrics) {
continue;
}

var names = this.config.processKeyForNamespace ? this.processKey(key) : {};
var names = this.config.processKeyForNames ? this.processKey(key) : {};
namespace = this.config.namespace || names.namespace || "AwsCloudWatchStatsdBackend";
var metricName = this.config.metricName || names.metricName || key;
var dimensions = this.config.dimensions || names.dimensions || undefined;

currentSetMetrics.push({
MetricName: metricName,
Dimensions: dimensions,
Unit: 'None',
Timestamp: new Date(timestamp * 1000).toISOString(),
Value: sets[key].values().length
Expand Down