Helios
Helios

Reputation: 31

How do I correctly use the CloudWatch boto3 API to retrieve data from metrics?

I'm using Python3's boto3 to try to pull data from SQS's metrics using the get_metric_statistics function documented here:

https://boto3.readthedocs.io/en/latest/reference/services/cloudwatch.html#CloudWatch.Client.get_metric_statistics

This is the code I made to try to pull it:

import boto3
import sys
from datetime import datetime, timedelta

client = boto3.client('cloudwatch')

response = client.get_metric_statistics(
    Namespace='SQS',
    MetricName='NumberOfEmptyReceives',
    Dimensions=[
        {
            'Name' : 'QueueName',
            'Value' : 'AlertNotifications'
        }
    ],  
    StartTime=datetime.utcnow() - timedelta(seconds=600),
    EndTime=datetime.utcnow(),
    Period=60,
    Statistics=[
        'Sum'
    ]
)

print(response)
sys.exit(0)

I get a response from the API, with a HTTP status code 200, so that worked, but I get no data points. I also double checked that I'm calling the correct profile using boto3.setup_default_session().

I also double checked that my data existed: https://i.sstatic.net/cRGQo.png

Does anybody spot what I'm doing wrong?

Upvotes: 2

Views: 4753

Answers (2)

BDalli
BDalli

Reputation: 66

Namespace needs to be prepended with AWS/ e.g 'AWS/SQS'

See: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/aws-namespaces.html

Upvotes: 5

neojb1989
neojb1989

Reputation: 181

Give the thing a Unit parameter too. I've had some cloudwatch metrics return back nothing if I didn't include a Unit in my request.

Upvotes: 0

Related Questions