Skip to content

Commit

Permalink
split AzureServiceBus into dedicated nuget packages
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Sep 14, 2023
1 parent 67b8b48 commit 964ad22
Show file tree
Hide file tree
Showing 23 changed files with 309 additions and 223 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/healthchecks_azureservicebus_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ jobs:
run: dotnet restore ./src/HealthChecks.AzureServiceBus/HealthChecks.AzureServiceBus.csproj
- name: Build
run: dotnet build --no-restore ./src/HealthChecks.AzureServiceBus/HealthChecks.AzureServiceBus.csproj -c $BUILD_CONFIG
- name: Pack EventHubs
run: dotnet pack --no-build ./src/HealthChecks.Azure.Messaging.EventHubs/HealthChecks.Azure.Messaging.EventHubs.csproj -c $BUILD_CONFIG -o ./artifacts
- name: Pack ServiceBus
run: dotnet pack --no-build ./src/HealthChecks.Azure.Messaging.ServiceBus/HealthChecks.Azure.Messaging.ServiceBus.csproj -c $BUILD_CONFIG -o ./artifacts
- name: Pack
run: dotnet pack --no-build ./src/HealthChecks.AzureServiceBus/HealthChecks.AzureServiceBus.csproj -c $BUILD_CONFIG -o ./artifacts
- name: Publish EventHubs
run: dotnet nuget push ./artifacts/HealthChecks.Azure.Messaging.EventHubs.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
- name: Publish ServiceBus
run: dotnet nuget push ./artifacts/HealthChecks.Azure.Messaging.ServiceBus.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
- name: Publish
run: dotnet nuget push ./artifacts/AspNetCore.HealthChecks.AzureServiceBus.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
10 changes: 9 additions & 1 deletion .github/workflows/healthchecks_azureservicebus_cd_preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ jobs:
run: dotnet restore ./src/HealthChecks.AzureServiceBus/HealthChecks.AzureServiceBus.csproj
- name: Build
run: dotnet build --no-restore ./src/HealthChecks.AzureServiceBus/HealthChecks.AzureServiceBus.csproj -c $BUILD_CONFIG
- name: Pack EventHubs
run: dotnet pack --no-build ./src/HealthChecks.Azure.Messaging.EventHubs/HealthChecks.Azure.Messaging.EventHubs.csproj --version-suffix $VERSION_SUFFIX -c $BUILD_CONFIG -o ./artifacts
- name: Pack ServiceBus
run: dotnet pack --no-build ./src/HealthChecks.Azure.Messaging.ServiceBus/HealthChecks.Azure.Messaging.ServiceBus.csproj --version-suffix $VERSION_SUFFIX -c $BUILD_CONFIG -o ./artifacts
- name: Pack
run: dotnet pack --no-build ./src/HealthChecks.AzureServiceBus/HealthChecks.AzureServiceBus.csproj --version-suffix $VERSION_SUFFIX -c $BUILD_CONFIG -o ./artifacts
- name: Publish EventHubs
run: dotnet nuget push ./artifacts/HealthChecks.Azure.Messaging.EventHubs.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
- name: Publish ServiceBus
run: dotnet nuget push ./artifacts/HealthChecks.Azure.Messaging.ServiceBus.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
- name: Publish
run: dotnet nuget push ./artifacts/AspNetCore.HealthChecks.AzureServiceBus.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
run: dotnet nuget push ./artifacts/AspNetCore.HealthChecks.AzureServiceBus.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
2 changes: 2 additions & 0 deletions .github/workflows/healthchecks_azureservicebus_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
push:
branches: [ master ]
paths:
- src/HealthChecks.Azure.Messaging.*/**
- src/HealthChecks.AzureServiceBus/**
- test/HealthChecks.AzureServiceBus.Tests/**
- test/_SHARED/**
Expand All @@ -18,6 +19,7 @@ on:
pull_request:
branches: [ master ]
paths:
- src/HealthChecks.Azure.Messaging.*/**
- src/HealthChecks.AzureServiceBus/**
- test/HealthChecks.AzureServiceBus.Tests/**
- test/_SHARED/**
Expand Down
14 changes: 14 additions & 0 deletions AspNetCore.Diagnostics.HealthChecks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.Azure.Storage.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.Azure.Storage.Files.Shares", "src\HealthChecks.Azure.Storage.Files.Shares\HealthChecks.Azure.Storage.Files.Shares.csproj", "{94031EF0-8262-46BE-9CF0-4E357CAE27AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.Azure.Messaging.ServiceBus", "src\HealthChecks.Azure.Messaging.ServiceBus\HealthChecks.Azure.Messaging.ServiceBus.csproj", "{C84C0A78-B520-4B21-95D1-9113A6B46611}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.Azure.Messaging.EventHubs", "src\HealthChecks.Azure.Messaging.EventHubs\HealthChecks.Azure.Messaging.EventHubs.csproj", "{87F760F2-C265-4898-8B9F-2C78532BC321}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -825,6 +829,14 @@ Global
{94031EF0-8262-46BE-9CF0-4E357CAE27AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94031EF0-8262-46BE-9CF0-4E357CAE27AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94031EF0-8262-46BE-9CF0-4E357CAE27AE}.Release|Any CPU.Build.0 = Release|Any CPU
{C84C0A78-B520-4B21-95D1-9113A6B46611}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C84C0A78-B520-4B21-95D1-9113A6B46611}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C84C0A78-B520-4B21-95D1-9113A6B46611}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C84C0A78-B520-4B21-95D1-9113A6B46611}.Release|Any CPU.Build.0 = Release|Any CPU
{87F760F2-C265-4898-8B9F-2C78532BC321}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87F760F2-C265-4898-8B9F-2C78532BC321}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87F760F2-C265-4898-8B9F-2C78532BC321}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87F760F2-C265-4898-8B9F-2C78532BC321}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -961,6 +973,8 @@ Global
{35994A03-ABFD-4016-865E-907CCA6B0175} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{D51E2882-48E2-4384-A729-4FB627C6DB91} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{94031EF0-8262-46BE-9CF0-4E357CAE27AE} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{C84C0A78-B520-4B21-95D1-9113A6B46611} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{87F760F2-C265-4898-8B9F-2C78532BC321} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2B8C62A1-11B6-469F-874C-A02443256568}
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<WarningsNotAsErrors>$(WarningsNotAsErrors);IDE0053;IDE0060</WarningsNotAsErrors>
</PropertyGroup>

<ItemGroup Condition="$(MSBuildProjectName) == 'HealthChecks.AzureServiceBus' OR $(MSBuildProjectName) == 'HealthChecks.CosmosDb'">
<ItemGroup Condition="$(MSBuildProjectName) == 'HealthChecks.CosmosDb'">
<Compile Include="../ClientCache.cs" Link="ClientCache.cs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageTags>$(PackageTags);Azure;EventHub</PackageTags>
<Description>HealthChecks.Azure.Messaging.EventHubs is the health check package for Azure EventHub.</Description>
<VersionPrefix>$(HealthCheckAzureServiceBus)</VersionPrefix>
</PropertyGroup>

<ItemGroup>
<Compile Include="../ClientCache.cs" Link="ClientCache.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.9.2" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.9" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
using HealthChecks.AzureServiceBus.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace HealthChecks.AzureServiceBus;

public class AzureServiceBusQueueHealthCheck : AzureServiceBusHealthCheck<AzureServiceBusQueueHealthCheckOptions>, IHealthCheck
{
private readonly string _queueKey;

public AzureServiceBusQueueHealthCheck(AzureServiceBusQueueHealthCheckOptions options, ServiceBusClientProvider clientProvider)
: base(options, clientProvider)
{
Guard.ThrowIfNull(options.QueueName, true);

_queueKey = $"{nameof(AzureServiceBusQueueHealthCheck)}_{ConnectionKey}_{Options.QueueName}";
}

public AzureServiceBusQueueHealthCheck(AzureServiceBusQueueHealthCheckOptions options)
: this(options, new ServiceBusClientProvider())
{ }

/// <inheritdoc />
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
if (Options.UsePeekMode)
await CheckWithReceiver().ConfigureAwait(false);
else
await CheckWithManagement().ConfigureAwait(false);

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

async Task CheckWithReceiver()
{
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient()).ConfigureAwait(false);
var receiver = await ClientCache.GetOrAddAsyncDisposableAsync(
_queueKey,
_ => client.CreateReceiver(Options.QueueName))
.ConfigureAwait(false);

await receiver.PeekMessageAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

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

return managementClient.GetQueueRuntimePropertiesAsync(Options.QueueName, cancellationToken);
}
}
}
using HealthChecks.AzureServiceBus.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace HealthChecks.AzureServiceBus;

public class AzureServiceBusQueueHealthCheck : AzureServiceBusHealthCheck<AzureServiceBusQueueHealthCheckOptions>, IHealthCheck
{
private readonly string _queueKey;

public AzureServiceBusQueueHealthCheck(AzureServiceBusQueueHealthCheckOptions options, ServiceBusClientProvider clientProvider)
: base(options, clientProvider)
{
Guard.ThrowIfNull(options.QueueName, true);

_queueKey = $"{nameof(AzureServiceBusQueueHealthCheck)}_{ConnectionKey}_{Options.QueueName}";
}

public AzureServiceBusQueueHealthCheck(AzureServiceBusQueueHealthCheckOptions options)
: this(options, new ServiceBusClientProvider())
{ }

/// <inheritdoc />
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
if (Options.UsePeekMode)
await CheckWithReceiver().ConfigureAwait(false);
else
await CheckWithManagement().ConfigureAwait(false);

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

async Task CheckWithReceiver()
{
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient()).ConfigureAwait(false);
var receiver = await ClientCache.GetOrAddAsyncDisposableAsync(
_queueKey,
_ => client.CreateReceiver(Options.QueueName))
.ConfigureAwait(false);

await receiver.PeekMessageAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

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

return managementClient.GetQueueRuntimePropertiesAsync(Options.QueueName, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
using HealthChecks.AzureServiceBus.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace HealthChecks.AzureServiceBus;

public class AzureServiceBusQueueMessageCountThresholdHealthCheck : AzureServiceBusHealthCheck<AzureServiceBusQueueMessagesCountThresholdHealthCheckOptions>, IHealthCheck
{
private readonly string _queueName;
private readonly AzureServiceBusQueueMessagesCountThreshold? _activeMessagesThreshold;
private readonly AzureServiceBusQueueMessagesCountThreshold? _deadLetterMessagesThreshold;

public AzureServiceBusQueueMessageCountThresholdHealthCheck(AzureServiceBusQueueMessagesCountThresholdHealthCheckOptions options, ServiceBusClientProvider clientProvider)
: base(options, clientProvider)
{
_queueName = Guard.ThrowIfNull(options.QueueName);
_activeMessagesThreshold = options.ActiveMessages;
_deadLetterMessagesThreshold = options.DeadLetterMessages;
}

public AzureServiceBusQueueMessageCountThresholdHealthCheck(AzureServiceBusQueueMessagesCountThresholdHealthCheckOptions options)
: this(options, new ServiceBusClientProvider())
{ }

/// <inheritdoc />
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var managementClient = ClientCache.GetOrAdd(ConnectionKey, _ => CreateManagementClient());

var properties = await managementClient.GetQueueRuntimePropertiesAsync(_queueName, cancellationToken).ConfigureAwait(false);

var activeQueueHealthStatus = CheckHealthStatus(
properties.Value.ActiveMessageCount,
_activeMessagesThreshold,
"queue");

if (activeQueueHealthStatus.Status != HealthStatus.Healthy)
{
return activeQueueHealthStatus;
}

var deadLetterQueueHealthStatus = CheckHealthStatus(
properties.Value.DeadLetterMessageCount,
_deadLetterMessagesThreshold,
"dead letter queue");

if (deadLetterQueueHealthStatus.Status != HealthStatus.Healthy)
{
return deadLetterQueueHealthStatus;
}

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

private HealthCheckResult CheckHealthStatus(
long messagesCount,
AzureServiceBusQueueMessagesCountThreshold? threshold,
string queueType)
{
if (threshold is null)
{
return HealthCheckResult.Healthy();
}

if (messagesCount >= threshold.Value.UnhealthyThreshold)
{
return HealthCheckResult.Unhealthy($"Message in {queueType} {_queueName} exceeded the amount of messages allowed for the unhealthy threshold {threshold.Value.UnhealthyThreshold}/{messagesCount}");
}

if (messagesCount >= threshold.Value.DegradedThreshold)
{
return HealthCheckResult.Degraded($"Message in {queueType} {_queueName} exceeded the amount of messages allowed for the degraded threshold {threshold.Value.DegradedThreshold}/{messagesCount}");
}

return HealthCheckResult.Healthy();
}
}
using HealthChecks.AzureServiceBus.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace HealthChecks.AzureServiceBus;

public class AzureServiceBusQueueMessageCountThresholdHealthCheck : AzureServiceBusHealthCheck<AzureServiceBusQueueMessagesCountThresholdHealthCheckOptions>, IHealthCheck
{
private readonly string _queueName;
private readonly AzureServiceBusQueueMessagesCountThreshold? _activeMessagesThreshold;
private readonly AzureServiceBusQueueMessagesCountThreshold? _deadLetterMessagesThreshold;

public AzureServiceBusQueueMessageCountThresholdHealthCheck(AzureServiceBusQueueMessagesCountThresholdHealthCheckOptions options, ServiceBusClientProvider clientProvider)
: base(options, clientProvider)
{
_queueName = Guard.ThrowIfNull(options.QueueName);
_activeMessagesThreshold = options.ActiveMessages;
_deadLetterMessagesThreshold = options.DeadLetterMessages;
}

public AzureServiceBusQueueMessageCountThresholdHealthCheck(AzureServiceBusQueueMessagesCountThresholdHealthCheckOptions options)
: this(options, new ServiceBusClientProvider())
{ }

/// <inheritdoc />
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var managementClient = ClientCache.GetOrAdd(ConnectionKey, _ => CreateManagementClient());

var properties = await managementClient.GetQueueRuntimePropertiesAsync(_queueName, cancellationToken).ConfigureAwait(false);

var activeQueueHealthStatus = CheckHealthStatus(
properties.Value.ActiveMessageCount,
_activeMessagesThreshold,
"queue");

if (activeQueueHealthStatus.Status != HealthStatus.Healthy)
{
return activeQueueHealthStatus;
}

var deadLetterQueueHealthStatus = CheckHealthStatus(
properties.Value.DeadLetterMessageCount,
_deadLetterMessagesThreshold,
"dead letter queue");

if (deadLetterQueueHealthStatus.Status != HealthStatus.Healthy)
{
return deadLetterQueueHealthStatus;
}

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

private HealthCheckResult CheckHealthStatus(
long messagesCount,
AzureServiceBusQueueMessagesCountThreshold? threshold,
string queueType)
{
if (threshold is null)
{
return HealthCheckResult.Healthy();
}

if (messagesCount >= threshold.Value.UnhealthyThreshold)
{
return HealthCheckResult.Unhealthy($"Message in {queueType} {_queueName} exceeded the amount of messages allowed for the unhealthy threshold {threshold.Value.UnhealthyThreshold}/{messagesCount}");
}

if (messagesCount >= threshold.Value.DegradedThreshold)
{
return HealthCheckResult.Degraded($"Message in {queueType} {_queueName} exceeded the amount of messages allowed for the degraded threshold {threshold.Value.DegradedThreshold}/{messagesCount}");
}

return HealthCheckResult.Healthy();
}
}
Loading

0 comments on commit 964ad22

Please sign in to comment.