如何使用 python 获取 AWS SQS 指标

How to get the AWS SQS metrics with python

目前我们使用包boto获取队列消息,然后进行一些处理。相关代码为:

from boto.sqs.message import RawMessage
import boto.sqs
import boto.sns
import json

conn = boto.sqs.connect_to_region(
    "us-west-1",
    aws_access_key_id="XXXXXXX",
    aws_secret_access_key="XXXXXXX")
q=conn.get_queue('queueName')
q.set_message_class(RawMessage)
res=q.get_messages(10,wait_time_seconds=1)
....

剩下的只是处理代码(对问题不重要)。此代码完美运行。

我想知道是否有办法从 python 获取队列的指标,例如 NumberOfMessagesSent

基于这个 post get metrics and the CloudWatch 网站,我认为可能会有

conn = boto.sqs.cloudwatch.connect_to_region()

我可以从中做到

conn.get_metric_statistics()

但似乎并非如此(除非我遗漏了什么)。

我找到了这篇 code 非常好的作品。但我想知道 boto.sqs

中是否有 "better" (或更简洁的替代方案)

调用q.get_attributes()可以获得以下属性

ApproximateNumberOfMessages, ApproximateNumberOfMessagesNotVisible, VisibilityTimeout, CreatedTimestamp, LastModifiedTimestamp, Policy ReceiveMessageWaitTimeSeconds

对于 NumberOfMessagesSent 之类的事情,您将需要在 CloudWatch 中查询那里的数据 SQS 日志。您链接的代码看起来像是使用 boto 查询 CloudWatch 指标的正确方法。

谢谢,

此外,如果有人觉得它有用,我们找到了与我链接的 code 中的方法类似的方法,但可能更简洁一些。

import boto
import datetime
conn = boto.connect_cloudwatch(aws_access_key_id="xxx",aws_secret_access_key="xxx")
conn.get_metric_statistics(
        300,
        datetime.datetime.utcnow() - datetime.timedelta(seconds=300),
        datetime.datetime.utcnow(),
        'ApproximateNumberOfMessagesVisible',
        'AWS/SQS',
        'Average',
        dimensions={'QueueName':['Myqueuename']}
   )