From f50fbe3b3f0035132136ee0f204796716afc906c Mon Sep 17 00:00:00 2001 From: Cobertos / Peter Date: Thu, 27 Aug 2020 23:45:37 -0400 Subject: [PATCH 1/3] Added docker container State.Health.Status as a metric #7 --- ContainerTracker.cs | 7 +++++++ ContainerTrackerStateMetrics.cs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/ContainerTracker.cs b/ContainerTracker.cs index f109675..5a47a80 100644 --- a/ContainerTracker.cs +++ b/ContainerTracker.cs @@ -122,6 +122,13 @@ private void UpdateStateMetrics(ContainerTrackerStateMetrics metrics, ContainerI else metrics.RunningState.Set(0); + if (container.State.Status == "healthy") + metrics.HealthState.Set(1); + else if (container.State.Status == "starting") + metrics.HealthState.Set(0.5); + else // "unhealthy" + metrics.HealthState.Set(0); + 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..686deab 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) or unhealthy (0).", 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()); From f0a7eba08b6eadd273602b23893e39cd5b075d11 Mon Sep 17 00:00:00 2001 From: Cobertos / Peter Date: Fri, 28 Aug 2020 01:18:29 -0400 Subject: [PATCH 2/3] Added -1 value for when health information does not exist --- ContainerTracker.cs | 6 ++++-- ContainerTrackerStateMetrics.cs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ContainerTracker.cs b/ContainerTracker.cs index 5a47a80..de15a73 100644 --- a/ContainerTracker.cs +++ b/ContainerTracker.cs @@ -122,9 +122,11 @@ private void UpdateStateMetrics(ContainerTrackerStateMetrics metrics, ContainerI else metrics.RunningState.Set(0); - if (container.State.Status == "healthy") + if (container.State.Health == null) // no health information + metrics.HealthState.Set(-1); + else if (container.State.Health.Status == "healthy") metrics.HealthState.Set(1); - else if (container.State.Status == "starting") + else if (container.State.Health.Status == "starting") metrics.HealthState.Set(0.5); else // "unhealthy" metrics.HealthState.Set(0); diff --git a/ContainerTrackerStateMetrics.cs b/ContainerTrackerStateMetrics.cs index 686deab..05892c7 100644 --- a/ContainerTrackerStateMetrics.cs +++ b/ContainerTrackerStateMetrics.cs @@ -41,7 +41,7 @@ public void Unpublish() .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) or unhealthy (0).", ConfigureGauge()); + .CreateGauge("docker_container_health_state", "Whether the container is healthy (1), starting (0.5), unhealthy (0), or no health information (-1).", 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()); From ffa2c4177f8de0d5c1b569e52d7220eba0badc69 Mon Sep 17 00:00:00 2001 From: Cobertos / Peter Date: Fri, 28 Aug 2020 02:09:41 -0400 Subject: [PATCH 3/3] Health state for containers that don't have it aren't published --- ContainerTracker.cs | 23 +++++++++++++++-------- ContainerTrackerStateMetrics.cs | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ContainerTracker.cs b/ContainerTracker.cs index de15a73..7570b11 100644 --- a/ContainerTracker.cs +++ b/ContainerTracker.cs @@ -122,14 +122,21 @@ private void UpdateStateMetrics(ContainerTrackerStateMetrics metrics, ContainerI else metrics.RunningState.Set(0); - if (container.State.Health == null) // no health information - metrics.HealthState.Set(-1); - else 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); + 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 05892c7..cf4dd71 100644 --- a/ContainerTrackerStateMetrics.cs +++ b/ContainerTrackerStateMetrics.cs @@ -41,7 +41,7 @@ public void Unpublish() .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 no health information (-1).", ConfigureGauge()); + .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());