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

@@ -288,52 +288,6 @@ public static class HeimdallMetrics
private static long _activeViewers;
// ── Background jobs (heimdall.hangfire.*) ────────────────────
private static long _hangfireOldestEnqueuedAgeSeconds;
private static long _hangfireOldestProcessingAgeSeconds;
private static long _hangfireFailedJobs;
private static int _hangfireMonitoringAvailable;
private static readonly ObservableGauge<long> HangfireOldestEnqueuedAgeGauge =
Meter.CreateObservableGauge(
"heimdall.hangfire.jobs.oldest_enqueued.age",
observeValue: () => Interlocked.Read(ref _hangfireOldestEnqueuedAgeSeconds),
unit: "s",
description: "Age of the oldest enqueued job observed in the bounded Hangfire monitoring sample");
private static readonly ObservableGauge<long> HangfireOldestProcessingAgeGauge =
Meter.CreateObservableGauge(
"heimdall.hangfire.jobs.oldest_processing.age",
observeValue: () => Interlocked.Read(ref _hangfireOldestProcessingAgeSeconds),
unit: "s",
description: "Age of the oldest processing job observed in the bounded Hangfire monitoring sample");
private static readonly ObservableGauge<long> HangfireFailedJobsGauge =
Meter.CreateObservableGauge(
"heimdall.hangfire.jobs.failed",
observeValue: () => Interlocked.Read(ref _hangfireFailedJobs),
description: "Current number of failed Hangfire jobs");
private static readonly ObservableGauge<int> HangfireMonitoringAvailableGauge =
Meter.CreateObservableGauge(
"heimdall.hangfire.monitoring.available",
observeValue: () => Volatile.Read(ref _hangfireMonitoringAvailable),
description: "Whether the latest Hangfire monitoring API poll succeeded (1=yes, 0=no)");
/// <summary>Update the bounded Hangfire monitoring snapshot.</summary>
public static void SetHangfireJobState(
long oldestEnqueuedAgeSeconds,
long oldestProcessingAgeSeconds,
long failedJobs,
bool monitoringAvailable)
{
Interlocked.Exchange(ref _hangfireOldestEnqueuedAgeSeconds, Math.Max(0, oldestEnqueuedAgeSeconds));
Interlocked.Exchange(ref _hangfireOldestProcessingAgeSeconds, Math.Max(0, oldestProcessingAgeSeconds));
Interlocked.Exchange(ref _hangfireFailedJobs, Math.Max(0, failedJobs));
Volatile.Write(ref _hangfireMonitoringAvailable, monitoringAvailable ? 1 : 0);
}
/// <summary>
/// Increment active viewers count.
/// </summary>
@@ -358,9 +312,5 @@ public static class HeimdallMetrics
Volatile.Write(ref _signalrConnections, 0);
SignalRConnectionsByHub.Clear();
Volatile.Write(ref _activeViewers, 0);
Interlocked.Exchange(ref _hangfireOldestEnqueuedAgeSeconds, 0);
Interlocked.Exchange(ref _hangfireOldestProcessingAgeSeconds, 0);
Interlocked.Exchange(ref _hangfireFailedJobs, 0);
Volatile.Write(ref _hangfireMonitoringAvailable, 0);
}
}
@@ -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);
}
}
+1
View File
@@ -444,6 +444,7 @@ public partial class Program
metrics.AddPrometheusExporter();
metrics.AddAspNetCoreInstrumentation();
metrics.AddMeter(HeimdallMetrics.MeterName);
metrics.AddMeter(Heimdall.Infrastructure.Metrics.HangfireJobMetrics.MeterName);
});
builder.Services.AddApplication();