AWS SDK SQS get number of messages in a dead letter queue

0

Hello community,

I somehow can't find the right information. I have following simple task to solve: create a lambda that checks if a dead letter queue has messages and if it has, read how many.

Before I did that I had an alarm set on an SQS metric. I chose the 'ApproximateNumberOfMessagesVisible' metric since 'NumberOfMessagesSent' (which was my first choice) does not work for DLQueues. I have read this article: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html.

The NumberOfMessagesSent and NumberOfMessagesReceived for a dead-letter queue don't match

If you send a message to a dead-letter queue manually, it is captured by the NumberOfMessagesSent metric. However, if a message is sent to a dead-letter queue as a result of a failed processing attempt, it isn't captured by this metric. Thus, it is possible for the values of NumberOfMessagesSent and NumberOfMessagesReceived to be different.

That is nice to know, but I was missing the information: which metric shall I use if NumberOfMessagesSent won't work? I was being pragmatic here so I created an error, a message was sent to the DLQ as a result of a failed processing attempt. Now I looked at the queue in the AWS console under the monitoring-tab and I checked which metric spiked. It was ApproximateNumberOfMessagesVisible, which sounded suitable, so I used it.

Now I wanted to get alerted more often so I chose to build a lambda function that checks how many messages are in the DLQueue. I use Javascript / Typescript so I found this:

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html.
Code looked something like this:

const params = {
        QueueUrl: url,
        AttributeNames: ['ApproximateNumberOfMessagesVisible']
    }

const resp = SQS.getQueueAttributes(params).promise()

It was kind of a bummer that the attribute I wanted was not in there, or better: it was not valid.

Valid Values: All | Policy | VisibilityTimeout | MaximumMessageSize | MessageRetentionPeriod | ApproximateNumberOfMessages | ApproximateNumberOfMessagesNotVisible | CreatedTimestamp | LastModifiedTimestamp | QueueArn | ApproximateNumberOfMessagesDelayed | DelaySeconds | ReceiveMessageWaitTimeSeconds | RedrivePolicy | FifoQueue | ContentBasedDeduplication | ...

My first attempt was to use CloudWatch metrics. So I tried this:
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/cloudwatch-examples-getting-metrics.html

var params = {
  Dimensions: [
    {
      Name: 'LogGroupName', /* required */
    },
  ],
  MetricName: 'IncomingLogEvents',
  Namespace: 'AWS/Logs'
};

cw.listMetrics(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Metrics", JSON.stringify(data.Metrics));
  }
});

but I could not get this working since I did not know what to add to Dimensions / Name to make this working.

Please note that I am not working very long with AWS (only 6 months). Maybe I am on a total wrong track.
Summarized: I want to achieve that my lambda gets the number of messages in a DLQ.

I hope someone can help me Cheers
Aleks

1 Antwort
0

You can use ApproximateNumberOfMessages to find out how many messages are in the queue.

profile pictureAWS
EXPERTE
Uri
beantwortet vor 2 Jahren
  • Thank you for your answer I will try that, and post my reply if it works as expected.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen