From 4d994031dcfdac11dd1100d9e21b663ca8c0a0f2 Mon Sep 17 00:00:00 2001 From: Jason Antman Date: Fri, 6 Oct 2017 19:06:12 -0400 Subject: [PATCH] issue #678 - convert cloudwatch handler to boto3 --- src/diamond/handler/cloudwatch.py | 54 +++++++++++++++++++------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/diamond/handler/cloudwatch.py b/src/diamond/handler/cloudwatch.py index 5e10f7387..0a0753668 100644 --- a/src/diamond/handler/cloudwatch.py +++ b/src/diamond/handler/cloudwatch.py @@ -6,7 +6,7 @@ #### Dependencies - * [boto](http://boto.readthedocs.org/en/latest/index.html) + * [boto3](http://boto3.readthedocs.org/en/latest/) #### Configuration @@ -57,11 +57,11 @@ from configobj import Section try: - import boto - import boto.ec2.cloudwatch - import boto.utils + import boto3 + from botocore.utils import InstanceMetadataFetcher except ImportError: - boto = None + boto3 = None + InstanceMetadataFetcher = None class cloudwatchHandler(Handler): @@ -78,7 +78,7 @@ def __init__(self, config=None): # Initialize Handler Handler.__init__(self, config) - if not boto: + if not boto3: self.log.error( "CloudWatch: Boto is not installed, please install boto.") return @@ -89,13 +89,15 @@ def __init__(self, config=None): # Initialize Options self.region = self.config['region'] - instance_metadata = boto.utils.get_instance_metadata( - timeout=1, num_retries=5 - ) - if 'instance-id' in instance_metadata: - self.instance_id = instance_metadata['instance-id'] + try: + self.instance_id = InstanceMetadataFetcher( + timeout=1, num_attempts=5 + )._get_request( + 'http://169.254.169.254/latest/meta-data/instance-id', + 1, num_attempts=5 + ).text.strip() self.log.debug("Setting InstanceId: " + self.instance_id) - else: + except Exception: self.instance_id = None self.log.error('CloudWatch: Failed to load instance metadata') @@ -188,12 +190,13 @@ def _bind(self): "CloudWatch: Attempting to connect to CloudWatch at Region: %s", self.region) try: - self.connection = boto.ec2.cloudwatch.connect_to_region( - self.region) + self.connection = boto3.client( + 'cloudwatch', region_name=self.region + ) self.log.debug( "CloudWatch: Succesfully Connected to CloudWatch at Region: %s", self.region) - except boto.exception.EC2ResponseError: + except boto3.exceptions.Boto3Error: self.log.error('CloudWatch: CloudWatch Exception Handler: ') def __del__(self): @@ -209,7 +212,7 @@ def process(self, metric): """ Process a metric and send it to CloudWatch """ - if not boto: + if not boto3: return collector = str(metric.getCollectorPath()) @@ -267,11 +270,20 @@ def send_metrics_to_cloudwatch(self, rule, metric, dimensions): try: self.connection.put_metric_data( - str(rule['namespace']), - str(rule['name']), - str(metric.value), - timestamp, str(rule['unit']), - dimensions) + Namespace=str(rule['namespace']), + MetricData=[ + { + 'MetricName': str(rule['name']), + 'Dimensions': [ + {'Name': x, 'Value': dimensions[x]} + for x in dimensions.keys() + ], + 'Timestamp': timestamp, + 'Value': metric.value, + 'Unit': str(rule['unit']) + } + ] + ) self.log.debug( "CloudWatch: Successfully published metric: %s to" " %s with value (%s) for dimensions %s",