No need for the extremely long container ID in labels

Just name is enough for meaningful results
This commit is contained in:
Sander Saares 2019-12-24 09:15:37 +02:00
parent 7226c7c3cc
commit b394173b60
6 changed files with 26 additions and 32 deletions

View file

@ -27,7 +27,7 @@ namespace DockerExporter
Id = id;
DisplayName = displayName;
_metrics = new ContainerTrackerMetrics(id, displayName);
_metrics = new ContainerTrackerMetrics(displayName);
}
public void Dispose()
@ -87,7 +87,7 @@ namespace DockerExporter
if (_stateMetrics == null)
{
_log.Debug($"First update of state metrics for {DisplayName} ({Id}).");
_stateMetrics = new ContainerTrackerStateMetrics(Id, DisplayName);
_stateMetrics = new ContainerTrackerStateMetrics(DisplayName);
}
UpdateStateMetrics(_stateMetrics, container);
@ -97,7 +97,7 @@ namespace DockerExporter
if (_resourceMetrics == null)
{
_log.Debug($"Initializing resource metrics for {DisplayName} ({Id}).");
_resourceMetrics = new ContainerTrackerResourceMetrics(Id, DisplayName);
_resourceMetrics = new ContainerTrackerResourceMetrics(DisplayName);
}
UpdateResourceMetrics(_resourceMetrics, container, resourceStatsRecorder.Response);

View file

@ -12,9 +12,9 @@ namespace DockerExporter
public Histogram InspectContainerDuration => BaseInspectContainerDuration;
public Histogram GetResourceStatsDuration => BaseGetResourceStatsDuration;
public ContainerTrackerMetrics(string id, string displayName)
public ContainerTrackerMetrics(string displayName)
{
FailedProbeCount = BaseFailedProbeCount.WithLabels(id, displayName);
FailedProbeCount = BaseFailedProbeCount.WithLabels(displayName);
}
public void Dispose()
@ -24,7 +24,7 @@ namespace DockerExporter
private static readonly Counter BaseFailedProbeCount = Metrics.CreateCounter("docker_probe_container_failed_total", "Number of times the exporter failed to collect information about a specific container.", new CounterConfiguration
{
LabelNames = new[] { "id", "display_name" }
LabelNames = new[] { "display_name" }
});
private static readonly Histogram BaseInspectContainerDuration = Metrics

View file

@ -14,15 +14,15 @@ namespace DockerExporter
public Gauge.Child TotalDiskBytesRead { get; private set; }
public Gauge.Child TotalDiskBytesWrite { get; private set; }
public ContainerTrackerResourceMetrics(string id, string displayName)
public ContainerTrackerResourceMetrics(string displayName)
{
CpuUsage = BaseCpuUsage.WithLabels(id, displayName);
CpuCapacity = BaseCpuCapacity.WithLabels(id, displayName);
MemoryUsage = BaseMemoryUsage.WithLabels(id, displayName);
TotalNetworkBytesIn = BaseTotalNetworkBytesIn.WithLabels(id, displayName);
TotalNetworkBytesOut = BaseTotalNetworkBytesOut.WithLabels(id, displayName);
TotalDiskBytesRead = BaseTotalDiskBytesRead.WithLabels(id, displayName);
TotalDiskBytesWrite = BaseTotalDiskBytesWrite.WithLabels(id, displayName);
CpuUsage = BaseCpuUsage.WithLabels(displayName);
CpuCapacity = BaseCpuCapacity.WithLabels(displayName);
MemoryUsage = BaseMemoryUsage.WithLabels(displayName);
TotalNetworkBytesIn = BaseTotalNetworkBytesIn.WithLabels(displayName);
TotalNetworkBytesOut = BaseTotalNetworkBytesOut.WithLabels(displayName);
TotalDiskBytesRead = BaseTotalDiskBytesRead.WithLabels(displayName);
TotalDiskBytesWrite = BaseTotalDiskBytesWrite.WithLabels(displayName);
}
public void Dispose()
@ -70,12 +70,9 @@ namespace DockerExporter
private static readonly Gauge BaseTotalDiskBytesWrite = Metrics
.CreateGauge("docker_container_disk_write_bytes", "Total bytes written to disk by a container.", ConfigureGauge());
private static string[] LabelNames(params string[] extra) =>
new[] { "id", "display_name" }.Concat(extra).ToArray();
private static GaugeConfiguration ConfigureGauge() => new GaugeConfiguration
{
LabelNames = LabelNames(),
LabelNames = new[] { "display_name" },
SuppressInitialValue = true
};
}

View file

@ -1,6 +1,5 @@
using Prometheus;
using System;
using System.Linq;
namespace DockerExporter
{
@ -10,11 +9,11 @@ namespace DockerExporter
public Gauge.Child RunningState { get; private set; }
public Gauge.Child StartTime { get; private set; }
public ContainerTrackerStateMetrics(string id, string displayName)
public ContainerTrackerStateMetrics(string displayName)
{
RestartCount = BaseRestartCount.WithLabels(id, displayName);
RunningState = BaseRunningState.WithLabels(id, displayName);
StartTime = BaseStartTime.WithLabels(id, displayName);
RestartCount = BaseRestartCount.WithLabels(displayName);
RunningState = BaseRunningState.WithLabels(displayName);
StartTime = BaseStartTime.WithLabels(displayName);
}
public void Dispose()
@ -40,12 +39,9 @@ namespace DockerExporter
private static readonly Gauge BaseStartTime = Metrics
.CreateGauge("docker_container_start_time", "Timestamp indicating when the container was started. Does not get reset by automatic restarts.", ConfigureGauge());
private static string[] LabelNames(params string[] extra) =>
new[] { "id", "display_name" }.Concat(extra).ToArray();
private static GaugeConfiguration ConfigureGauge() => new GaugeConfiguration
{
LabelNames = LabelNames(),
LabelNames = new[] { "display_name" },
SuppressInitialValue = true
};
}

View file

@ -129,7 +129,7 @@ namespace DockerExporter
var newIds = containerIds.Except(trackedIds);
foreach (var id in newIds)
{
var displayName = GetDisplayNameOrId(allContainers.Single(c => c.ID == id));
var displayName = GetDisplayName(allContainers.Single(c => c.ID == id));
_log.Debug($"Encountered container for the first time: {displayName} ({id}).");
_containerTrackers[id] = new ContainerTracker(id, displayName);
@ -149,16 +149,17 @@ namespace DockerExporter
}
/// <summary>
/// If a display name can be determined, returns it. Otherwise returns the container ID.
/// If the container has a name assigned, it is used.
/// Otherwise, the first 12 characters of the ID are used.
/// </summary>
private static string GetDisplayNameOrId(ContainerListResponse container)
private static string GetDisplayName(ContainerListResponse container)
{
var name = container.Names.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(name))
return name.Trim('/');
return container.ID;
return container.ID.Substring(0, 12);
}
// Synchronized - only single threaded access occurs.

View file

@ -21,7 +21,7 @@ Example Prometheus scrape configuration:
# What metrics are exported?
Basic state (running or not), lifecycle (unexpected restart count) and resource usage metrics for each container, labeled by container ID and name. Metrics are collected from the same instance of Docker that is running the exporter app.
Basic state (running or not), lifecycle (unexpected restart count) and resource usage metrics for each container, labeled by container name. Metrics are collected from the same instance of Docker that is running the exporter app.
To see the detailed list and documentation on each metric type, open the `/metrics` URL of the running app and read the output.