Skip to content

Commit 75783e4

Browse files
Merge pull request #1249 from Particular/asb/ninety-day-fix
Fix 90 day data collection for Azure Service Bus
2 parents 06e6f31 + dd70fce commit 75783e4

3 files changed

Lines changed: 44 additions & 5 deletions

File tree

src/AppCommon/Commands/AzureServiceBusCommand.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
using System.CommandLine;
2+
using Azure.Monitor.Query.Metrics.Models;
23
using Particular.EndpointThroughputCounter.Infra;
34
using Particular.LicensingComponent.Report;
45
using Particular.ThroughputQuery;
56
using Particular.ThroughputQuery.AzureServiceBus;
67

78
class 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}...");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public static class ReportingWindow
2+
{
3+
public static IEnumerable<(DateOnly Start, DateOnly End)> GetReportingWindow(DateOnly startDate, DateOnly endDate, int maxDaysPerPeriod)
4+
{
5+
DateOnly currentStart = startDate;
6+
while (currentStart <= endDate)
7+
{
8+
DateOnly currentEnd = currentStart.AddDays(maxDaysPerPeriod);
9+
if (currentEnd > endDate)
10+
{
11+
currentEnd = endDate;
12+
}
13+
yield return (currentStart, currentEnd);
14+
currentStart = currentEnd.AddDays(1);
15+
}
16+
}
17+
}

src/Tool/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"ThroughputTool-ASB": {
3232
"commandName": "Project",
33-
"commandLineArgs": "azureservicebus --resourceId LOAD_FROM_CONFIG --queueNameMasks Samples --customerName \"Particular Software\" --unattended",
33+
"commandLineArgs": "azureservicebus --resourceId LOAD_FROM_CONFIG --region LOAD_FROM_CONFIG --queueNameMasks Samples --customerName \"Particular Software\" --unattended",
3434
"environmentVariables": {
3535
"IS_DEVELOPMENT": "true"
3636
}

0 commit comments

Comments
 (0)