3333import software .amazon .awssdk .services .sqs .model .MessageSystemAttributeName ;
3434import software .amazon .awssdk .services .sqs .model .ReceiveMessageRequest ;
3535import software .amazon .awssdk .services .sqs .model .SqsException ;
36+ import software .amazon .awssdk .awscore .exception .AwsServiceException ;
37+ import software .amazon .awssdk .services .sqs .model .KmsAccessDeniedException ;
38+ import software .amazon .awssdk .services .sqs .model .KmsNotFoundException ;
39+ import software .amazon .awssdk .services .sqs .model .KmsThrottledException ;
40+ import software .amazon .awssdk .services .sqs .model .QueueDoesNotExistException ;
3641import software .amazon .awssdk .services .sts .model .StsException ;
3742
3843import java .time .Duration ;
@@ -58,6 +63,9 @@ public class SqsWorker implements Runnable {
5863 static final String SQS_VISIBILITY_TIMEOUT_CHANGED_COUNT_METRIC_NAME = "sqsVisibilityTimeoutChangedCount" ;
5964 static final String SQS_VISIBILITY_TIMEOUT_CHANGE_FAILED_COUNT_METRIC_NAME = "sqsVisibilityTimeoutChangeFailedCount" ;
6065 static final String ACKNOWLEDGEMENT_SET_CALLACK_METRIC_NAME = "acknowledgementSetCallbackCounter" ;
66+ public static final String SQS_MESSAGE_ACCESS_DENIED_METRIC_NAME = "sqsMessagesAccessDenied" ;
67+ public static final String SQS_MESSAGE_THROTTLED_METRIC_NAME = "sqsMessagesThrottled" ;
68+ public static final String SQS_QUEUE_NOT_FOUND_METRIC_NAME = "sqsQueueNotFound" ;
6169
6270 private final S3SourceConfig s3SourceConfig ;
6371 private final SqsClient sqsClient ;
@@ -74,6 +82,9 @@ public class SqsWorker implements Runnable {
7482 private final Counter acknowledgementSetCallbackCounter ;
7583 private final Counter sqsVisibilityTimeoutChangedCount ;
7684 private final Counter sqsVisibilityTimeoutChangeFailedCount ;
85+ private final Counter sqsMessageAccessDeniedCounter ;
86+ private final Counter sqsMessageThrottledCounter ;
87+ private final Counter sqsQueueNotFoundCounter ;
7788 private final Timer sqsMessageDelayTimer ;
7889 private final Backoff standardBackoff ;
7990 private final SqsMessageParser sqsMessageParser ;
@@ -111,6 +122,9 @@ public SqsWorker(final AcknowledgementSetManager acknowledgementSetManager,
111122 acknowledgementSetCallbackCounter = pluginMetrics .counter (ACKNOWLEDGEMENT_SET_CALLACK_METRIC_NAME );
112123 sqsVisibilityTimeoutChangedCount = pluginMetrics .counter (SQS_VISIBILITY_TIMEOUT_CHANGED_COUNT_METRIC_NAME );
113124 sqsVisibilityTimeoutChangeFailedCount = pluginMetrics .counter (SQS_VISIBILITY_TIMEOUT_CHANGE_FAILED_COUNT_METRIC_NAME );
125+ sqsMessageAccessDeniedCounter = pluginMetrics .counter (SQS_MESSAGE_ACCESS_DENIED_METRIC_NAME );
126+ sqsMessageThrottledCounter = pluginMetrics .counter (SQS_MESSAGE_THROTTLED_METRIC_NAME );
127+ sqsQueueNotFoundCounter = pluginMetrics .counter (SQS_QUEUE_NOT_FOUND_METRIC_NAME );
114128 }
115129
116130 @ Override
@@ -164,6 +178,7 @@ private List<Message> getMessagesFromSqs() {
164178 return messages ;
165179 } catch (final SqsException | StsException e ) {
166180 LOG .error ("Error reading from SQS: {}. Retrying with exponential backoff." , e .getMessage ());
181+ recordSqsException (e );
167182 sqsReceiveMessagesFailedCounter .increment ();
168183 applyBackoff ();
169184 return Collections .emptyList ();
@@ -452,4 +467,21 @@ private int getApproximateReceiveCount(final Message message) {
452467 void stop () {
453468 isStopped = true ;
454469 }
470+
471+ public void recordSqsException (final AwsServiceException e ) {
472+ // AWS SQS emits some of their exceptions without the matching HTTP code. As we want to generate an aggregate version of
473+ // these exceptions, we have to explicitly catch the type alongside the status code for the ones that leverage the status
474+ // code (i.e. InvalidAddressException)
475+ if (e .statusCode () == 403 ||
476+ e instanceof KmsAccessDeniedException ) {
477+ sqsMessageAccessDeniedCounter .increment ();
478+ } else if (e .statusCode () == 404 ||
479+ e instanceof QueueDoesNotExistException ||
480+ e instanceof KmsNotFoundException ) {
481+ sqsQueueNotFoundCounter .increment ();
482+ } else if (e .isThrottlingException () ||
483+ e instanceof KmsThrottledException ) {
484+ sqsMessageThrottledCounter .increment ();
485+ }
486+ }
455487}
0 commit comments