-
Notifications
You must be signed in to change notification settings - Fork 18
Add Redis connectivity health checks #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Aaronontheweb
merged 6 commits into
akkadotnet:dev
from
Aaronontheweb:feature/redis-connectivity-health-checks
Oct 26, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e4c1d6
Add Redis connectivity health checks
Aaronontheweb 46ce2c3
Add documentation for Redis connectivity health checks
Aaronontheweb 2ec9a88
Update to Akka.Hosting 1.5.55-beta1 and add WithConnectivityCheck API
Aaronontheweb e907f3e
Add integration tests for Redis connectivity checks happy path
Aaronontheweb 4e86525
Address Redis PR review comments
Aaronontheweb 1d941b0
Fix NuGet package README inclusion
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
27 changes: 13 additions & 14 deletions
27
src/Akka.Persistence.Redis.Hosting/Akka.Persistence.Redis.Hosting.csproj
This file contains hidden or 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 |
|---|---|---|
| @@ -1,19 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>$(NetStandardVersion);$(NetVersion)</TargetFrameworks> | ||
| <PackageReadmeFile>README.md</PackageReadmeFile> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>$(NetStandardVersion);$(NetVersion)</TargetFrameworks> | ||
| <PackageReadmeFile>README.md</PackageReadmeFile> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Akka.Persistence.Hosting" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Akka.Persistence.Hosting" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Akka.Persistence.Redis\Akka.Persistence.Redis.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Akka.Persistence.Redis\Akka.Persistence.Redis.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="$(MSBuildThisFileDirectory)\README.md" Pack="true" PackagePath="\" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <None Include="README.md" Pack="true" PackagePath="\" /> | ||
| </ItemGroup> | ||
| </Project> |
79 changes: 79 additions & 0 deletions
79
src/Akka.Persistence.Redis.Hosting/RedisConnectivityCheckExtensions.cs
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tags need to be customizable for all health checks |
This file contains hidden or 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,79 @@ | ||
| using System; | ||
| using Akka.Hosting; | ||
| using Akka.Persistence.Hosting; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
|
||
| #nullable enable | ||
| namespace Akka.Persistence.Redis.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for Redis persistence connectivity checks | ||
| /// </summary> | ||
| public static class RedisConnectivityCheckExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds a connectivity check for the Redis journal. | ||
| /// This is a liveness check that proactively verifies database connectivity. | ||
| /// </summary> | ||
| /// <param name="builder">The journal builder</param> | ||
| /// <param name="journalOptions">The journal options containing connection details</param> | ||
| /// <param name="unHealthyStatus">The status to return when check fails. Defaults to Unhealthy.</param> | ||
| /// <param name="name">Optional name for the health check. Defaults to "Akka.Persistence.Redis.Journal.{id}.Connectivity"</param> | ||
| /// <param name="tags">Optional tags for the health check. Defaults to ["akka", "persistence", "redis", "journal", "connectivity"]</param> | ||
| /// <returns>The journal builder for chaining</returns> | ||
| public static AkkaPersistenceJournalBuilder WithConnectivityCheck( | ||
| this AkkaPersistenceJournalBuilder builder, | ||
| RedisJournalOptions journalOptions, | ||
| HealthStatus unHealthyStatus = HealthStatus.Unhealthy, | ||
| string? name = null, | ||
| string[]? tags = null) | ||
| { | ||
| if (journalOptions is null) | ||
| throw new ArgumentNullException(nameof(journalOptions)); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(journalOptions.ConfigurationString)) | ||
| throw new ArgumentException("ConfigurationString must be set on RedisJournalOptions", nameof(journalOptions)); | ||
|
|
||
| var registration = new AkkaHealthCheckRegistration( | ||
| name ?? $"Akka.Persistence.Redis.Journal.{journalOptions.Identifier}.Connectivity", | ||
| new RedisJournalConnectivityCheck(journalOptions.ConfigurationString, journalOptions.Identifier), | ||
| unHealthyStatus, | ||
| tags ?? new[] { "akka", "persistence", "redis", "journal", "connectivity" }); | ||
|
|
||
| // Use the new WithCustomHealthCheck method from Akka.Hosting 1.5.55-beta1 | ||
| return builder.WithCustomHealthCheck(registration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a connectivity check for the Redis snapshot store. | ||
| /// This is a liveness check that proactively verifies database connectivity. | ||
| /// </summary> | ||
| /// <param name="builder">The snapshot builder</param> | ||
| /// <param name="snapshotOptions">The snapshot options containing connection details</param> | ||
| /// <param name="unHealthyStatus">The status to return when check fails. Defaults to Unhealthy.</param> | ||
| /// <param name="name">Optional name for the health check. Defaults to "Akka.Persistence.Redis.SnapshotStore.{id}.Connectivity"</param> | ||
| /// <param name="tags">Optional tags for the health check. Defaults to ["akka", "persistence", "redis", "snapshot-store", "connectivity"]</param> | ||
| /// <returns>The snapshot builder for chaining</returns> | ||
| public static AkkaPersistenceSnapshotBuilder WithConnectivityCheck( | ||
| this AkkaPersistenceSnapshotBuilder builder, | ||
| RedisSnapshotOptions snapshotOptions, | ||
| HealthStatus unHealthyStatus = HealthStatus.Unhealthy, | ||
| string? name = null, | ||
| string[]? tags = null) | ||
| { | ||
| if (snapshotOptions is null) | ||
| throw new ArgumentNullException(nameof(snapshotOptions)); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(snapshotOptions.ConfigurationString)) | ||
| throw new ArgumentException("ConfigurationString must be set on RedisSnapshotOptions", nameof(snapshotOptions)); | ||
|
|
||
| var registration = new AkkaHealthCheckRegistration( | ||
| name ?? $"Akka.Persistence.Redis.SnapshotStore.{snapshotOptions.Identifier}.Connectivity", | ||
| new RedisSnapshotStoreConnectivityCheck(snapshotOptions.ConfigurationString, snapshotOptions.Identifier), | ||
| unHealthyStatus, | ||
| tags ?? new[] { "akka", "persistence", "redis", "snapshot-store", "connectivity" }); | ||
|
|
||
| // Use the new WithCustomHealthCheck method from Akka.Hosting 1.5.55-beta1 | ||
| return builder.WithCustomHealthCheck(registration); | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
src/Akka.Persistence.Redis.Hosting/RedisJournalConnectivityCheck.cs
This file contains hidden or 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,51 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="RedisJournalConnectivityCheck.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Akka.Hosting; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using StackExchange.Redis; | ||
|
|
||
| #nullable enable | ||
| namespace Akka.Persistence.Redis.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Health check that verifies connectivity to the Redis instance used by the journal. | ||
| /// This is a liveness check that proactively verifies backend connectivity. | ||
| /// </summary> | ||
| public sealed class RedisJournalConnectivityCheck : IAkkaHealthCheck | ||
| { | ||
| private readonly string _connectionString; | ||
| private readonly string _journalId; | ||
|
|
||
| public RedisJournalConnectivityCheck(string connectionString, string journalId) | ||
| { | ||
| _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); | ||
| _journalId = journalId ?? throw new ArgumentNullException(nameof(journalId)); | ||
| } | ||
|
|
||
| public async Task<HealthCheckResult> CheckHealthAsync(AkkaHealthCheckContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| try | ||
| { | ||
| using var connection = await ConnectionMultiplexer.ConnectAsync(_connectionString); | ||
| var server = connection.GetServer(connection.GetEndPoints().First()); | ||
| await server.PingAsync(); | ||
| return HealthCheckResult.Healthy($"Redis journal '{_journalId}' connection successful"); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| return HealthCheckResult.Unhealthy($"Redis journal '{_journalId}' connectivity check timed out"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return HealthCheckResult.Unhealthy($"Redis journal '{_journalId}' connection failed", ex); | ||
| } | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
src/Akka.Persistence.Redis.Hosting/RedisSnapshotStoreConnectivityCheck.cs
This file contains hidden or 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,51 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="RedisSnapshotStoreConnectivityCheck.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Akka.Hosting; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using StackExchange.Redis; | ||
|
|
||
| #nullable enable | ||
| namespace Akka.Persistence.Redis.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Health check that verifies connectivity to the Redis instance used by the snapshot store. | ||
| /// This is a liveness check that proactively verifies backend connectivity. | ||
| /// </summary> | ||
| public sealed class RedisSnapshotStoreConnectivityCheck : IAkkaHealthCheck | ||
| { | ||
| private readonly string _connectionString; | ||
| private readonly string _snapshotStoreId; | ||
|
|
||
| public RedisSnapshotStoreConnectivityCheck(string connectionString, string snapshotStoreId) | ||
| { | ||
| _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); | ||
| _snapshotStoreId = snapshotStoreId ?? throw new ArgumentNullException(nameof(snapshotStoreId)); | ||
| } | ||
|
|
||
| public async Task<HealthCheckResult> CheckHealthAsync(AkkaHealthCheckContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| try | ||
| { | ||
| using var connection = await ConnectionMultiplexer.ConnectAsync(_connectionString); | ||
| var server = connection.GetServer(connection.GetEndPoints().First()); | ||
| await server.PingAsync(); | ||
| return HealthCheckResult.Healthy($"Redis snapshot store '{_snapshotStoreId}' connection successful"); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| return HealthCheckResult.Unhealthy($"Redis snapshot store '{_snapshotStoreId}' connectivity check timed out"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return HealthCheckResult.Unhealthy($"Redis snapshot store '{_snapshotStoreId}' connection failed", ex); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was the
READMEfile packaging removed from this file?