Skip to content

Commit

Permalink
Merge pull request #93 from wemogy/fix/AzureServiceBusHealthCheck
Browse files Browse the repository at this point in the history
fix: improved AzureServiceBusHealthCheck logging
  • Loading branch information
SebastianKuesters authored Jul 2, 2024
2 parents 6839f1a + 08224e2 commit 21d276e
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,29 @@ public AzureServiceBusHealthCheck(string connectionString, string queueName)

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
using var activity = Observability.DefaultActivities.StartActivity();
using var activity = Observability.DefaultActivities.StartActivity("AzureServiceBusHealthCheck.CheckHealth");
try
{
var client = ClientConnections.GetOrAdd(_queueName, _ => new ServiceBusClient(_connectionString));
var receiver = ServiceBusReceivers.GetOrAdd($"{nameof(AzureServiceBusHealthCheck)}_{_queueName}", client.CreateReceiver(_queueName));
using var peekMessageActivity = Observability.DefaultActivities.StartActivity("AzureServiceBusHealthCheck.PeekMessage");
_ = await receiver.PeekMessageAsync(cancellationToken: cancellationToken);
peekMessageActivity?.SetStatus(Status.Ok);
peekMessageActivity?.Stop();
return HealthCheckResult.Healthy();
}
catch (TaskCanceledException ex)
catch (OperationCanceledException ex)
{
activity?.RecordException(ex);

// propagate the exception if the cancellation token has been canceled
// propagate the exception if the cancellation token has been canceled, so that the health check is not marked as unhealthy
// the ExceptionHandlerMiddleware will handle the exception
if (cancellationToken.IsCancellationRequested)
{
throw;
}

// Record the exception in the activity
activity?.RecordException(ex);

return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
catch (Exception ex)
Expand Down

0 comments on commit 21d276e

Please sign in to comment.