diff --git a/README.md b/README.md index c3cef39..029faaa 100644 --- a/README.md +++ b/README.md @@ -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" } } @@ -62,24 +62,25 @@ 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 } @@ -87,7 +88,7 @@ Using the option *processKeyForNamespace* (default is false) you can parse the b 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. @@ -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'] } @@ -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. { diff --git a/lib/aws-cloudwatch-statsd-backend.js b/lib/aws-cloudwatch-statsd-backend.js index f0de42f..4cb9243 100644 --- a/lib/aws-cloudwatch-statsd-backend.js +++ b/lib/aws-cloudwatch-statsd-backend.js @@ -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 }; }; @@ -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] @@ -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: { @@ -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] @@ -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