@@ -14,6 +14,7 @@ namespace ServiceControl.Transports.SQS;
1414using Amazon . CloudWatch . Model ;
1515using Amazon . Runtime ;
1616using Amazon . Runtime . CredentialManagement ;
17+ using Amazon . Runtime . Credentials ;
1718using Amazon . SQS ;
1819using Amazon . SQS . Model ;
1920using BrokerThroughput ;
@@ -29,7 +30,7 @@ public class AmazonSQSQuery(ILogger<AmazonSQSQuery> logger, TimeProvider timePro
2930 protected override void InitializeCore ( ReadOnlyDictionary < string , string > settings )
3031 {
3132 var sqsConnectionString = new SQSTransportConnectionString ( transportSettings . ConnectionString ) ;
32- AWSCredentials credentials = FallbackCredentialsFactory . GetCredentials ( ) ;
33+ AWSCredentials credentials = DefaultAWSCredentialsIdentityResolver . GetCredentials ( ) ;
3334 RegionEndpoint ? regionEndpoint = null ;
3435 if ( settings . TryGetValue ( AmazonSQSSettings . Profile , out string ? profile ) )
3536 {
@@ -91,9 +92,15 @@ protected override void InitializeCore(ReadOnlyDictionary<string, string> settin
9192 }
9293 }
9394
95+ bool IsValidAwsRegion ( string region ) => RegionEndpoint . EnumerableAllRegions . Any ( r => r . SystemName . Equals ( region , StringComparison . OrdinalIgnoreCase ) ) ;
96+
9497 if ( settings . TryGetValue ( AmazonSQSSettings . Region , out string ? region ) )
9598 {
9699 string ? previousSetSystemName = regionEndpoint ? . SystemName ;
100+ if ( ! IsValidAwsRegion ( region ) )
101+ {
102+ throw new ArgumentException ( "Invalid region endpoint provided" ) ;
103+ }
97104 regionEndpoint = RegionEndpoint . GetBySystemName ( region ) ;
98105
99106 Diagnostics . Append ( $ "Region set to \" { regionEndpoint . SystemName } \" ") ;
@@ -108,6 +115,10 @@ protected override void InitializeCore(ReadOnlyDictionary<string, string> settin
108115 {
109116 if ( sqsConnectionString . Region != null )
110117 {
118+ if ( ! IsValidAwsRegion ( sqsConnectionString . Region ) )
119+ {
120+ throw new ArgumentException ( "Invalid region endpoint provided" ) ;
121+ }
111122 regionEndpoint = RegionEndpoint . GetBySystemName ( sqsConnectionString . Region ) ;
112123 Diagnostics . AppendLine (
113124 $ "Region not set, defaulted to using \" { regionEndpoint . SystemName } \" from the ConnectionString used by instance") ;
@@ -197,8 +208,8 @@ public override async IAsyncEnumerable<QueueThroughput> GetThroughputPerDay(IBro
197208 {
198209 Namespace = "AWS/SQS" ,
199210 MetricName = "NumberOfMessagesDeleted" ,
200- StartTimeUtc = startDate . ToDateTime ( TimeOnly . MinValue ) ,
201- EndTimeUtc = endDate . ToDateTime ( TimeOnly . MaxValue ) ,
211+ StartTime = startDate . ToDateTime ( TimeOnly . MinValue ) ,
212+ EndTime = endDate . ToDateTime ( TimeOnly . MaxValue ) ,
202213 Period = 24 * 60 * 60 , // 1 day
203214 Statistics = [ "Sum" ] ,
204215 Dimensions = [
@@ -217,12 +228,15 @@ public override async IAsyncEnumerable<QueueThroughput> GetThroughputPerDay(IBro
217228 currentDate = currentDate . AddDays ( 1 ) ;
218229 }
219230
220- foreach ( var datapoint in resp . Datapoints )
231+ foreach ( var datapoint in resp . Datapoints ?? [ ] )
221232 {
222233 // There is a bug in the AWS SDK. The timestamp is actually UTC time, eventhough the DateTime returned type says Local
223234 // See https://github.com/aws/aws-sdk-net/issues/167
224235 // So do not convert the timestamp to UTC time!
225- data [ DateOnly . FromDateTime ( datapoint . Timestamp ) ] . TotalThroughput = ( long ) datapoint . Sum ;
236+ if ( datapoint . Timestamp . HasValue )
237+ {
238+ data [ DateOnly . FromDateTime ( datapoint . Timestamp . Value ) ] . TotalThroughput = ( long ) datapoint . Sum . GetValueOrDefault ( 0 ) ;
239+ }
226240 }
227241
228242 foreach ( QueueThroughput queueThroughput in data . Values )
@@ -244,7 +258,7 @@ public override async IAsyncEnumerable<IBrokerQueue> GetQueueNames(
244258 {
245259 var response = await sqs ! . ListQueuesAsync ( request , cancellationToken ) ;
246260
247- foreach ( var queue in response . QueueUrls . Select ( url => url . Split ( '/' ) [ 4 ] ) )
261+ foreach ( var queue in ( response . QueueUrls ?? [ ] ) . Select ( url => url . Split ( '/' ) [ 4 ] ) )
248262 {
249263 if ( ! queue . EndsWith ( "-delay.fifo" , StringComparison . OrdinalIgnoreCase ) )
250264 {
0 commit comments