Skip to content

Commit f50fbe3

Browse files
committedAug 28, 2020
Added docker container State.Health.Status as a metric prometheus-net#7
1 parent c6d0132 commit f50fbe3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed
 

‎ContainerTracker.cs

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ private void UpdateStateMetrics(ContainerTrackerStateMetrics metrics, ContainerI
122122
else
123123
metrics.RunningState.Set(0);
124124

125+
if (container.State.Status == "healthy")
126+
metrics.HealthState.Set(1);
127+
else if (container.State.Status == "starting")
128+
metrics.HealthState.Set(0.5);
129+
else // "unhealthy"
130+
metrics.HealthState.Set(0);
131+
125132
if (container.State.Running && !string.IsNullOrWhiteSpace(container.State.StartedAt))
126133
metrics.StartTime.SetToTimeUtc(DateTimeOffset.Parse(container.State.StartedAt));
127134
}

‎ContainerTrackerStateMetrics.cs

+7
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,30 @@ sealed class ContainerTrackerStateMetrics : IDisposable
77
{
88
public Gauge.Child RestartCount { get; private set; }
99
public Gauge.Child RunningState { get; private set; }
10+
public Gauge.Child HealthState { get; private set; }
1011
public Gauge.Child StartTime { get; private set; }
1112

1213
public ContainerTrackerStateMetrics(string displayName)
1314
{
1415
RestartCount = BaseRestartCount.WithLabels(displayName);
1516
RunningState = BaseRunningState.WithLabels(displayName);
17+
HealthState = BaseHealthState.WithLabels(displayName);
1618
StartTime = BaseStartTime.WithLabels(displayName);
1719
}
1820

1921
public void Dispose()
2022
{
2123
RestartCount.Remove();
2224
RunningState.Remove();
25+
HealthState.Remove();
2326
StartTime.Remove();
2427
}
2528

2629
public void Unpublish()
2730
{
2831
RestartCount.Unpublish();
2932
RunningState.Unpublish();
33+
HealthState.Unpublish();
3034
StartTime.Unpublish();
3135
}
3236

@@ -36,6 +40,9 @@ public void Unpublish()
3640
private static readonly Gauge BaseRunningState = Metrics
3741
.CreateGauge("docker_container_running_state", "Whether the container is running (1), restarting (0.5) or stopped (0).", ConfigureGauge());
3842

43+
private static readonly Gauge BaseHealthState = Metrics
44+
.CreateGauge("docker_container_health_state", "Whether the container is healthy (1), starting (0.5) or unhealthy (0).", ConfigureGauge());
45+
3946
private static readonly Gauge BaseStartTime = Metrics
4047
.CreateGauge("docker_container_start_time_seconds", "Timestamp indicating when the container was started. Does not get reset by automatic restarts.", ConfigureGauge());
4148

0 commit comments

Comments
 (0)
Please sign in to comment.