我正在使用Amazon SQS中的死信队列。我希望只要队列接收到新消息,就应该发出CloudWatch警报。问题是我在队列的指标:number_of_messages_sent上配置了一个警报,但是在Amazon SQS Dead-Letter Queues - Amazon Simple Queue Service文档中提到的死信队列的情况下,这个指标不能像预期的那样工作。
现在一些关于这方面的建议是使用number_of_messages_visible,但我不确定如何在警报中配置它。因此,如果我设置此metric>0的值,则这与在队列中获取新消息不同。如果存在旧消息,则度量值始终为>0。我可以做一些数学表达式,在某个定义的周期(比方说一分钟)内获得这个度量中的增量,但我正在寻找一些更好的解决方案。
发布于 2020-05-30 07:57:23
我为同样的问题而苦苦挣扎,对我来说答案是使用NumberOfMessagesSent。然后,我可以为在我配置的时间段内传入的新消息设置标准。以下是在CloudFormation中对我有效的方法。
请注意,如果报警由于持续故障而停留在报警状态,则不会发生单个报警。你可以设置另一个警报来捕捉它们。即:1小时内发生100个错误时进行告警,方法相同。
更新:由于NumberOfMessagesReceived和NumberOfMessagesSent的指标取决于消息的排队方式,因此在向dlq设置添加延迟后,我使用指标ApproximateNumberOfMessagesDelayed为我们的需求设计了一个新的解决方案。如果您手动将消息添加到队列中,则NumberOfMessagesReceived将起作用。否则,在设置延迟后使用ApproximateNumberOfMessagesDelayed。
MyDeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
MessageRetentionPeriod: 1209600 # 14 days
DelaySeconds: 60 #for alarms
DLQthresholdAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: "Alarm dlq messages when we have 1 or more failed messages in 10 minutes"
Namespace: "AWS/SQS"
MetricName: "ApproximateNumberOfMessagesDelayed"
Dimensions:
- Name: "QueueName"
Value:
Fn::GetAtt:
- "MyDeadLetterQueue"
- "QueueName"
Statistic: "Sum"
Period: 300
DatapointsToAlarm: 1
EvaluationPeriods: 2
Threshold: 1
ComparisonOperator: "GreaterThanOrEqualToThreshold"
AlarmActions:
- !Ref MyAlarmTopic发布于 2020-12-22 15:21:23
我们有同样的问题,并通过使用2个度量标准和创建一个数学表达式来解决它。
ConsentQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: "queue"
RedrivePolicy:
deadLetterTargetArn:
Fn::GetAtt:
- "DLQ"
- "Arn"
maxReceiveCount: 3 # after 3 tries the event will go to DLQ
VisibilityTimeout: 65
DLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: "DLQ"
DLQAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: "SQS failed"
AlarmName: "SQSAlarm"
Metrics:
- Expression: "m2-m1"
Id: "e1"
Label: "ChangeInAmountVisible"
ReturnData: true
- Id: "m1"
Label: "MessagesVisibleMin"
MetricStat:
Metric:
Dimensions:
- Name: QueueName
Value: !GetAtt DLQ.QueueName
MetricName: ApproximateNumberOfMessagesVisible
Namespace: "AWS/SQS"
Period: 300 # evaluate maximum over period of 5 min
Stat: Minimum
Unit: Count
ReturnData: false
- Id: "m2"
Label: "MessagesVisibleMax"
MetricStat:
Metric:
Dimensions:
- Name: QueueName
Value: !GetAtt DLQ.QueueName
MetricName: ApproximateNumberOfMessagesVisible
Namespace: "AWS/SQS"
Period: 300 # evaluate maximum over period of 5 min
Stat: Maximum
Unit: Count
ReturnData: false
ComparisonOperator: GreaterThanOrEqualToThreshold
Threshold: 1
DatapointsToAlarm: 1
EvaluationPeriods: 1周期很重要,因此最小值和最大值要在较长的周期内进行评估。

发布于 2020-05-30 15:59:00
很难实现问题中提出的问题。如果cloudwatch警报的端点是发送电子邮件或通知用户DLQ消息到达,您可以在SQS,SNS和Lambda的帮助下做类似的事情。从cloudwatch上,你可以看到当你收到任何电子邮件时,DLQ消息是如何及时增长的。
#!/usr/bin/python3
import json
import boto3
import os
def lambda_handler(event, context):
batch_processes=[]
for record in event['Records']:
send_request(record["body"])
def send_request(body):
# Create SNS client
sns = boto3.client('sns')
# Publish messages to the specified SNS topic
response = sns.publish(
TopicArn=#YOUR_TOPIC_ARN
Message=body,
)
# Print out the response
print(response)https://stackoverflow.com/questions/60211243
复制相似问题