From 9c80c1e24516c8f2cdce5c9ed2bd502d7d5114a3 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Mon, 26 Oct 2026 14:42:06 +0100 Subject: [PATCH] fix(monitoring): scope Hangfire metrics to Central --- deploy/prometheus/heimdall-alerts.yml | 2 +- docs/metrics.md | 14 ++--- .../Metrics/HeimdallMetrics.cs | 50 ----------------- .../BackgroundJobs/HangfireMetricsReporter.cs | 6 +-- .../Metrics/HangfireJobMetrics.cs | 53 +++++++++++++++++++ src/Heimdall.Web/Program.cs | 1 + 6 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 src/Heimdall.Infrastructure/Metrics/HangfireJobMetrics.cs diff --git a/deploy/prometheus/heimdall-alerts.yml b/deploy/prometheus/heimdall-alerts.yml index 7c7a296..3512d7a 100644 --- a/deploy/prometheus/heimdall-alerts.yml +++ b/deploy/prometheus/heimdall-alerts.yml @@ -1,5 +1,5 @@ groups: - - name: heimdall-agent + - name: heimdall rules: - alert: HeimdallAgentSnapshotDrops expr: increase(heimdall_ai_queue_dropped_total{job="heimdall-agent"}[5m]) > 0 diff --git a/docs/metrics.md b/docs/metrics.md index af8edc2..2bf53d4 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -26,8 +26,10 @@ les noms réellement exposés par `/metrics` avant de créer vos règles. Ces signaux rendent les pertes et la capacité observables, mais Heimdall ne choisit pas de seuil d'alerte disque/file à votre place. Les seuils dépendent du matériel, du nombre de caméras et de la charge d'analyse; définissez-les dans -votre système de supervision. Les files d'attente et compteurs appartiennent au -processus Agent courant et repartent de zéro après un redémarrage. +votre système de supervision. Les files et compteurs de l'Agent appartiennent à +son processus et repartent de zéro après un redémarrage. Les gauges Hangfire +sont mesurées par le Central à partir de l'API de monitoring et de l'état +persisté dans son stockage. ## Exemple de règles d'alerte Prometheus @@ -35,10 +37,10 @@ Le fichier [`deploy/prometheus/heimdall-alerts.yml`](../deploy/prometheus/heimda fournit des règles d'exemple pour les pertes de snapshots, les rejets d'inférence, les files durablement occupées, le disque et les jobs Hangfire. Importez-le dans -`rule_files` de Prometheus ou dans votre gestionnaire de règles. Ces règles -ciblent les scrapes portant le label `job="heimdall-agent"`; adaptez ce label -à votre configuration. Vérifiez les noms exposés par `/metrics` après -configuration de l'exporteur. +`rule_files` de Prometheus ou dans votre gestionnaire de règles. Les règles +Agent ciblent `job="heimdall-agent"`; les règles Hangfire ciblent +`job="heimdall-central"`. Adaptez chacun de ces labels à votre configuration. +Vérifiez les noms exposés par `/metrics` après configuration de l'exporteur. Le seuil disque de 90 %, la durée de backlog de 10 minutes et les seuils Hangfire de 15/30 minutes sont des valeurs d'exemple à adapter au matériel, au diff --git a/src/Heimdall.Application/Metrics/HeimdallMetrics.cs b/src/Heimdall.Application/Metrics/HeimdallMetrics.cs index 0a1c611..a768516 100644 --- a/src/Heimdall.Application/Metrics/HeimdallMetrics.cs +++ b/src/Heimdall.Application/Metrics/HeimdallMetrics.cs @@ -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 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 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 HangfireFailedJobsGauge = - Meter.CreateObservableGauge( - "heimdall.hangfire.jobs.failed", - observeValue: () => Interlocked.Read(ref _hangfireFailedJobs), - description: "Current number of failed Hangfire jobs"); - - private static readonly ObservableGauge 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)"); - - /// Update the bounded Hangfire monitoring snapshot. - 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); - } - /// /// Increment active viewers count. /// @@ -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); } } diff --git a/src/Heimdall.Infrastructure/BackgroundJobs/HangfireMetricsReporter.cs b/src/Heimdall.Infrastructure/BackgroundJobs/HangfireMetricsReporter.cs index 8c14037..16dcd89 100644 --- a/src/Heimdall.Infrastructure/BackgroundJobs/HangfireMetricsReporter.cs +++ b/src/Heimdall.Infrastructure/BackgroundJobs/HangfireMetricsReporter.cs @@ -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); diff --git a/src/Heimdall.Infrastructure/Metrics/HangfireJobMetrics.cs b/src/Heimdall.Infrastructure/Metrics/HangfireJobMetrics.cs new file mode 100644 index 0000000..c84a1db --- /dev/null +++ b/src/Heimdall.Infrastructure/Metrics/HangfireJobMetrics.cs @@ -0,0 +1,53 @@ +using System.Diagnostics.Metrics; + +namespace Heimdall.Infrastructure.Metrics; + +/// Central-only metrics for the Hangfire monitoring API. +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 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 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 FailedJobsGauge = + MeterInstance.CreateObservableGauge( + "heimdall.hangfire.jobs.failed", + observeValue: () => Interlocked.Read(ref _failedJobs), + description: "Current number of failed Hangfire jobs"); + + private static readonly ObservableGauge 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); + } +} diff --git a/src/Heimdall.Web/Program.cs b/src/Heimdall.Web/Program.cs index ead287d..31a3af5 100644 --- a/src/Heimdall.Web/Program.cs +++ b/src/Heimdall.Web/Program.cs @@ -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();