-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename Akkka in example projectname to Akka * Rename also namespace in current example * Example implementation of using Akka.Healthchecks together with IHealthChecks * Add Akka.Hosting * Use common.props for Akka.Hosting version * Add XML-DOCs * Add GUID to persistence healthchecks * Refactor API * Code cleanup, remove accidental inclusion of local snapshot files * Refactor service to probe * Add custom IProbeProvider sample project * Add documentation * Revert changes to persistence probe, moved to its own PR. Co-authored-by: Wessel Kranenborg <Wessel.Kranenborg@priva.com> Co-authored-by: Wessel Kranenborg <kranenborg.fam@gmail.com> Co-authored-by: Aaron Stannard <aaron@petabridge.com>
- Loading branch information
1 parent
7fc714f
commit 4869fb6
Showing
36 changed files
with
1,411 additions
and
10 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
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
File renamed without changes.
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
13 changes: 13 additions & 0 deletions
13
...HealthCheck.Hosting.Web.Custom.Example/Akka.HealthCheck.Hosting.Web.Custom.Example.csproj
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Akka.HealthCheck.Hosting.Web\Akka.HealthCheck.Hosting.Web.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
48 changes: 48 additions & 0 deletions
48
src/Akka.HealthCheck.Hosting.Web.Custom.Example/CustomHealthCheckReadinessProbe.cs
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,48 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="CustomHealthCheckReadinessProbe.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2015 - 2022 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Configuration; | ||
using Akka.HealthCheck.Liveness; | ||
using Akka.HealthCheck.Readiness; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Akka.HealthCheck.Hosting.Web.Custom.Example; | ||
|
||
public class CustomHealthCheckReadinessProbe: IHealthCheck | ||
{ | ||
private readonly IActorRef _probe; | ||
|
||
public CustomHealthCheckReadinessProbe(ActorSystem system) | ||
{ | ||
if (!AkkaHealthCheck.For(system).ReadinessProbes.TryGetValue("custom", out _probe!)) | ||
{ | ||
throw new ConfigurationException("Could not find readiness actor with key 'custom'."); | ||
} | ||
} | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
try | ||
{ | ||
var status = await _probe.Ask<ReadinessStatus>( | ||
message: GetCurrentReadiness.Instance, | ||
cancellationToken: cancellationToken); | ||
|
||
return status.IsReady | ||
? HealthCheckResult.Healthy("healthy", new Dictionary<string, object> { ["message"] = status.StatusMessage }) | ||
: HealthCheckResult.Unhealthy("unhealthy", data: new Dictionary<string, object> { ["message"] = status.StatusMessage }); | ||
} | ||
catch (Exception e) | ||
{ | ||
return HealthCheckResult.Unhealthy("unhealthy", e); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/Akka.HealthCheck.Hosting.Web.Custom.Example/CustomReadinessProbe.cs
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,66 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="CustomProbe.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2015 - 2022 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Akka.Actor; | ||
using Akka.HealthCheck.Readiness; | ||
|
||
namespace Akka.HealthCheck.Hosting.Web.Custom.Example; | ||
|
||
public class CustomReadinessProbe: ReceiveActor, IWithTimers | ||
{ | ||
private readonly string _timerKey = "periodic-timer"; | ||
private readonly string _timerSignal = "do-check"; | ||
|
||
private ReadinessStatus _readinessStatus; | ||
private readonly HashSet<IActorRef> _subscribers = new (); | ||
|
||
public CustomReadinessProbe() : this(new ReadinessStatus(false)) | ||
{ | ||
} | ||
|
||
public CustomReadinessProbe(ReadinessStatus readinessStatus) | ||
{ | ||
_readinessStatus = readinessStatus; | ||
|
||
Receive<GetCurrentReadiness>(_ => { | ||
Sender.Tell(_readinessStatus); | ||
}); | ||
|
||
Receive<SubscribeToReadiness>(s => | ||
{ | ||
_subscribers.Add(s.Subscriber); | ||
Context.Watch(s.Subscriber); | ||
s.Subscriber.Tell(_readinessStatus); | ||
}); | ||
|
||
Receive<UnsubscribeFromReadiness>(u => | ||
{ | ||
_subscribers.Remove(u.Subscriber); | ||
Context.Unwatch(u.Subscriber); | ||
}); | ||
|
||
Receive<Terminated>(t => { | ||
_subscribers.Remove(t.ActorRef); | ||
}); | ||
|
||
ReceiveAsync<string>( | ||
s => s == "do-check", | ||
async _ => | ||
{ | ||
// TODO: insert probe check here | ||
_readinessStatus = new ReadinessStatus(true); | ||
}); | ||
} | ||
|
||
protected override void PreStart() | ||
{ | ||
Timers.StartPeriodicTimer(_timerKey, _timerSignal, TimeSpan.FromSeconds(1)); | ||
} | ||
|
||
public ITimerScheduler Timers { get; set; } | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Akka.HealthCheck.Hosting.Web.Custom.Example/CustomReadinessProbeProvider.cs
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,18 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="CustomProbeProvider.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2015 - 2022 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using Akka.Actor; | ||
|
||
namespace Akka.HealthCheck.Hosting.Web.Custom.Example; | ||
|
||
public sealed class CustomReadinessProbeProvider: ProbeProviderBase | ||
{ | ||
public override Props ProbeProps => Props.Create(() => new CustomReadinessProbe()); | ||
|
||
public CustomReadinessProbeProvider(ActorSystem system) : base(system) | ||
{ | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Akka.HealthCheck.Hosting.Web.Custom.Example/Program.cs
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,32 @@ | ||
using Akka.HealthCheck.Hosting; | ||
using Akka.HealthCheck.Hosting.Web.Custom.Example; | ||
using Akka.Hosting; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
var webBuilder = WebApplication.CreateBuilder(args); | ||
|
||
webBuilder.Services | ||
.AddHealthChecks() | ||
.AddCheck<CustomHealthCheckReadinessProbe>("akka-custom-readiness", HealthStatus.Unhealthy, new [] { "akka", "ready", "custom" }); | ||
|
||
webBuilder.Services | ||
.AddAkka("actor-system", (builder, serviceProvider) => | ||
{ | ||
builder | ||
.WithHealthCheck(opt => | ||
{ | ||
opt.AddReadinessProvider<CustomReadinessProbeProvider>("custom"); | ||
}); | ||
}); | ||
|
||
var app = webBuilder.Build(); | ||
|
||
app.MapHealthChecks("/healthz/akka/ready/custom", new HealthCheckOptions | ||
{ | ||
Predicate = healthCheck => healthCheck.Tags.IsSupersetOf(new [] { "akka", "ready", "custom" }) | ||
}); | ||
|
||
await app.RunAsync(); |
8 changes: 8 additions & 0 deletions
8
src/Akka.HealthCheck.Hosting.Web.Custom.Example/appsettings.Development.json
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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Akka.HealthCheck.Hosting.Web.Custom.Example/appsettings.json
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.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Akka.HealthCheck.Hosting.Web.Example/Akka.HealthCheck.Hosting.Web.Example.csproj
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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<Import Project="..\common.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Akka.Cluster.Hosting" Version="$(AkkaHostingVersion)" /> | ||
<PackageReference Include="Akka.Hosting" Version="$(AkkaHostingVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Akka.HealthCheck.Hosting.Web\Akka.HealthCheck.Hosting.Web.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="snapshots\**" /> | ||
<EmbeddedResource Remove="snapshots\**" /> | ||
<None Remove="snapshots\**" /> | ||
<Content Remove="snapshots\**" /> | ||
</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,43 @@ | ||
using Akka.Cluster.Hosting; | ||
using Akka.Hosting; | ||
|
||
namespace Akka.HealthCheck.Hosting.Web.Example; | ||
|
||
public static class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
var webBuilder = WebApplication.CreateBuilder(args); | ||
|
||
webBuilder.Services | ||
// Register all of the health check service with IServiceCollection | ||
.WithAkkaHealthCheck(HealthCheckType.All) | ||
.AddAkka("actor-system", (builder, serviceProvider) => | ||
{ | ||
builder | ||
.AddHocon("akka.cluster.min-nr-of-members = 1", HoconAddMode.Prepend) | ||
.WithClustering() | ||
// Automatically detects which health checks were registered inside the health check middleware and starts them | ||
.WithWebHealthCheck(serviceProvider) | ||
.AddStartup((system, _) => | ||
{ | ||
var cluster = Akka.Cluster.Cluster.Get(system); | ||
cluster.Join(cluster.SelfAddress); | ||
}); | ||
}); | ||
|
||
var app = webBuilder.Build(); | ||
|
||
// Automatically detects which health checks were registered inside the health check middleware and maps their routes | ||
app.MapAkkaHealthCheckRoutes( | ||
prependPath:"/health", | ||
optionConfigure: opt => | ||
{ | ||
// Use a custom response writer to output a json of all reported statuses | ||
opt.ResponseWriter = Helper.JsonResponseWriter; | ||
}); | ||
|
||
await app.RunAsync(); | ||
} | ||
|
||
} |
Oops, something went wrong.