1717import org .opensearch .dataprepper .model .acknowledgements .AcknowledgementSetManager ;
1818import org .slf4j .Logger ;
1919import org .slf4j .LoggerFactory ;
20+ import software .amazon .awssdk .awscore .exception .AwsServiceException ;
2021import software .amazon .awssdk .core .exception .SdkException ;
2122import software .amazon .awssdk .services .sqs .SqsClient ;
2223import software .amazon .awssdk .services .sqs .model .ChangeMessageVisibilityRequest ;
2324import software .amazon .awssdk .services .sqs .model .DeleteMessageBatchRequest ;
2425import software .amazon .awssdk .services .sqs .model .DeleteMessageBatchRequestEntry ;
2526import software .amazon .awssdk .services .sqs .model .DeleteMessageBatchResponse ;
27+ import software .amazon .awssdk .services .sqs .model .KmsAccessDeniedException ;
28+ import software .amazon .awssdk .services .sqs .model .KmsNotFoundException ;
29+ import software .amazon .awssdk .services .sqs .model .KmsThrottledException ;
2630import software .amazon .awssdk .services .sqs .model .Message ;
31+ import software .amazon .awssdk .services .sqs .model .QueueDoesNotExistException ;
2732import software .amazon .awssdk .services .sqs .model .ReceiveMessageRequest ;
2833import software .amazon .awssdk .services .sqs .model .SqsException ;
2934import software .amazon .awssdk .services .sts .model .StsException ;
@@ -41,6 +46,9 @@ public class SqsWorkerCommon {
4146 public static final String SQS_MESSAGES_DELETE_FAILED_METRIC_NAME = "sqsMessagesDeleteFailed" ;
4247 public static final String SQS_VISIBILITY_TIMEOUT_CHANGED_COUNT_METRIC_NAME = "sqsVisibilityTimeoutChangedCount" ;
4348 public static final String SQS_VISIBILITY_TIMEOUT_CHANGE_FAILED_COUNT_METRIC_NAME = "sqsVisibilityTimeoutChangeFailedCount" ;
49+ public static final String SQS_MESSAGE_ACCESS_DENIED_METRIC_NAME = "sqsMessagesAccessDenied" ;
50+ public static final String SQS_MESSAGE_THROTTLED_METRIC_NAME = "sqsMessagesThrottled" ;
51+ public static final String SQS_QUEUE_NOT_FOUND_METRIC_NAME = "sqsQueueNotFound" ;
4452
4553 private final Backoff standardBackoff ;
4654 private final PluginMetrics pluginMetrics ;
@@ -54,6 +62,9 @@ public class SqsWorkerCommon {
5462 private final Counter acknowledgementSetCallbackCounter ;
5563 private final Counter sqsVisibilityTimeoutChangedCount ;
5664 private final Counter sqsVisibilityTimeoutChangeFailedCount ;
65+ private final Counter sqsMessageAccessDeniedCounter ;
66+ private final Counter sqsMessageThrottledCounter ;
67+ private final Counter sqsQueueNotFoundCounter ;
5768
5869 public SqsWorkerCommon (final Backoff standardBackoff ,
5970 final PluginMetrics pluginMetrics ,
@@ -73,6 +84,9 @@ public SqsWorkerCommon(final Backoff standardBackoff,
7384 acknowledgementSetCallbackCounter = pluginMetrics .counter (ACKNOWLEDGEMENT_SET_CALLACK_METRIC_NAME );
7485 sqsVisibilityTimeoutChangedCount = pluginMetrics .counter (SQS_VISIBILITY_TIMEOUT_CHANGED_COUNT_METRIC_NAME );
7586 sqsVisibilityTimeoutChangeFailedCount = pluginMetrics .counter (SQS_VISIBILITY_TIMEOUT_CHANGE_FAILED_COUNT_METRIC_NAME );
87+ sqsMessageAccessDeniedCounter = pluginMetrics .counter (SQS_MESSAGE_ACCESS_DENIED_METRIC_NAME );
88+ sqsMessageThrottledCounter = pluginMetrics .counter (SQS_MESSAGE_THROTTLED_METRIC_NAME );
89+ sqsQueueNotFoundCounter = pluginMetrics .counter (SQS_QUEUE_NOT_FOUND_METRIC_NAME );
7690 }
7791
7892 public List <Message > pollSqsMessages (final String queueUrl ,
@@ -91,6 +105,7 @@ public List<Message> pollSqsMessages(final String queueUrl,
91105 }
92106 catch (SqsException | StsException e ) {
93107 LOG .error ("Error reading from SQS: {}. Retrying with exponential backoff." , e .getMessage ());
108+ recordSqsException (e );
94109 applyBackoff ();
95110 return Collections .emptyList ();
96111 }
@@ -212,4 +227,21 @@ public Counter getSqsMessagesDeletedCounter() {
212227 public void stop () {
213228 isStopped = true ;
214229 }
230+
231+ public void recordSqsException (final AwsServiceException e ) {
232+ // AWS SQS emits some of their exceptions without the matching HTTP code. As we want to generate an aggregate version of
233+ // these exceptions, we have to explicitly catch the type alongside the status code for the ones that leverage the status
234+ // code (i.e. InvalidAddressException)
235+ if (e .statusCode () == 403 ||
236+ e instanceof KmsAccessDeniedException ) {
237+ sqsMessageAccessDeniedCounter .increment ();
238+ } else if (e .statusCode () == 404 ||
239+ e instanceof QueueDoesNotExistException ||
240+ e instanceof KmsNotFoundException ) {
241+ sqsQueueNotFoundCounter .increment ();
242+ } else if (e .isThrottlingException () ||
243+ e instanceof KmsThrottledException ) {
244+ sqsMessageThrottledCounter .increment ();
245+ }
246+ }
215247}
0 commit comments