Monday, September 18, 2017

Monitoring DynamoDB using Amazon CloudWatch

You can monitor dynamo using CloudWatch service. If you wish to build up your own auto-scaling or only scale down application then you have to use this service to get different Metrics data. For example, you can capture ConsumedReadCapacityUnits, ConsumedWriteCapacityUnits, ReadThrottleEvents etc using Amazon CloudWatch service.

Here I'm going to show you how to get those data using boto3 python API. Coding structure is almost same for all other API. You can try with any other language.
    import boto3
    import datetime

    cloudwatch = boto3.client('cloudwatch')

    endTime = datetime.datetime.utcnow()
    startTime = endTime - datetime.timedelta(minutes=5)
    consumedWriteCapacityData = cloudwatch.get_metric_statistics(Period=60, 
                        StartTime = startTime, EndTime = endTime,
                        MetricName = 'ConsumedWriteCapacityUnits', Namespace = 'AWS/DynamoDB',
                        Statistics = ['Sum'], Dimensions = [{'Name': 'TableName', 'Value': 'Employee' }])
    for item in consumedWriteCapacityData['Datapoints']:
        print('Time: ', item['Timestamp'], '\t ', item['Sum'])
                
As I am using 60 secods period. So, you have to divide each data point value by 60 to get the actual ConsumedWriteCapacityUnits.

Similarly you can also get WriteThrottleEvents data.
    import boto3
    import datetime

    cloudwatch = boto3.client('cloudwatch')

    endTime = datetime.datetime.utcnow()
    startTime = endTime - datetime.timedelta(minutes=5)

    writeThrottleEventsData = cloudwatch.get_metric_statistics(Period=60, 
                        StartTime = startTime, EndTime = endTime,
                        MetricName = 'WriteThrottleEvents', Namespace = 'AWS/DynamoDB',
                        Statistics = ['Sum'], Dimensions = [{'Name': 'TableName', 'Value': 'Employee' }])
    for item in writeThrottleEventsData['Datapoints']:
        print('Time: ', item['Timestamp'], '\t ', item['Sum'])
                

References:

No comments:

Post a Comment