fix(monitoring): scope Hangfire metrics to Central
Heimdall Gitea / Detect CI scope (pull_request) Successful in 4s
Heimdall Gitea / Build and tests (pull_request) Successful in 13m15s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 7m47s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 10m39s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 2s

This commit is contained in:
Arnaud Dartois committed 2026-10-26 14:42:06 +01:00
1 parent 9785cbd3c3
commit 9c80c1e245
6 files changed
+66 -60

No files matched your search

@@ -1,5 +1,5 @@
using Hangfire;
using Heimdall.Application.Metrics;
using Heimdall.Infrastructure.Metrics;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@@ -25,7 +25,7 @@ internal sealed class HangfireMetricsReporter(
try
{
var snapshot = await Task.Run(ReadSnapshot, CancellationToken.None);
HeimdallMetrics.SetHangfireJobState(
HangfireJobMetrics.SetJobState(
snapshot.OldestEnqueuedAgeSeconds,
snapshot.OldestProcessingAgeSeconds,
snapshot.FailedJobs,
@@ -33,7 +33,7 @@ internal sealed class HangfireMetricsReporter(
}
catch (Exception exception) when (!stoppingToken.IsCancellationRequested)
{
HeimdallMetrics.SetHangfireJobState(0, 0, 0, monitoringAvailable: false);
HangfireJobMetrics.SetJobState(0, 0, 0, monitoringAvailable: false);
logger.LogWarning(
"Hangfire monitoring poll failed ({ExceptionType})",
exception.GetType().Name);
@@ -0,0 +1,53 @@
using System.Diagnostics.Metrics;
namespace Heimdall.Infrastructure.Metrics;
/// <summary>Central-only metrics for the Hangfire monitoring API.</summary>
public static class HangfireJobMetrics
{
public const string MeterName = "Heimdall.Hangfire";
private static readonly Meter MeterInstance = new(MeterName);
private static long _oldestEnqueuedAgeSeconds;
private static long _oldestProcessingAgeSeconds;
private static long _failedJobs;
private static int _monitoringAvailable;
private static readonly ObservableGauge<long> OldestEnqueuedAgeGauge =
MeterInstance.CreateObservableGauge(
"heimdall.hangfire.jobs.oldest_enqueued.age",
observeValue: () => Interlocked.Read(ref _oldestEnqueuedAgeSeconds),
unit: "s",
description: "Age of the oldest enqueued job observed in the bounded Hangfire monitoring sample");
private static readonly ObservableGauge<long> OldestProcessingAgeGauge =
MeterInstance.CreateObservableGauge(
"heimdall.hangfire.jobs.oldest_processing.age",
observeValue: () => Interlocked.Read(ref _oldestProcessingAgeSeconds),
unit: "s",
description: "Age of the oldest processing job observed in the bounded Hangfire monitoring sample");
private static readonly ObservableGauge<long> FailedJobsGauge =
MeterInstance.CreateObservableGauge(
"heimdall.hangfire.jobs.failed",
observeValue: () => Interlocked.Read(ref _failedJobs),
description: "Current number of failed Hangfire jobs");
private static readonly ObservableGauge<int> MonitoringAvailableGauge =
MeterInstance.CreateObservableGauge(
"heimdall.hangfire.monitoring.available",
observeValue: () => Volatile.Read(ref _monitoringAvailable),
description: "Whether the latest Hangfire monitoring API poll succeeded (1=yes, 0=no)");
public static void SetJobState(
long oldestEnqueuedAgeSeconds,
long oldestProcessingAgeSeconds,
long failedJobs,
bool monitoringAvailable)
{
Interlocked.Exchange(ref _oldestEnqueuedAgeSeconds, Math.Max(0, oldestEnqueuedAgeSeconds));
Interlocked.Exchange(ref _oldestProcessingAgeSeconds, Math.Max(0, oldestProcessingAgeSeconds));
Interlocked.Exchange(ref _failedJobs, Math.Max(0, failedJobs));
Volatile.Write(ref _monitoringAvailable, monitoringAvailable ? 1 : 0);
}
}