diff --git a/ContainerTracker.cs b/ContainerTracker.cs index 0b3dab3..62eb7b6 100644 --- a/ContainerTracker.cs +++ b/ContainerTracker.cs @@ -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); diff --git a/ContainerTrackerMetrics.cs b/ContainerTrackerMetrics.cs index 6ace054..487c4aa 100644 --- a/ContainerTrackerMetrics.cs +++ b/ContainerTrackerMetrics.cs @@ -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 diff --git a/ContainerTrackerResourceMetrics.cs b/ContainerTrackerResourceMetrics.cs index 2f50781..7d10ff9 100644 --- a/ContainerTrackerResourceMetrics.cs +++ b/ContainerTrackerResourceMetrics.cs @@ -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 }; } diff --git a/ContainerTrackerStateMetrics.cs b/ContainerTrackerStateMetrics.cs index e71d13e..e22362b 100644 --- a/ContainerTrackerStateMetrics.cs +++ b/ContainerTrackerStateMetrics.cs @@ -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 }; } diff --git a/DockerTracker.cs b/DockerTracker.cs index e512688..8022450 100644 --- a/DockerTracker.cs +++ b/DockerTracker.cs @@ -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 } /// - /// 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. /// - 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. diff --git a/README.md b/README.md index ea6213f..cc77a8a 100644 --- a/README.md +++ b/README.md @@ -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.