Skip to content

GuidesMonitor the beacons

Monitor the beacons

Report beacon health to a readiness probe, read each beacon's state, and chart the pulses and reseeds.

A beacon is an external service, so it can be down while your application is fine: the generator keeps drawing from the operating system’s source, and mixes beacon pulses in when they arrive. You still want to know when a beacon stops answering. This page shows how to report beacon health to a readiness probe, how to read each beacon’s state, how to chart the pulses and reseeds, and what to watch for.

AddRandomnessBeacons registers one health check, named randomness-beacons, on the health checks builder. Tag it ready so a readiness probe runs it.

var section = builder.Configuration.GetSection("Randomness");
builder.Services.AddRandomness(section).AddCurbyBeacon();
builder.Services.AddHealthChecks().AddRandomnessBeacons("ready");
using var host = builder.Build();
var health = host.Services.GetRequiredService<HealthCheckService>();
var report = await health.CheckHealthAsync(
check => check.Tags.Contains("ready"),
cancellationToken
);
// report.Status: Healthy, or Degraded while a beacon is unavailable;
// never Unhealthy

The check is Healthy while every beacon works, and Degraded while a beacon is unavailable. It is never Unhealthy, because the generator keeps working without a beacon. It is Healthy with the message “Beacons are offline by configuration.” when the mode is Offline.

IBeaconStatus lists every registered beacon’s state. RandomnessHost.BeaconStatus returns it, and it resolves from dependency injection too.

var status = randomness.BeaconStatus;
foreach (var beacon in status.Beacons)
{
var failures = beacon.ConsecutiveFailures;
lines.Add($"{beacon.Name}: {beacon.Health}, failures {failures}");
}

Health is Pending until a fetch finishes, Healthy when the last fetch gave a verified, fresh or unchanged pulse, and Unavailable when it failed or its pulse was refused. LastPulse, LastError and LastAttempt say what the beacon last returned, what went wrong and when it tried. WaitForPulseAsync waits until a pulse is mixed in, for a policy that demands beacon entropy before it draws.

The generator publishes two counters from the meter NCode.Randomness (RandomnessDiagnostics.MeterName), created through IMeterFactory, so subscribe to it with OpenTelemetry or MeterListener.

ncode.randomness.beacon.pulses

Attributes
ncode.randomness.beacon, ncode.randomness.outcome (accepted, unchanged, unavailable, not_found, invalid, stale, rolled_back)
Counts
one per fetch

ncode.randomness.generator.reseeds

Attributes
ncode.randomness.reseed.reason (beacon, interval, volume)
Counts
one per shard reseed

Outbound calls to a beacon are measured by System.Net.Http’s own instruments on each adapter’s named client. Metrics and Traces is the full catalog.

An outage is logged once when it starts and once when it ends, so a log search finds each outage without a line for every failed fetch.

  • Putting the check in a liveness probe.
    • You write AddRandomnessBeacons() and map every check to the liveness endpoint.
    • What happens: a beacon outage, which restarting the process does not fix, restarts or flags every instance.
    • Write instead: tag the check ready, as AddRandomnessBeacons("ready") does, and run only that tag in the readiness probe, because a beacon is an external dependency.
  • Failing readiness on Degraded.
    • You write a readiness policy that treats Degraded as not ready.
    • What happens: every instance leaves service when a beacon is down, although the generator keeps working.
    • Write instead: keep ASP.NET Core’s default, which maps Degraded to 200, and alert on the metrics instead.
  • Calling WaitForPulseAsync with no timeout.
    • You write await status.WaitForPulseAsync(CancellationToken.None).
    • What happens: the call never returns while every beacon is unavailable.
    • Write instead: pass a token that cancels, such as one from a CancellationTokenSource with a timeout.
  • Add beacons shows how to register the beacons you monitor.
  • Choose a mode shows how the mode changes what the check reports.