-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathHealthCheckHelper.cs
More file actions
350 lines (307 loc) · 18.5 KB
/
HealthCheckHelper.cs
File metadata and controls
350 lines (307 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Config.HealthCheck;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Core.Authorization;
using Azure.DataApiBuilder.Product;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
namespace Azure.DataApiBuilder.Service.HealthCheck
{
/// <summary>
/// Creates a JSON response of health report by executing the Datasource, Rest and Graphql endpoints.
/// Checks the response time with the threshold given to formulate the comprehensive report.
/// </summary>
public class HealthCheckHelper
{
// Dependencies
private ILogger<HealthCheckHelper> _logger;
private HttpUtilities _httpUtility;
private string _incomingRoleHeader = string.Empty;
private string _incomingRoleToken = string.Empty;
private const string TIME_EXCEEDED_ERROR_MESSAGE = "The threshold for executing the request has exceeded.";
/// <summary>
/// Constructor to inject the logger and HttpUtility class.
/// </summary>
/// <param name="logger">Logger to track the log statements.</param>
/// <param name="httpUtility">HttpUtility to call methods from the internal class.</param>
public HealthCheckHelper(ILogger<HealthCheckHelper> logger, HttpUtilities httpUtility)
{
_logger = logger;
_httpUtility = httpUtility;
}
/// <summary>
/// GetHealthCheckResponse is the main function which fetches the HttpContext and then creates the comprehensive health check report.
/// Serializes the report to JSON and returns the response.
/// </summary>
/// <param name="runtimeConfig">RuntimeConfig</param>
/// <returns>This function returns the comprehensive health report after calculating the response time of each datasource, rest and graphql health queries.</returns>
public async Task<ComprehensiveHealthCheckReport> GetHealthCheckResponseAsync(RuntimeConfig runtimeConfig)
{
// Create a JSON response for the comprehensive health check endpoint using the provided basic health report.
// If the response has already been created, it will be reused.
_logger.LogTrace("Comprehensive Health check is enabled in the runtime configuration.");
ComprehensiveHealthCheckReport comprehensiveHealthCheckReport = new();
UpdateVersionAndAppName(ref comprehensiveHealthCheckReport);
UpdateTimestampOfResponse(ref comprehensiveHealthCheckReport);
UpdateDabConfigurationDetails(ref comprehensiveHealthCheckReport, runtimeConfig);
await UpdateHealthCheckDetailsAsync(comprehensiveHealthCheckReport, runtimeConfig);
UpdateOverallHealthStatus(ref comprehensiveHealthCheckReport);
return comprehensiveHealthCheckReport;
}
// Updates the incoming role header with the appropriate value from the request headers.
public void StoreIncomingRoleHeader(HttpContext httpContext)
{
StringValues clientRoleHeader = httpContext.Request.Headers[AuthorizationResolver.CLIENT_ROLE_HEADER];
StringValues clientTokenHeader = httpContext.Request.Headers[AuthenticationOptions.CLIENT_PRINCIPAL_HEADER];
if (clientRoleHeader.Count > 1 || clientTokenHeader.Count > 1)
{
throw new ArgumentException("Multiple values for the client role or token header are not allowed.");
}
// Role Header is not present in the request, set it to anonymous.
if (clientRoleHeader.Count == 1)
{
_incomingRoleHeader = clientRoleHeader.ToString().ToLowerInvariant();
}
if (clientTokenHeader.Count == 1)
{
_incomingRoleToken = clientTokenHeader.ToString();
}
}
/// <summary>
/// Checks if the incoming request is allowed to access the health check endpoint.
/// Anonymous requests are only allowed in Development Mode.
/// </summary>
/// <param name="httpContext">HttpContext to get the headers.</param>
/// <param name="hostMode">Compare with the HostMode of DAB</param>
/// <param name="allowedRoles">AllowedRoles in the Runtime.Health config</param>
/// <returns></returns>
public bool IsUserAllowedToAccessHealthCheck(HttpContext httpContext, bool isDevelopmentMode, HashSet<string> allowedRoles)
{
if (allowedRoles == null || allowedRoles.Count == 0)
{
// When allowedRoles is null or empty, all roles are allowed if Mode = Development.
return isDevelopmentMode;
}
return allowedRoles.Contains(_incomingRoleHeader);
}
// Updates the overall status by comparing all the internal HealthStatuses in the response.
private static void UpdateOverallHealthStatus(ref ComprehensiveHealthCheckReport comprehensiveHealthCheckReport)
{
if (comprehensiveHealthCheckReport.Checks == null)
{
comprehensiveHealthCheckReport.Status = HealthStatus.Healthy;
return;
}
comprehensiveHealthCheckReport.Status = comprehensiveHealthCheckReport.Checks?.Any(check => check.Status == HealthStatus.Unhealthy) == true
? HealthStatus.Unhealthy
: HealthStatus.Healthy;
}
// Updates the AppName and Version for the Health report.
private static void UpdateVersionAndAppName(ref ComprehensiveHealthCheckReport response)
{
// Update the version and app name to the response.
response.Version = ProductInfo.GetProductVersion();
response.AppName = ProductInfo.GetDataApiBuilderUserAgent();
}
// Updates the timestamp for the Health report.
private static void UpdateTimestampOfResponse(ref ComprehensiveHealthCheckReport response)
{
response.TimeStamp = DateTime.UtcNow;
}
// Updates the DAB configuration details coming from RuntimeConfig for the Health report.
private static void UpdateDabConfigurationDetails(ref ComprehensiveHealthCheckReport comprehensiveHealthCheckReport, RuntimeConfig runtimeConfig)
{
comprehensiveHealthCheckReport.ConfigurationDetails = new ConfigurationDetails
{
Rest = runtimeConfig.IsRestEnabled,
GraphQL = runtimeConfig.IsGraphQLEnabled,
Mcp = runtimeConfig.IsMcpEnabled,
Caching = runtimeConfig.IsCachingEnabled,
Telemetry = runtimeConfig?.Runtime?.Telemetry != null,
Mode = runtimeConfig?.Runtime?.Host?.Mode ?? HostMode.Production, // Modify to runtimeConfig.HostMode in Roles PR
};
}
// Main function to internally call for data source and entities health check.
private async Task UpdateHealthCheckDetailsAsync(ComprehensiveHealthCheckReport comprehensiveHealthCheckReport, RuntimeConfig runtimeConfig)
{
comprehensiveHealthCheckReport.Checks = new List<HealthCheckResultEntry>();
await UpdateDataSourceHealthCheckResultsAsync(comprehensiveHealthCheckReport, runtimeConfig);
await UpdateEntityHealthCheckResultsAsync(comprehensiveHealthCheckReport, runtimeConfig);
}
// Updates the DataSource Health Check Results in the response.
private async Task UpdateDataSourceHealthCheckResultsAsync(ComprehensiveHealthCheckReport comprehensiveHealthCheckReport, RuntimeConfig runtimeConfig)
{
if (comprehensiveHealthCheckReport.Checks != null && runtimeConfig.DataSource.IsDatasourceHealthEnabled)
{
string query = Utilities.GetDatSourceQuery(runtimeConfig.DataSource.DatabaseType);
(int, string?) response = await ExecuteDatasourceQueryCheckAsync(query, runtimeConfig.DataSource.ConnectionString, Utilities.GetDbProviderFactory(runtimeConfig.DataSource.DatabaseType), runtimeConfig.DataSource.DatabaseType);
bool isResponseTimeWithinThreshold = response.Item1 >= 0 && response.Item1 < runtimeConfig.DataSource.DatasourceThresholdMs;
// Add DataSource Health Check Results
comprehensiveHealthCheckReport.Checks.Add(new HealthCheckResultEntry
{
Name = runtimeConfig?.DataSource?.Health?.Name ?? runtimeConfig?.DataSource?.DatabaseType.ToString(),
ResponseTimeData = new ResponseTimeData
{
ResponseTimeMs = response.Item1,
ThresholdMs = runtimeConfig?.DataSource?.DatasourceThresholdMs
},
Exception = !isResponseTimeWithinThreshold ? TIME_EXCEEDED_ERROR_MESSAGE : response.Item2,
Tags = [HealthCheckConstants.DATASOURCE],
Status = isResponseTimeWithinThreshold ? HealthStatus.Healthy : HealthStatus.Unhealthy
});
}
}
// Executes the DB Query and keeps track of the response time and error message.
private async Task<(int, string?)> ExecuteDatasourceQueryCheckAsync(string query, string connectionString, DbProviderFactory dbProviderFactory, DatabaseType databaseType)
{
string? errorMessage = null;
if (!string.IsNullOrEmpty(query) && !string.IsNullOrEmpty(connectionString))
{
Stopwatch stopwatch = new();
stopwatch.Start();
errorMessage = await _httpUtility.ExecuteDbQueryAsync(query, connectionString, dbProviderFactory, databaseType);
stopwatch.Stop();
return string.IsNullOrEmpty(errorMessage) ? ((int)stopwatch.ElapsedMilliseconds, errorMessage) : (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}
return (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}
// Updates the Entity Health Check Results in the response.
// Goes through the entities one by one and executes the rest and graphql checks (if enabled).
// Stored procedures are excluded from health checks because they require parameters and are not guaranteed to be deterministic.
private async Task UpdateEntityHealthCheckResultsAsync(ComprehensiveHealthCheckReport report, RuntimeConfig runtimeConfig)
{
List<KeyValuePair<string, Entity>> enabledEntities = runtimeConfig.Entities.Entities
.Where(e => e.Value.IsEntityHealthEnabled && e.Value.Source.Type != EntitySourceType.StoredProcedure)
.ToList();
if (enabledEntities.Count == 0)
{
_logger.LogInformation("No enabled entities found for health checks. Skipping entity health checks.");
return;
}
ConcurrentBag<HealthCheckResultEntry> concurrentChecks = new();
// Use MaxQueryParallelism from RuntimeConfig or default to RuntimeHealthCheckConfig.DEFAULT_MAX_QUERY_PARALLELISM
int maxParallelism = runtimeConfig.Runtime?.Health?.MaxQueryParallelism ?? RuntimeHealthCheckConfig.DEFAULT_MAX_QUERY_PARALLELISM;
_logger.LogInformation("Executing health checks for {Count} enabled entities with parallelism of {MaxParallelism}.", enabledEntities.Count, maxParallelism);
// Executes health checks for all enabled entities in parallel, with a maximum degree of parallelism
// determined by configuration (or a default). Each entity's health check runs as an independent task.
// Results are collected in a thread-safe ConcurrentBag. This approach significantly improves performance
// for large numbers of entities by utilizing available CPU and I/O resources efficiently.
await Parallel.ForEachAsync(enabledEntities, new ParallelOptions { MaxDegreeOfParallelism = maxParallelism }, async (entity, _) =>
{
try
{
ComprehensiveHealthCheckReport localReport = new()
{
Checks = new List<HealthCheckResultEntry>()
};
await PopulateEntityHealthAsync(localReport, entity, runtimeConfig);
if (localReport.Checks != null)
{
foreach (HealthCheckResultEntry check in localReport.Checks)
{
concurrentChecks.Add(check);
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error processing entity '{EntityKey}'", entity.Key);
}
});
report.Checks ??= new List<HealthCheckResultEntry>();
report.Checks.AddRange(concurrentChecks);
}
// Populates the Entity Health Check Results in the response for a particular entity.
// Checks for Rest enabled and executes the rest query.
// Checks for GraphQL enabled and executes the graphql query.
private async Task PopulateEntityHealthAsync(ComprehensiveHealthCheckReport comprehensiveHealthCheckReport, KeyValuePair<string, Entity> entity, RuntimeConfig runtimeConfig)
{
// Global Rest and GraphQL Runtime Options
RuntimeOptions? runtimeOptions = runtimeConfig.Runtime;
string entityKeyName = entity.Key;
// Entity Health Check and Runtime Options
Entity entityValue = entity.Value;
if (runtimeOptions != null && entityValue != null)
{
if (runtimeOptions.IsRestEnabled && entityValue.IsRestEnabled)
{
comprehensiveHealthCheckReport.Checks ??= new List<HealthCheckResultEntry>();
// In case of REST API, use the path specified in [entity.path] (if present).
// The path is trimmed to remove the leading '/' character.
// If the path is not present, use the entity key name as the path.
string entityPath = entityValue.Rest.Path != null ? entityValue.Rest.Path.TrimStart('/') : entityKeyName;
(int, string?) response = await ExecuteRestEntityQueryAsync(runtimeConfig.RestPath, entityPath, entityValue.EntityFirst);
bool isResponseTimeWithinThreshold = response.Item1 >= 0 && response.Item1 < entityValue.EntityThresholdMs;
// Add Entity Health Check Results
comprehensiveHealthCheckReport.Checks.Add(new HealthCheckResultEntry
{
Name = entityKeyName,
ResponseTimeData = new ResponseTimeData
{
ResponseTimeMs = response.Item1,
ThresholdMs = entityValue.EntityThresholdMs
},
Tags = [HealthCheckConstants.REST, HealthCheckConstants.ENDPOINT],
Exception = response.Item2 ?? (!isResponseTimeWithinThreshold ? TIME_EXCEEDED_ERROR_MESSAGE : null),
Status = isResponseTimeWithinThreshold ? HealthStatus.Healthy : HealthStatus.Unhealthy
});
}
if (runtimeOptions.IsGraphQLEnabled && entityValue.IsGraphQLEnabled)
{
comprehensiveHealthCheckReport.Checks ??= new List<HealthCheckResultEntry>();
(int, string?) response = await ExecuteGraphQlEntityQueryAsync(runtimeConfig.GraphQLPath, entityValue, entityKeyName);
bool isResponseTimeWithinThreshold = response.Item1 >= 0 && response.Item1 < entityValue.EntityThresholdMs;
comprehensiveHealthCheckReport.Checks.Add(new HealthCheckResultEntry
{
Name = entityKeyName,
ResponseTimeData = new ResponseTimeData
{
ResponseTimeMs = response.Item1,
ThresholdMs = entityValue.EntityThresholdMs
},
Tags = [HealthCheckConstants.GRAPHQL, HealthCheckConstants.ENDPOINT],
Exception = response.Item2 ?? (!isResponseTimeWithinThreshold ? TIME_EXCEEDED_ERROR_MESSAGE : null),
Status = isResponseTimeWithinThreshold ? HealthStatus.Healthy : HealthStatus.Unhealthy
});
}
}
}
// Executes the Rest Entity Query and keeps track of the response time and error message.
private async Task<(int, string?)> ExecuteRestEntityQueryAsync(string restUriSuffix, string entityName, int first)
{
string? errorMessage = null;
if (!string.IsNullOrEmpty(entityName))
{
Stopwatch stopwatch = new();
stopwatch.Start();
errorMessage = await _httpUtility.ExecuteRestQueryAsync(restUriSuffix, entityName, first, _incomingRoleHeader, _incomingRoleToken);
stopwatch.Stop();
return string.IsNullOrEmpty(errorMessage) ? ((int)stopwatch.ElapsedMilliseconds, errorMessage) : (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}
return (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}
// Executes the GraphQL Entity Query and keeps track of the response time and error message.
private async Task<(int, string?)> ExecuteGraphQlEntityQueryAsync(string graphqlUriSuffix, Entity entity, string entityName)
{
string? errorMessage = null;
if (entity != null)
{
Stopwatch stopwatch = new();
stopwatch.Start();
errorMessage = await _httpUtility.ExecuteGraphQLQueryAsync(graphqlUriSuffix, entityName, entity, _incomingRoleHeader, _incomingRoleToken);
stopwatch.Stop();
return string.IsNullOrEmpty(errorMessage) ? ((int)stopwatch.ElapsedMilliseconds, errorMessage) : (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}
return (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}
}
}