@@ -25,6 +25,9 @@ namespace ServiceControl.Transports.ASBS;
2525public class AzureQuery ( ILogger < AzureQuery > logger , TimeProvider timeProvider , TransportSettings transportSettings )
2626 : BrokerThroughputQuery ( logger , "AzureServiceBus" )
2727{
28+ const string CompleteMessageMetricName = "CompleteMessage" ;
29+ const string MicrosoftServicebusNamespacesMetricsNamespace = "Microsoft.ServiceBus/Namespaces" ;
30+
2831 string serviceBusName = string . Empty ;
2932 ArmClient ? armClient ;
3033 TokenCredential ? credential ;
@@ -135,10 +138,7 @@ protected override void InitializeCore(ReadOnlyDictionary<string, string> settin
135138 {
136139 Environment = armEnvironment ,
137140 Transport = new HttpClientTransport (
138- new HttpClient ( new SocketsHttpHandler
139- {
140- PooledConnectionIdleTimeout = TimeSpan . FromMinutes ( 2 )
141- } ) )
141+ new HttpClient ( new SocketsHttpHandler { PooledConnectionIdleTimeout = TimeSpan . FromMinutes ( 2 ) } ) )
142142 } ) ;
143143
144144 return ;
@@ -166,11 +166,7 @@ protected override void InitializeCore(ReadOnlyDictionary<string, string> settin
166166 }
167167
168168 string options = string . Join ( ", " ,
169- new [ ]
170- {
171- ArmEnvironment . AzurePublicCloud , ArmEnvironment . AzureGermany , ArmEnvironment . AzureGovernment ,
172- ArmEnvironment . AzureChina
173- } . Select ( environment => $ "\" { environment . Endpoint } \" ") ) ;
169+ new [ ] { ArmEnvironment . AzurePublicCloud , ArmEnvironment . AzureGermany , ArmEnvironment . AzureGovernment , ArmEnvironment . AzureChina } . Select ( environment => $ "\" { environment . Endpoint } \" ") ) ;
174170 InitialiseErrors . Add ( $ "Management url configuration is invalid, available options are { options } ") ;
175171
176172 return ( ArmEnvironment . AzurePublicCloud , MetricsClientAudience . AzurePublicCloud ) ;
@@ -214,7 +210,11 @@ public override async IAsyncEnumerable<QueueThroughput> GetThroughputPerDay(IBro
214210 var data = new Dictionary < DateOnly , QueueThroughput > ( ) ;
215211 while ( currentDate <= endDate )
216212 {
217- data . Add ( currentDate , new QueueThroughput { TotalThroughput = 0 , DateUTC = currentDate } ) ;
213+ data . Add ( currentDate , new QueueThroughput
214+ {
215+ TotalThroughput = 0 ,
216+ DateUTC = currentDate
217+ } ) ;
218218 currentDate = currentDate . AddDays ( 1 ) ;
219219 }
220220
@@ -237,8 +237,8 @@ async Task<MetricsClient> InitializeMetricsClient(CancellationToken cancellation
237237 }
238238
239239 var serviceBusNamespaceResource = await armClient
240- . GetServiceBusNamespaceResource ( resourceId ) . GetAsync ( cancellationToken )
241- ?? throw new Exception ( $ "Could not find an Azure Service Bus namespace with resource Id: \" { resourceId } \" ") ;
240+ . GetServiceBusNamespaceResource ( resourceId ) . GetAsync ( cancellationToken )
241+ ?? throw new Exception ( $ "Could not find an Azure Service Bus namespace with resource Id: \" { resourceId } \" ") ;
242242
243243 // Determine the region of the namespace
244244 var regionName = serviceBusNamespaceResource . Value . Data . Location . Name ;
@@ -254,10 +254,7 @@ async Task<MetricsClient> InitializeMetricsClient(CancellationToken cancellation
254254 {
255255 Audience = metricsClientAudience ,
256256 Transport = new HttpClientTransport (
257- new HttpClient ( new SocketsHttpHandler
258- {
259- PooledConnectionIdleTimeout = TimeSpan . FromMinutes ( 2 )
260- } ) )
257+ new HttpClient ( new SocketsHttpHandler { PooledConnectionIdleTimeout = TimeSpan . FromMinutes ( 2 ) } ) )
261258 } ) ;
262259 }
263260
@@ -276,8 +273,8 @@ async Task<IReadOnlyList<MetricValue>> GetMetrics(string queueName, DateOnly sta
276273
277274 var response = await metricsClient . QueryResourcesAsync (
278275 [ resourceId ! ] ,
279- [ "CompleteMessage" ] ,
280- "Microsoft.ServiceBus/namespaces" ,
276+ [ CompleteMessageMetricName ] ,
277+ MicrosoftServicebusNamespacesMetricsNamespace ,
281278 new MetricsQueryResourcesOptions
282279 {
283280 Filter = $ "EntityName eq '{ queueName } '",
@@ -286,10 +283,28 @@ async Task<IReadOnlyList<MetricValue>> GetMetrics(string queueName, DateOnly sta
286283 } ,
287284 cancellationToken ) ;
288285
289- var metricValues =
290- response . Value . Values . FirstOrDefault ( ) ? . Metrics . FirstOrDefault ( ) ? . TimeSeries . FirstOrDefault ( ) ? . Values ?? [ ] ;
286+ var metricQueryResult = response . Value . Values . SingleOrDefault ( mr => mr . Namespace == MicrosoftServicebusNamespacesMetricsNamespace ) ;
287+
288+ if ( metricQueryResult is null )
289+ {
290+ throw new Exception ( $ "No metrics query results returned for { MicrosoftServicebusNamespacesMetricsNamespace } ") ;
291+ }
292+
293+ var metricResult = metricQueryResult . GetMetricByName ( CompleteMessageMetricName ) ;
294+
295+ if ( metricResult . Error . Message is not null )
296+ {
297+ throw new Exception ( $ "Metrics query result for '{ metricResult . Name } ' failed: { metricResult . Error . Message } ") ;
298+ }
299+
300+ var timeSeries = metricResult . TimeSeries . SingleOrDefault ( ) ;
301+
302+ if ( timeSeries is null )
303+ {
304+ throw new Exception ( $ "Metrics query result for '{ metricResult . Name } ' contained no time series") ;
305+ }
291306
292- return metricValues . AsReadOnly ( ) ;
307+ return timeSeries . Values . AsReadOnly ( ) ;
293308 }
294309
295310 public override async IAsyncEnumerable < IBrokerQueue > GetQueueNames (
0 commit comments