Skip to content

Commit 34ef70b

Browse files
authored
chore(gateway): update charts (#17873)
1 parent 62096c0 commit 34ef70b

File tree

8 files changed

+58
-67
lines changed

8 files changed

+58
-67
lines changed

src/Runtime/gateway/src/Altinn.Studio.Gateway.Api/Application/Metrics/HandleMetrics.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,14 @@ CancellationToken cancellationToken
6060
);
6161

6262
var amMetrics = await metricsClient.GetAppMetrics(app, range, cancellationToken);
63+
var bucketSize = AzureMonitorClient.GetBucketSize(range);
6364

6465
var metrics = amMetrics.Select(metric => new AppMetric
6566
{
6667
Name = metric.Name,
67-
DataPoints = metric.DataPoints.Select(dataPoint => new AppMetricDataPoint
68-
{
69-
DateTimeOffset = dataPoint.DateTimeOffset,
70-
Count = dataPoint.Count,
71-
}),
68+
Timestamps = metric.Timestamps,
69+
Counts = metric.Counts,
70+
BucketSize = bucketSize,
7271
});
7372

7473
return Results.Ok(metrics);
@@ -92,15 +91,14 @@ CancellationToken cancellationToken
9291
var from = now.AddMinutes(-range);
9392

9493
var amFailedRequests = await metricsClient.GetAppFailedRequests(app, range, cancellationToken);
94+
var bucketSize = AzureMonitorClient.GetBucketSize(range);
9595

9696
var metrics = amFailedRequests.Select(failedRequest => new AppErrorMetric
9797
{
9898
Name = failedRequest.Name,
99-
DataPoints = failedRequest.DataPoints.Select(dataPoint => new AppMetricDataPoint
100-
{
101-
DateTimeOffset = dataPoint.DateTimeOffset,
102-
Count = dataPoint.Count,
103-
}),
99+
Timestamps = failedRequest.Timestamps,
100+
Counts = failedRequest.Counts,
101+
BucketSize = bucketSize,
104102
LogsUrl = metricsClient.GetLogsUrl(
105103
currentGatewayContext.AzureSubscriptionId,
106104
currentGatewayContext.ServiceOwner,

src/Runtime/gateway/src/Altinn.Studio.Gateway.Api/Clients/MetricsClient/AzureMonitorClient.cs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Azure.Monitor.Query.Logs;
99
using Azure.Monitor.Query.Logs.Models;
1010
using Azure.ResourceManager.OperationalInsights;
11-
using Microsoft.Extensions.Logging;
1211
using Microsoft.Extensions.Options;
1312

1413
namespace Altinn.Studio.Gateway.Api.Clients.MetricsClient;
@@ -44,9 +43,16 @@ ILogger<AzureMonitorClient> _logger
4443

4544
internal static IReadOnlyCollection<string> OperationNameKeys { get; } = _operationNames.Keys.ToArray();
4645

46+
internal static int GetBucketSize(int range)
47+
{
48+
var maxPoints = 12;
49+
return (int)Math.Max(1, Math.Ceiling((double)range / maxPoints));
50+
}
51+
4752
private static string GetInterval(int range)
4853
{
49-
return range < 360 ? "5m" : "1h";
54+
var bucketSize = GetBucketSize(range);
55+
return $"{bucketSize}m";
5056
}
5157

5258
private ResourceIdentifier GetApplicationLogAnalyticsWorkspaceId()
@@ -73,7 +79,7 @@ public async Task<IEnumerable<FailedRequest>> GetFailedRequests(int range, Cance
7379
| where ClientType != 'Browser'
7480
| where toint(ResultCode) >= 500
7581
| where OperationName in ('{string.Join("','", _operationNames.Values.SelectMany(value => value))}')
76-
| summarize Count = count() by AppRoleName, OperationName";
82+
| summarize Count = count() by AppRoleName, OperationName;";
7783

7884
Response<LogsQueryResult> response = await _logsQueryClient.QueryResourceAsync(
7985
logAnalyticsWorkspaceId,
@@ -209,7 +215,7 @@ CancellationToken cancellationToken
209215
| where AppRoleName == '{app.Replace("'", "''")}'
210216
| where OperationName in ('{string.Join("','", _operationNames.Values.SelectMany(value => value))}')
211217
| summarize Count = count() by OperationName, DateTimeOffset = bin(TimeGenerated, {interval})
212-
| order by DateTimeOffset desc;";
218+
| order by DateTimeOffset desc";
213219

214220
Response<LogsQueryResult> response = await _logsQueryClient.QueryResourceAsync(
215221
logAnalyticsWorkspaceId,
@@ -219,28 +225,26 @@ CancellationToken cancellationToken
219225
);
220226

221227
var metrics = response
222-
.Value.Table.Rows.Select(row =>
228+
.Value.Table.Rows.GroupBy(row =>
229+
_operationNames.First(n => n.Value.Contains(row.GetString("OperationName"))).Key
230+
)
231+
.Select(group => new AppFailedRequest
223232
{
224-
return new
225-
{
226-
Name = _operationNames.First(n => n.Value.Contains(row.GetString("OperationName"))).Key,
227-
DateTimeOffset = row.GetDateTimeOffset("DateTimeOffset").GetValueOrDefault(),
228-
Count = row.GetDouble("Count") ?? 0,
229-
};
230-
})
231-
.GroupBy(row => row.Name)
232-
.Select(row =>
233-
{
234-
return new AppFailedRequest
235-
{
236-
Name = row.Key,
237-
DataPoints = row.Select(e => new DataPoint { DateTimeOffset = e.DateTimeOffset, Count = e.Count }),
238-
};
233+
Name = group.Key,
234+
Timestamps = group.Select(row =>
235+
row.GetDateTimeOffset("DateTimeOffset")?.ToUnixTimeMilliseconds() ?? 0
236+
),
237+
Counts = group.Select(row => row.GetDouble("Count") ?? 0),
239238
});
240239

241240
return _operationNames.Select(name =>
242241
metrics.FirstOrDefault(metric => metric.Name == name.Key)
243-
?? new AppFailedRequest { Name = name.Key, DataPoints = [] }
242+
?? new AppFailedRequest
243+
{
244+
Name = name.Key,
245+
Timestamps = [],
246+
Counts = [],
247+
}
244248
);
245249
}
246250

@@ -261,7 +265,7 @@ public async Task<IEnumerable<AppMetric>> GetAppMetrics(string app, int range, C
261265
| where AppRoleName == '{app.Replace("'", "''")}'
262266
| where Name in ('{string.Join("','", names)}')
263267
| summarize Count = sum(Sum) by Name, DateTimeOffset = bin(TimeGenerated, {interval})
264-
| order by DateTimeOffset desc;";
268+
| order by DateTimeOffset desc";
265269

266270
Response<LogsQueryResult> response = await _logsQueryClient.QueryResourceAsync(
267271
logAnalyticsWorkspaceId,
@@ -271,27 +275,24 @@ public async Task<IEnumerable<AppMetric>> GetAppMetrics(string app, int range, C
271275
);
272276

273277
var metrics = response
274-
.Value.Table.Rows.Select(row =>
278+
.Value.Table.Rows.GroupBy(row => row.GetString("Name"))
279+
.Select(group => new AppMetric
275280
{
276-
return new
277-
{
278-
Name = row.GetString("Name"),
279-
DateTimeOffset = row.GetDateTimeOffset("DateTimeOffset").GetValueOrDefault(),
280-
Count = row.GetDouble("Count") ?? 0,
281-
};
282-
})
283-
.GroupBy(row => row.Name)
284-
.Select(row =>
285-
{
286-
return new AppMetric
287-
{
288-
Name = row.Key,
289-
DataPoints = row.Select(e => new DataPoint { DateTimeOffset = e.DateTimeOffset, Count = e.Count }),
290-
};
281+
Name = group.Key,
282+
Timestamps = group.Select(row =>
283+
row.GetDateTimeOffset("DateTimeOffset")?.ToUnixTimeMilliseconds() ?? 0
284+
),
285+
Counts = group.Select(row => row.GetDouble("Count") ?? 0),
291286
});
292287

293288
return names.Select(name =>
294-
metrics.FirstOrDefault(metric => metric.Name == name) ?? new AppMetric { Name = name, DataPoints = [] }
289+
metrics.FirstOrDefault(metric => metric.Name == name)
290+
?? new AppMetric
291+
{
292+
Name = name,
293+
Timestamps = [],
294+
Counts = [],
295+
}
295296
);
296297
}
297298

src/Runtime/gateway/src/Altinn.Studio.Gateway.Api/Clients/MetricsClient/Contracts/AzureMonitor/AppFailedRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ namespace Altinn.Studio.Gateway.Api.Clients.MetricsClient.Contracts.AzureMonitor
33
internal sealed class AppFailedRequest
44
{
55
public required string Name { get; set; }
6-
public required IEnumerable<DataPoint> DataPoints { get; set; }
6+
public required IEnumerable<long> Timestamps { get; set; }
7+
public required IEnumerable<double> Counts { get; set; }
78
}

src/Runtime/gateway/src/Altinn.Studio.Gateway.Api/Clients/MetricsClient/Contracts/AzureMonitor/AppMetric.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ namespace Altinn.Studio.Gateway.Api.Clients.MetricsClient.Contracts.AzureMonitor
33
internal sealed class AppMetric
44
{
55
public required string Name { get; set; }
6-
public required IEnumerable<DataPoint> DataPoints { get; set; }
6+
public required IEnumerable<long> Timestamps { get; set; }
7+
public required IEnumerable<double> Counts { get; set; }
78
}

src/Runtime/gateway/src/Altinn.Studio.Gateway.Api/Clients/MetricsClient/Contracts/AzureMonitor/DataPoint.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Runtime/gateway/src/Altinn.Studio.Gateway.Contracts/Metrics/AppErrorMetric.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ namespace Altinn.Studio.Gateway.Contracts.Metrics;
33
public class AppErrorMetric
44
{
55
public required string Name { get; set; }
6-
public required IEnumerable<AppMetricDataPoint> DataPoints { get; set; }
6+
public required IEnumerable<long> Timestamps { get; set; }
7+
public required IEnumerable<double> Counts { get; set; }
8+
public required int BucketSize { get; set; }
79
public required Uri LogsUrl { get; set; }
810
}

src/Runtime/gateway/src/Altinn.Studio.Gateway.Contracts/Metrics/AppMetric.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ namespace Altinn.Studio.Gateway.Contracts.Metrics;
33
public class AppMetric
44
{
55
public required string Name { get; set; }
6-
public required IEnumerable<AppMetricDataPoint> DataPoints { get; set; }
6+
public required IEnumerable<long> Timestamps { get; set; }
7+
public required IEnumerable<double> Counts { get; set; }
8+
public required int BucketSize { get; set; }
79
}

src/Runtime/gateway/src/Altinn.Studio.Gateway.Contracts/Metrics/AppMetricDataPoint.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)