Class ResourceMetricsService
Background service that periodically updates resource snapshots and raises alerts.
public sealed class ResourceMetricsService : IDisposable
- Inheritance
-
ResourceMetricsService
- Implements
- Inherited Members
Remarks
This service is timer-based and does not depend on ASP.NET Core's IHostedService. It can be used in any .NET application.
Operation:
- Periodically calls UpdateSnapshot()
- Runs health checks after each snapshot
- Raises AlertRaised on state transitions
var service = new ResourceMetricsService(exporter, healthChecker, alertGenerator, options);
service.AlertRaised += (sender, alert) =>
{
Console.WriteLine($"Alert: {alert.Title}");
};
service.Start();
// Later...
service.Stop();
service.Dispose();
Constructors
ResourceMetricsService(ResourceMetricsExporter, ResourceHealthChecker, ResourceAlertGenerator, ObservabilityBridgeOptions)
Creates a new ResourceMetricsService.
public ResourceMetricsService(ResourceMetricsExporter exporter, ResourceHealthChecker healthChecker, ResourceAlertGenerator alertGenerator, ObservabilityBridgeOptions options)
Parameters
exporterResourceMetricsExporterThe metrics exporter.
healthCheckerResourceHealthCheckerThe health checker.
alertGeneratorResourceAlertGeneratorThe alert generator.
optionsObservabilityBridgeOptionsConfiguration options.
Properties
CurrentStatus
The current health status from the last check.
public HealthStatus CurrentStatus { get; }
Property Value
IsRunning
Whether the service is currently running.
public bool IsRunning { get; }
Property Value
Methods
Dispose()
Disposes the timer and stops the service.
public void Dispose()
ForceUpdate()
Force an immediate snapshot update and health check.
public HealthStatus ForceUpdate()
Returns
- HealthStatus
The current health status.
Start()
Start the background refresh service.
public void Start()
Stop()
Stop the background refresh service.
public void Stop()
Events
AlertRaised
Event raised when a resource crosses health thresholds.
public event EventHandler<ResourceAlert> AlertRaised
Event Type
Remarks
Alerts are only raised on state transitions to avoid flooding:
- Healthy → Degraded: Warning alert
- Healthy → Unhealthy: Critical alert
- Degraded → Unhealthy: Critical alert (escalation)
No alert is raised when recovering (Unhealthy → Degraded → Healthy).
HealthStatusChanged
Event raised when health status changes (including recovery).
public event EventHandler<HealthStatusChangedEventArgs> HealthStatusChanged