11using System . CommandLine ;
2+ using Azure . Monitor . Query . Metrics . Models ;
23using Particular . EndpointThroughputCounter . Infra ;
34using Particular . LicensingComponent . Report ;
45using Particular . ThroughputQuery ;
56using Particular . ThroughputQuery . AzureServiceBus ;
67
78class AzureServiceBusCommand : BaseCommand
89{
10+ // ASB keeps 90 days of data but will only return 30 days in a single query
11+ const int MaxDaysToCollect = 90 ;
12+ const int MaxDaysToCollectInOneQuery = 30 ;
13+
914 public static Command CreateCommand ( )
1015 {
1116 var command = new Command ( "azureservicebus" , "Measure endpoints and throughput using Azure Service Bus metrics" ) ;
@@ -54,12 +59,17 @@ public static Command CreateCommand()
5459 var cancellationToken = context . GetCancellationToken ( ) ;
5560
5661#if DEBUG
62+ // So we don't have to keep an Azure Service Bus resource id and region in launchSettings.json
63+ // Create a local.settings.json file with the keys below.
5764 if ( resourceId == "LOAD_FROM_CONFIG" )
5865 {
59- // So we don't have to keep an Azure Service Bus resource id in launchSettings.json
60- // Create a local.settings.json file with the key below.
6166 resourceId = AppConfig . Get < string > ( "AZURESERVICEBUS_RESOURCE_ID" ) ;
6267 }
68+
69+ if ( region == "LOAD_FROM_CONFIG" )
70+ {
71+ region = AppConfig . Get < string > ( "AZURESERVICEBUS_REGION" ) ;
72+ }
6373#endif
6474
6575 var runner = new AzureServiceBusCommand ( shared , resourceId , serviceBusDomain , region , metricsDomain ) ;
@@ -85,7 +95,7 @@ protected override async Task<QueueDetails> GetData(CancellationToken cancellati
8595 try
8696 {
8797 var endTime = DateOnly . FromDateTime ( DateTime . UtcNow ) ;
88- var startTime = endTime . AddDays ( - 90 ) ; // Azure Monitor only gives a data for a month back, but we ask for more just in case
98+ var startTime = endTime . AddDays ( - MaxDaysToCollect ) ;
8999 var results = new List < QueueThroughput > ( ) ;
90100
91101 azure . ResetConnectionQueue ( ) ;
@@ -97,7 +107,7 @@ protected override async Task<QueueDetails> GetData(CancellationToken cancellati
97107
98108 Out . Write ( $ "Gathering metrics for queue { i + 1 } /{ queueNames . Length } : { queueName } ") ;
99109
100- var metricValues = ( await azure . GetMetrics ( queueName , startTime , endTime , cancellationToken ) ) . OrderBy ( m => m . TimeStamp ) . ToArray ( ) ;
110+ var metricValues = await GetMetricValues ( queueName , startTime , endTime , cancellationToken ) ;
101111
102112 var maxThroughput = metricValues . Select ( timeEntry => timeEntry . Total ) . Max ( ) ;
103113 var start = DateOnly . FromDateTime ( metricValues . First ( ) . TimeStamp . UtcDateTime ) ;
@@ -159,6 +169,18 @@ protected override async Task<QueueDetails> GetData(CancellationToken cancellati
159169 }
160170 }
161171
172+ async Task < MetricValue [ ] > GetMetricValues ( string queueName , DateOnly start , DateOnly end , CancellationToken cancellationToken )
173+ {
174+ var metricValues = new List < MetricValue > ( ) ;
175+
176+ foreach ( var ( startTime , endTime ) in ReportingWindow . GetReportingWindow ( start , end , MaxDaysToCollectInOneQuery ) )
177+ {
178+ metricValues . AddRange ( await azure . GetMetrics ( queueName , startTime , endTime , cancellationToken ) ) ;
179+ }
180+
181+ return [ .. metricValues . OrderBy ( x => x . TimeStamp ) ] ;
182+ }
183+
162184 protected override async Task < EnvironmentDetails > GetEnvironment ( CancellationToken cancellationToken = default )
163185 {
164186 Out . WriteLine ( $ "Getting data from { azure . FullyQualifiedNamespace } ...") ;
0 commit comments