diff --git a/ContainerTracker.cs b/ContainerTracker.cs index f109675..7570b11 100644 --- a/ContainerTracker.cs +++ b/ContainerTracker.cs @@ -122,6 +122,22 @@ private void UpdateStateMetrics(ContainerTrackerStateMetrics metrics, ContainerI else metrics.RunningState.Set(0); + if (container.State.Health != null) + { + // Publish container health if it exists + if (container.State.Health.Status == "healthy") + metrics.HealthState.Set(1); + else if (container.State.Health.Status == "starting") + metrics.HealthState.Set(0.5); + else // "unhealthy" + metrics.HealthState.Set(0); + } + else + { + // Makes sure to unpublish it if it wasn't initially published + metrics.HealthState.Unpublish(); + } + if (container.State.Running && !string.IsNullOrWhiteSpace(container.State.StartedAt)) metrics.StartTime.SetToTimeUtc(DateTimeOffset.Parse(container.State.StartedAt)); } diff --git a/ContainerTrackerStateMetrics.cs b/ContainerTrackerStateMetrics.cs index 2243d62..cf4dd71 100644 --- a/ContainerTrackerStateMetrics.cs +++ b/ContainerTrackerStateMetrics.cs @@ -7,12 +7,14 @@ sealed class ContainerTrackerStateMetrics : IDisposable { public Gauge.Child RestartCount { get; private set; } public Gauge.Child RunningState { get; private set; } + public Gauge.Child HealthState { get; private set; } public Gauge.Child StartTime { get; private set; } public ContainerTrackerStateMetrics(string displayName) { RestartCount = BaseRestartCount.WithLabels(displayName); RunningState = BaseRunningState.WithLabels(displayName); + HealthState = BaseHealthState.WithLabels(displayName); StartTime = BaseStartTime.WithLabels(displayName); } @@ -20,6 +22,7 @@ public void Dispose() { RestartCount.Remove(); RunningState.Remove(); + HealthState.Remove(); StartTime.Remove(); } @@ -27,6 +30,7 @@ public void Unpublish() { RestartCount.Unpublish(); RunningState.Unpublish(); + HealthState.Unpublish(); StartTime.Unpublish(); } @@ -36,6 +40,9 @@ public void Unpublish() private static readonly Gauge BaseRunningState = Metrics .CreateGauge("docker_container_running_state", "Whether the container is running (1), restarting (0.5) or stopped (0).", ConfigureGauge()); + private static readonly Gauge BaseHealthState = Metrics + .CreateGauge("docker_container_health_state", "Whether the container is healthy (1), starting (0.5), unhealthy (0), or has no health information (unpublished, won't show up)", ConfigureGauge()); + private static readonly Gauge BaseStartTime = Metrics .CreateGauge("docker_container_start_time_seconds", "Timestamp indicating when the container was started. Does not get reset by automatic restarts.", ConfigureGauge());