Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Added docker container State.Health.Status as a metric #7 #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ContainerTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
7 changes: 7 additions & 0 deletions ContainerTrackerStateMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@ 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);
}

public void Dispose()
{
RestartCount.Remove();
RunningState.Remove();
HealthState.Remove();
StartTime.Remove();
}

public void Unpublish()
{
RestartCount.Unpublish();
RunningState.Unpublish();
HealthState.Unpublish();
StartTime.Unpublish();
}

Expand All @@ -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());

Expand Down