-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sample for health checks (#585)
- Loading branch information
1 parent
8abe976
commit d2292b9
Showing
11 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Azure.Messaging.ServiceBus.Administration; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Options; | ||
using Tingle.EventBus.Transports.Azure.ServiceBus; | ||
|
||
namespace HealthCheck; | ||
|
||
internal class AzureServiceBusHealthCheck(IOptionsMonitor<AzureServiceBusTransportOptions> optionsMonitor) : IHealthCheck | ||
{ | ||
private const string QueueName = "health-check"; | ||
|
||
private readonly AzureServiceBusTransportOptions options = optionsMonitor?.Get(AzureServiceBusDefaults.Name) ?? throw new ArgumentNullException(nameof(optionsMonitor)); | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
var cred = (AzureServiceBusTransportCredentials)options.Credentials.CurrentValue; | ||
var managementClient = new ServiceBusAdministrationClient( | ||
fullyQualifiedNamespace: cred.FullyQualifiedNamespace, | ||
credential: cred.TokenCredential); | ||
|
||
_ = await managementClient.GetQueueRuntimePropertiesAsync(QueueName, cancellationToken); | ||
|
||
return HealthCheckResult.Healthy(); | ||
} | ||
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested) | ||
{ | ||
// ignore long running calls | ||
return HealthCheckResult.Healthy(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" Version="1.10.4" /> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Tingle.EventBus.Transports.Azure.ServiceBus\Tingle.EventBus.Transports.Azure.ServiceBus.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Azure.Identity; | ||
using HealthCheck; | ||
using Tingle.EventBus.Configuration; | ||
|
||
var host = Host.CreateDefaultBuilder(args) | ||
.ConfigureServices((hostContext, services) => | ||
{ | ||
var configuration = hostContext.Configuration; | ||
|
||
services.AddEventBus(builder => | ||
{ | ||
builder.AddConsumer<VehicleTelemetryEventsConsumer>(); | ||
|
||
var credential = new DefaultAzureCredential(); | ||
|
||
// Transport specific configuration | ||
builder.AddAzureServiceBusTransport(options => | ||
{ | ||
options.Credentials = new AzureServiceBusTransportCredentials | ||
{ | ||
TokenCredential = credential, | ||
FullyQualifiedNamespace = "{your_namespace}.servicebus.windows.net" | ||
}; | ||
options.DefaultEntityKind = EntityKind.Queue; // required if using the basic SKU (does not support topics) | ||
}); | ||
|
||
builder.Services.AddHealthChecks() | ||
.AddCheck<AzureServiceBusHealthCheck>(name: "servicebus", tags: ["eventbus"]); | ||
}); | ||
}) | ||
.Build(); | ||
|
||
await host.RunAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"profiles": { | ||
"HealthCheck": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace HealthCheck; | ||
|
||
public class VehicleDoorOpenedEvent | ||
{ | ||
public string? VehicleId { get; set; } | ||
public VehicleDoorKind Kind { get; set; } | ||
public DateTimeOffset? Opened { get; set; } | ||
public DateTimeOffset? Closed { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace HealthCheck; | ||
|
||
internal class VehicleTelemetryEvent | ||
{ | ||
public string? DeviceId { get; set; } | ||
|
||
public DateTimeOffset Timestamp { get; set; } | ||
|
||
public string? Action { get; set; } | ||
|
||
public VehicleDoorKind? VehicleDoorKind { get; set; } | ||
public VehicleDoorStatus? VehicleDoorStatus { get; set; } | ||
|
||
[JsonExtensionData] | ||
public Dictionary<string, object>? Extras { get; set; } | ||
} | ||
|
||
public enum VehicleDoorStatus | ||
{ | ||
Unknown, | ||
Open, | ||
Closed, | ||
} | ||
|
||
public enum VehicleDoorKind | ||
{ | ||
FrontLeft, | ||
FrontRight, | ||
RearLeft, | ||
ReadRight, | ||
Hood, | ||
Trunk, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace HealthCheck; | ||
|
||
internal class VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> logger) : IEventConsumer<VehicleTelemetryEvent> | ||
{ | ||
public async Task ConsumeAsync(EventContext<VehicleTelemetryEvent> context, CancellationToken cancellationToken) | ||
{ | ||
var telemetry = context.Event; | ||
|
||
var status = telemetry.VehicleDoorStatus; | ||
if (status is not VehicleDoorStatus.Open and not VehicleDoorStatus.Closed) | ||
{ | ||
logger.LogWarning("Vehicle Door status '{VehicleDoorStatus}' is not yet supported", status); | ||
return; | ||
} | ||
|
||
var kind = telemetry.VehicleDoorKind; | ||
if (kind is null) | ||
{ | ||
logger.LogWarning("Vehicle Door kind '{VehicleDoorKind}' cannot be null", kind); | ||
return; | ||
} | ||
|
||
var timestamp = telemetry.Timestamp; | ||
var updateEvt = new VehicleDoorOpenedEvent | ||
{ | ||
VehicleId = telemetry.DeviceId, | ||
Kind = kind.Value, | ||
Closed = status is VehicleDoorStatus.Closed ? timestamp : null, | ||
Opened = status is VehicleDoorStatus.Open ? timestamp : null, | ||
}; | ||
|
||
// the VehicleDoorOpenedEvent on a broadcast bus would notify all subscribers | ||
await context.PublishAsync(updateEvt, cancellationToken: cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"Microsoft": "Information", | ||
"System": "Information" | ||
}, | ||
"Console": { | ||
"FormatterName": "simple", | ||
"FormatterOptions": { | ||
"SingleLine": true, | ||
"TimestampFormat": "HH:mm:ss " | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |