Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
if (Options.UsePeekMode)
await CheckWithReceiver().ConfigureAwait(false);
else

if (Options.UseCreateMessageBatchAsyncMode)
await CheckWithSender().ConfigureAwait(false);

if (Options.UsePeekMode is false && Options.UseCreateMessageBatchAsyncMode is false)
await CheckWithManagement().ConfigureAwait(false);

return HealthCheckResult.Healthy();
Expand All @@ -47,6 +51,17 @@ async Task CheckWithReceiver()
await receiver.PeekMessageAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

async Task CheckWithSender()
{
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient()).ConfigureAwait(false);
var sender = await ClientCache.GetOrAddAsyncDisposableAsync(
_queueKey,
_ => client.CreateSender(Options.QueueName))
.ConfigureAwait(false);

await sender.CreateMessageBatchAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

Task CheckWithManagement()
{
var managementClient = ClientCache.GetOrAdd(ConnectionKey, _ => CreateManagementClient());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ namespace HealthChecks.AzureServiceBus;

public class AzureServiceBusTopicHealthCheck : AzureServiceBusHealthCheck<AzureServiceBusTopicHealthCheckOptions>, IHealthCheck
{
private readonly string _topicKey;

public AzureServiceBusTopicHealthCheck(AzureServiceBusTopicHealthCheckOptions options, ServiceBusClientProvider clientProvider)
: base(options, clientProvider)
{
Guard.ThrowIfNull(options.TopicName, true);

_topicKey = $"{nameof(AzureServiceBusTopicHealthCheck)}_{ConnectionKey}_{Options.TopicName}";
}

public AzureServiceBusTopicHealthCheck(AzureServiceBusTopicHealthCheckOptions options)
Expand All @@ -21,15 +25,34 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
var managementClient = ClientCache.GetOrAdd(ConnectionKey, _ => CreateManagementClient());

_ = await managementClient.GetTopicRuntimePropertiesAsync(Options.TopicName, cancellationToken).ConfigureAwait(false);
if (Options.UseCreateMessageBatchAsyncMode)
await CheckWithSender().ConfigureAwait(false);
else
await CheckWithManagement().ConfigureAwait(false);

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

async Task CheckWithSender()
{
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient()).ConfigureAwait(false);
var sender = await ClientCache.GetOrAddAsyncDisposableAsync(
_topicKey,
_ => client.CreateSender(Options.TopicName))
.ConfigureAwait(false);

await sender.CreateMessageBatchAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

Task CheckWithManagement()
{
var managementClient = ClientCache.GetOrAdd(ConnectionKey, _ => CreateManagementClient());

return managementClient.GetTopicRuntimePropertiesAsync(Options.TopicName, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public class AzureServiceBusQueueHealthCheckOptions : AzureServiceBusHealthCheck
/// </remarks>
public bool UsePeekMode { get; set; } = true;

/// <summary>
/// Will use <c>CreateMessageBatchAsync</c> method to determine status if set to <see langword="true"/> (default),
/// otherwise; will use <c>GetProperties*</c> method.
/// </summary>
/// <remarks>
/// CreateMessageBatch requires Send claim to work. However, if only Receiver claim using the Azure built-in roles (RBAC)
/// <see href="https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles/integration#azure-service-bus-data-receiver">Azure Service Bus Data Receiver</see>
/// is used set this to <see langword="false"/>. By default <see langword="true"/>.
/// </remarks>
public bool UseCreateMessageBatchAsyncMode { get; set; } = true;

public AzureServiceBusQueueHealthCheckOptions(string queueName)
{
QueueName = queueName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ public class AzureServiceBusTopicHealthCheckOptions : AzureServiceBusHealthCheck
/// </summary>
public string TopicName { get; set; }

/// <summary>
/// Will use <c>CreateMessageBatchAsync</c> method to determine status if set to <see langword="true"/> (default),
/// otherwise; will use <c>GetProperties*</c> method.
/// </summary>
/// <remarks>
/// CreateMessageBatch requires Send claim to work. However, if only Receiver claim using the Azure built-in roles (RBAC)
/// <see href="https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles/integration#azure-service-bus-data-receiver">Azure Service Bus Data Receiver</see>
/// is used set this to <see langword="false"/>. By default <see langword="true"/>.
/// </remarks>
public bool UseCreateMessageBatchAsyncMode { get; set; } = true;

public AzureServiceBusTopicHealthCheckOptions(string topicName)
{
TopicName = topicName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" />
</ItemGroup>

</Project>
Loading