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 -1
View File
@@ -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
+8 -6
View File
@@ -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
@@ -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();