From 525da95f49350711d5fdc1f724a4b3b313f46edc Mon Sep 17 00:00:00 2001 From: Michael Ambrose Date: Fri, 5 May 2017 16:22:03 -0400 Subject: [PATCH 1/3] Updated cloudwatch plugin to allow specific tag selection Tags come back from AWS as a key value pair inside an array This array is now converted to an object Tags can be selected when using the 'ec2_instance_attribute' query Example: `ec2_instance_attribute(us-east-1, Tags.Name, { "tag:Grafana": [ "true" ] })` --- .../app/plugins/datasource/cloudwatch/datasource.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index 60c7e167a06..b9f9b062ce6 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -260,7 +260,17 @@ function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnot return this.performEC2DescribeInstances(region, filters, null).then(function(result) { var attributes = _.chain(result.Reservations) .map(function(reservations) { - return _.map(reservations.Instances, targetAttributeName); + return _.map(reservations.Instances, function(instance) { + var tags = {}; + _.each(instance.Tags, function(tag) { + tags[tag.Key] = tag.Value; + }); + instance.Tags = tags; + return instance; + }); + }) + .map(function(instances) { + return _.map(instances, targetAttributeName); }) .flatten().uniq().sortBy().value(); return transformSuggestData(attributes); From f0169656ba57dcea47eaed808cfb7758b39a6755 Mon Sep 17 00:00:00 2001 From: Michael Ambrose Date: Wed, 7 Jun 2017 16:07:47 -0400 Subject: [PATCH 2/3] Added test to for Cloudwatch EC2 tag selection --- .../cloudwatch/specs/datasource_specs.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts index afc0f4a5962..28fd524663b 100644 --- a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts @@ -318,4 +318,38 @@ describe('CloudWatchDatasource', function() { expect(scenario.request.data.action).to.be('ListMetrics'); }); }); + + describeMetricFindQuery('ec2_instance_attribute(us-east-1, Tags.Name, { "tag:team": [ "sysops" ] })', scenario => { + scenario.setup(() => { + scenario.requestResponse = { + Reservations: [ + { + Instances: [ + { + Tags: [ + { Key: 'InstanceId', Value: 'i-123456' }, + { Key: 'Name', Value: 'Sysops Dev Server' }, + { Key: 'env', Value: 'dev' }, + { Key: 'team', Value: 'sysops' } + ] + }, + { + Tags: [ + { Key: 'InstanceId', Value: 'i-789012' }, + { Key: 'Name', Value: 'Sysops Staging Server' }, + { Key: 'env', Value: 'staging' }, + { Key: 'team', Value: 'sysops' } + ] + } + ] + } + ] + }; + }); + + it('should return the "Name" tag for each instance', function() { + expect(scenario.result[0].text).to.be('Sysops Dev Server'); + expect(scenario.result[1].text).to.be('Sysops Staging Server'); + }); + }); }); From aa3a737fea8a77a858d5d022c3ce70766e161d35 Mon Sep 17 00:00:00 2001 From: Michael Ambrose Date: Wed, 7 Jun 2017 16:52:45 -0400 Subject: [PATCH 3/3] Updated cloudwatch doc to be more clear on ec2_instance_attribute usage and added Tag selection example --- .../features/datasources/cloudwatch.md | 50 +++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/docs/sources/features/datasources/cloudwatch.md b/docs/sources/features/datasources/cloudwatch.md index 8d77e5c59c0..61dd90d2881 100644 --- a/docs/sources/features/datasources/cloudwatch.md +++ b/docs/sources/features/datasources/cloudwatch.md @@ -84,8 +84,8 @@ Name | Description *metrics(namespace, [region])* | Returns a list of metrics in the namespace. (specify region for custom metrics) *dimension_keys(namespace)* | Returns a list of dimension keys in the namespace. *dimension_values(region, namespace, metric, dimension_key)* | Returns a list of dimension values matching the specified `region`, `namespace`, `metric` and `dimension_key`. -*ebs_volume_ids(region, instance_id)* | Returns a list of volume id matching the specified `region`, `instance_id`. -*ec2_instance_attribute(region, attribute_name, filters)* | Returns a list of attribute matching the specified `region`, `attribute_name`, `filters`. +*ebs_volume_ids(region, instance_id)* | Returns a list of volume ids matching the specified `region`, `instance_id`. +*ec2_instance_attribute(region, attribute_name, filters)* | Returns a list of attributes matching the specified `region`, `attribute_name`, `filters`. For details about the metrics CloudWatch provides, please refer to the [CloudWatch documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html). @@ -101,10 +101,13 @@ Query | Service *dimension_values(us-east-1,AWS/RDS,CPUUtilization,DBInstanceIdentifier)* | RDS *dimension_values(us-east-1,AWS/S3,BucketSizeBytes,BucketName)* | S3 -#### ec2_instance_attribute JSON filters +## ec2_instance_attribute examples -The `ec2_instance_attribute` query take `filters` in JSON format. +### JSON filters + +The `ec2_instance_attribute` query takes `filters` in JSON format. You can specify [pre-defined filters of ec2:DescribeInstances](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html). +Note that the actual filtering takes place on Amazon's servers, not in Grafana. Filters syntax: @@ -116,6 +119,45 @@ Example `ec2_instance_attribute()` query ec2_instance_attribute(us-east-1, InstanceId, { "tag:Environment": [ "production" ] }) +### Selecting Attributes + +Only 1 attribute per instance can be returned. Any flat attribute can be selected (i.e. if the attribute has a single value and isn't an object or array). Below is a list of available flat attributes: + + * `AmiLaunchIndex` + * `Architecture` + * `ClientToken` + * `EbsOptimized` + * `EnaSupport` + * `Hypervisor` + * `IamInstanceProfile` + * `ImageId` + * `InstanceId` + * `InstanceLifecycle` + * `InstanceType` + * `KernelId` + * `KeyName` + * `LaunchTime` + * `Platform` + * `PrivateDnsName` + * `PrivateIpAddress` + * `PublicDnsName` + * `PublicIpAddress` + * `RamdiskId` + * `RootDeviceName` + * `RootDeviceType` + * `SourceDestCheck` + * `SpotInstanceRequestId` + * `SriovNetSupport` + * `SubnetId` + * `VirtualizationType` + * `VpcId` + +Tags can be selected by prepending the tag name with `Tags.` + +Example `ec2_instance_attribute()` query + + ec2_instance_attribute(us-east-1, Tags.Name, { "tag:Team": [ "sysops" ] }) + ## Cost Amazon provides 1 million CloudWatch API requests each month at no additional charge. Past this,