Skip to content

Commit

Permalink
issue python-diamond#678 - convert cloudwatch handler to boto3
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Oct 6, 2017
1 parent 19d136f commit 4d99403
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/diamond/handler/cloudwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#### Dependencies
* [boto](http://boto.readthedocs.org/en/latest/index.html)
* [boto3](http://boto3.readthedocs.org/en/latest/)
#### Configuration
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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')

Expand Down Expand Up @@ -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):
Expand All @@ -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())
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 4d99403

Please sign in to comment.