private static Func <ValueTask <HealthCheckResult> > CheckEventHubConnectivity(string name, EventHubClient eventHubClient) { return(async() => { var result = true; try { await eventHubClient.GetRuntimeInformationAsync().ConfigureAwait(false); } catch (Exception ex) { Logger.ErrorException($"{name} failed.", ex); result = false; } return result ? HealthCheckResult.Healthy($"OK. '{eventHubClient.EventHubName}' is available.") : HealthCheckResult.Unhealthy($"Failed. '{eventHubClient.EventHubName}' is unavailable."); }); }
private static Func <ValueTask <HealthCheckResult> > CheckDocumentDBDatabaseConnectivity(Uri databaseUri, string endpointUri, string key) { return(async() => { bool result; try { using (var documentClient = new DocumentClient(new Uri(endpointUri), key)) { var token = new CancellationTokenSource(); token.CancelAfter(TimeSpan.FromSeconds(10)); await documentClient.OpenAsync(token.Token).ConfigureAwait(false); var database = await documentClient.ReadDatabaseAsync(databaseUri); result = database?.StatusCode == HttpStatusCode.OK; } } catch (DocumentClientException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { Logger.ErrorException($"{databaseUri} was not found.", ex); result = false; } catch (Exception ex) { Logger.ErrorException($"{databaseUri} failed.", ex); result = false; } return result ? HealthCheckResult.Healthy($"OK. '{databaseUri}' is available.") : HealthCheckResult.Unhealthy($"Failed. '{databaseUri}' is unavailable."); }); }
/// <summary> /// Executes the health check asynchrously /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns> /// The <see cref="Result" /> of running the health check /// </returns> public async ValueTask <Result> ExecuteAsync(CancellationToken cancellationToken = default) { try { if (HasCacheDuration()) { return(await ExecuteWithCachingAsync(cancellationToken)); } if (HasQuiteTime()) { return(await ExecuteWithQuiteTimeAsync(cancellationToken)); } var checkResult = await CheckAsync(cancellationToken); return(new Result(Name, checkResult)); } catch (Exception ex) when(!(ex is OperationCanceledException)) { return(new Result(Name, HealthCheckResult.Unhealthy(ex))); } }
private static Func <ValueTask <HealthCheckResult> > CheckStorageAccountConnectivity(string name, CloudStorageAccount storageAccount) { var queueClient = storageAccount.CreateCloudQueueClient(); return(async() => { var result = true; try { await queueClient.GetServicePropertiesAsync().ConfigureAwait(false); } catch (Exception ex) { Logger.ErrorException($"{name} failed.", ex); result = false; } return result ? HealthCheckResult.Healthy($"OK. '{storageAccount.BlobStorageUri}' is available.") : HealthCheckResult.Unhealthy($"Failed. '{storageAccount.BlobStorageUri}' is unavailable."); }); }
private static Func <ValueTask <HealthCheckResult> > CheckMessageCount(string name, CloudStorageAccount storageAccount, string queueName, long degradedThreshold, long?unhealthyThreshold) { var queue = storageAccount .CreateCloudQueueClient() .GetQueueReference(queueName); return(async() => { int?result = null; try { await queue.FetchAttributesAsync().ConfigureAwait(false); result = queue.ApproximateMessageCount; } catch (Exception ex) { Logger.ErrorException($"{name} failed.", ex); return HealthCheckResult.Unhealthy($"Failed. Unable to check queue '{queueName}'."); } if (result > 0 && result >= unhealthyThreshold) { return HealthCheckResult.Unhealthy($"Unhealthy. '{queueName}' has {result.Value} messages."); } if (result > 0 && result >= degradedThreshold) { return HealthCheckResult.Degraded($"Degraded. '{queueName}' has {result.Value} messages."); } return HealthCheckResult.Healthy($"OK. '{queueName}' has {result} messages."); }); }
private static Func <ValueTask <HealthCheckResult> > CheckServiceBusTopicConnectivity(string name, TopicClient topicClient) { return(async() => { var result = true; try { var id = await topicClient.ScheduleMessageAsync(HealthMessage, HealthMessageTestSchedule).ConfigureAwait(false); await topicClient.CancelScheduledMessageAsync(id); } catch (Exception ex) { Logger.ErrorException($"{name} failed.", ex); result = false; } return result ? HealthCheckResult.Healthy($"OK. '{topicClient.Path}/{topicClient.TopicName}' is available.") : HealthCheckResult.Unhealthy($"Failed. '{topicClient.Path}/{topicClient.TopicName}' is unavailable."); }); }
/// <summary> /// Create a failure (degraded or unhealthy) status response. /// </summary> /// <param name="message">Status message.</param> /// <param name="degradedOnError"> /// If true, create a degraded status response. /// Otherwise create an unhealthy status response. (default: false) /// </param> /// <returns>Failure status response.</returns> private static HealthCheckResult HealthCheckResultOnError(string message, bool degradedOnError) { return(degradedOnError ? HealthCheckResult.Degraded(message) : HealthCheckResult.Unhealthy(message)); }