-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathHealthCheckHelper.cs
More file actions
520 lines (459 loc) · 27.5 KB
/
Copy pathHealthCheckHelper.cs
File metadata and controls
520 lines (459 loc) · 27.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// 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.Config.ObjectModel.Embeddings;
using Azure.DataApiBuilder.Core.Authorization;
using Azure.DataApiBuilder.Core.Services.Embeddings;
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 IEmbeddingService? _embeddingService;
private const string TIME_EXCEEDED_ERROR_MESSAGE = "The threshold for executing the request has exceeded.";
private const string DIMENSIONS_MISMATCH_ERROR_MESSAGE = "The embedding dimensions do not match the expected dimensions.";
/// <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>
/// <param name="embeddingService">Optional embedding service for embedding health checks.</param>
public HealthCheckHelper(ILogger<HealthCheckHelper> logger, HttpUtilities httpUtility, IEmbeddingService? embeddingService = null)
{
_logger = logger;
_httpUtility = httpUtility;
_embeddingService = embeddingService;
}
/// <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>
/// <param name="roleHeader">The effective role header for the current request.</param>
/// <param name="roleToken">The bearer token for the current request.</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, string roleHeader = "", string roleToken = "")
{
// 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, roleHeader, roleToken);
UpdateOverallHealthStatus(ref comprehensiveHealthCheckReport);
return comprehensiveHealthCheckReport;
}
// Reads the incoming role and token headers from the request and returns them as local values.
public (string roleHeader, string roleToken) ReadRoleHeaders(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.");
}
string roleHeader = clientRoleHeader.Count == 1 ? clientRoleHeader.ToString().ToLowerInvariant() : string.Empty;
string roleToken = clientTokenHeader.Count == 1 ? clientTokenHeader.ToString() : string.Empty;
return (roleHeader, roleToken);
}
// Returns the effective role for the current request.
// Falls back to "authenticated" if a bearer token is present, or "anonymous" otherwise.
public string GetCurrentRole(string roleHeader, string roleToken)
{
return !string.IsNullOrEmpty(roleHeader)
? roleHeader
: !string.IsNullOrEmpty(roleToken)
? AuthorizationResolver.ROLE_AUTHENTICATED
: AuthorizationResolver.ROLE_ANONYMOUS;
}
/// <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="isDevelopmentMode">Compare with the HostMode of DAB</param>
/// <param name="allowedRoles">AllowedRoles in the Runtime.Health config</param>
/// <param name="roleHeader">The effective role header for the current request.</param>
/// <returns></returns>
public bool IsUserAllowedToAccessHealthCheck(bool isDevelopmentMode, HashSet<string> allowedRoles, string roleHeader)
{
if (allowedRoles == null || allowedRoles.Count == 0)
{
// When allowedRoles is null or empty, all roles are allowed if Mode = Development.
return isDevelopmentMode;
}
return allowedRoles.Contains(roleHeader);
}
// 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)
{
bool embeddingsEnabled = runtimeConfig?.Runtime?.Embeddings?.Enabled ?? false;
bool embeddingsEndpointEnabled = embeddingsEnabled && (runtimeConfig?.Runtime?.Embeddings?.IsEndpointEnabled ?? false);
comprehensiveHealthCheckReport.ConfigurationDetails = new ConfigurationDetails
{
Rest = runtimeConfig?.IsRestEnabled ?? false,
GraphQL = runtimeConfig?.IsGraphQLEnabled ?? false,
Mcp = runtimeConfig?.IsMcpEnabled ?? false,
Caching = runtimeConfig?.IsCachingEnabled ?? false,
Telemetry = runtimeConfig?.Runtime?.Telemetry != null,
Mode = runtimeConfig?.Runtime?.Host?.Mode ?? HostMode.Production,
Embeddings = embeddingsEnabled,
EmbeddingsEndpoint = embeddingsEndpointEnabled
};
}
// Main function to internally call for data source and entities health check.
private async Task UpdateHealthCheckDetailsAsync(ComprehensiveHealthCheckReport comprehensiveHealthCheckReport, RuntimeConfig runtimeConfig, string roleHeader, string roleToken)
{
comprehensiveHealthCheckReport.Checks = new List<HealthCheckResultEntry>();
await UpdateDataSourceHealthCheckResultsAsync(comprehensiveHealthCheckReport, runtimeConfig);
await UpdateEntityHealthCheckResultsAsync(comprehensiveHealthCheckReport, runtimeConfig, roleHeader, roleToken);
await UpdateMcpHealthCheckResultsAsync(comprehensiveHealthCheckReport, runtimeConfig, roleHeader, roleToken);
await UpdateEmbeddingsHealthCheckResultsAsync(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 is not 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 MCP Health Check Result in the response.
// The check verifies that the MCP endpoint is reachable and responds within the threshold.
// It runs only when MCP is enabled in the runtime configuration.
private async Task UpdateMcpHealthCheckResultsAsync(ComprehensiveHealthCheckReport comprehensiveHealthCheckReport, RuntimeConfig runtimeConfig, string roleHeader, string roleToken)
{
if (comprehensiveHealthCheckReport.Checks is null || !runtimeConfig.IsMcpEnabled)
{
return;
}
(int, string?) response = await ExecuteMcpQueryAsync(runtimeConfig.McpPath, roleHeader, roleToken);
bool isResponseTimeWithinThreshold = response.Item1 >= 0 && response.Item1 < HealthCheckConstants.DEFAULT_THRESHOLD_RESPONSE_TIME_MS;
comprehensiveHealthCheckReport.Checks.Add(new HealthCheckResultEntry
{
Name = HealthCheckConstants.MCP,
ResponseTimeData = new ResponseTimeData
{
ResponseTimeMs = response.Item1,
ThresholdMs = HealthCheckConstants.DEFAULT_THRESHOLD_RESPONSE_TIME_MS
},
Tags = [HealthCheckConstants.MCP],
Exception = response.Item2 ?? (!isResponseTimeWithinThreshold ? TIME_EXCEEDED_ERROR_MESSAGE : null),
Status = isResponseTimeWithinThreshold ? HealthStatus.Healthy : HealthStatus.Unhealthy
});
}
// Executes the MCP Query and keeps track of the response time and error message.
private async Task<(int, string?)> ExecuteMcpQueryAsync(string mcpUriSuffix, string roleHeader, string roleToken)
{
if (string.IsNullOrEmpty(mcpUriSuffix))
{
return (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, "MCP is enabled but no MCP path is configured.");
}
Stopwatch stopwatch = new();
stopwatch.Start();
string? errorMessage = await _httpUtility.ExecuteMcpQueryAsync(mcpUriSuffix, roleHeader, roleToken);
stopwatch.Stop();
return string.IsNullOrEmpty(errorMessage)
? ((int)stopwatch.ElapsedMilliseconds, errorMessage)
: (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, string roleHeader, string roleToken)
{
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, roleHeader, roleToken);
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, string roleHeader, string roleToken)
{
// 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, roleHeader, roleToken);
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, roleHeader, roleToken);
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 roleHeader, string roleToken)
{
string? errorMessage = null;
if (!string.IsNullOrEmpty(entityName))
{
Stopwatch stopwatch = new();
stopwatch.Start();
errorMessage = await _httpUtility.ExecuteRestQueryAsync(restUriSuffix, entityName, first, roleHeader, roleToken);
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 roleHeader, string roleToken)
{
string? errorMessage = null;
if (entity != null)
{
Stopwatch stopwatch = new();
stopwatch.Start();
errorMessage = await _httpUtility.ExecuteGraphQLQueryAsync(graphqlUriSuffix, entityName, entity, roleHeader, roleToken);
stopwatch.Stop();
return string.IsNullOrEmpty(errorMessage) ? ((int)stopwatch.ElapsedMilliseconds, errorMessage) : (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}
return (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}
/// <summary>
/// Updates the Embeddings Health Check Results in the response.
/// Executes a test embedding and validates response time and optionally dimensions.
/// </summary>
private async Task UpdateEmbeddingsHealthCheckResultsAsync(ComprehensiveHealthCheckReport comprehensiveHealthCheckReport, RuntimeConfig runtimeConfig)
{
EmbeddingsOptions? embeddingsOptions = runtimeConfig?.Runtime?.Embeddings;
EmbeddingsHealthCheckConfig? healthConfig = embeddingsOptions?.Health;
// Only run health check if embeddings is enabled, health check is enabled, and embedding service is available
if (embeddingsOptions is null || !embeddingsOptions.Enabled || healthConfig is null || !healthConfig.Enabled || _embeddingService is null)
{
return;
}
if (comprehensiveHealthCheckReport.Checks is null)
{
comprehensiveHealthCheckReport.Checks = new List<HealthCheckResultEntry>();
}
string testText = healthConfig.TestText;
int thresholdMs = healthConfig.ThresholdMs;
int? expectedDimensions = healthConfig.ExpectedDimensions;
try
{
Stopwatch stopwatch = new();
stopwatch.Start();
EmbeddingResult result = await _embeddingService.TryEmbedAsync(testText);
stopwatch.Stop();
int responseTimeMs = (int)stopwatch.ElapsedMilliseconds;
bool isResponseTimeWithinThreshold = responseTimeMs <= thresholdMs;
bool isDimensionsValid = false;
string? errorMessage = null;
if (!result.Success)
{
errorMessage = result.ErrorMessage ?? "Embedding request failed.";
comprehensiveHealthCheckReport.Checks.Add(new HealthCheckResultEntry
{
Name = "embeddings",
ResponseTimeData = new ResponseTimeData
{
ResponseTimeMs = HealthCheckConstants.ERROR_RESPONSE_TIME_MS,
ThresholdMs = thresholdMs
},
Exception = errorMessage,
Tags = new List<string> { HealthCheckConstants.EMBEDDING },
Status = HealthStatus.Unhealthy
});
return;
}
// Validate dimensions if expected dimensions is specified
if (expectedDimensions.HasValue && result.Embedding is not null)
{
isDimensionsValid = result.Embedding.Length == expectedDimensions.Value;
if (!isDimensionsValid)
{
errorMessage = $"{DIMENSIONS_MISMATCH_ERROR_MESSAGE} Expected: {expectedDimensions.Value}, Actual: {result.Embedding.Length}";
}
}
else if (!expectedDimensions.HasValue)
{
// If no expected dimensions are specified, consider dimensions valid
isDimensionsValid = true;
}
// Check response time threshold
if (!isResponseTimeWithinThreshold)
{
errorMessage = errorMessage is null ? TIME_EXCEEDED_ERROR_MESSAGE : $"{errorMessage} {TIME_EXCEEDED_ERROR_MESSAGE}";
}
bool isHealthy = isResponseTimeWithinThreshold && isDimensionsValid;
comprehensiveHealthCheckReport.Checks.Add(new HealthCheckResultEntry
{
Name = "embeddings",
ResponseTimeData = new ResponseTimeData
{
ResponseTimeMs = responseTimeMs,
ThresholdMs = thresholdMs
},
Exception = errorMessage,
Tags = new List<string> { HealthCheckConstants.EMBEDDING },
Status = isHealthy ? HealthStatus.Healthy : HealthStatus.Unhealthy
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Error executing embeddings health check.");
comprehensiveHealthCheckReport.Checks.Add(new HealthCheckResultEntry
{
Name = "embeddings",
ResponseTimeData = new ResponseTimeData
{
ResponseTimeMs = HealthCheckConstants.ERROR_RESPONSE_TIME_MS,
ThresholdMs = thresholdMs
},
Exception = ex.Message,
Tags = new List<string> { HealthCheckConstants.EMBEDDING },
Status = HealthStatus.Unhealthy
});
}
}
}
}