forked from prometheus-net/docker_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainerTrackerResourceMetrics.cs
79 lines (66 loc) · 3.75 KB
/
ContainerTrackerResourceMetrics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Prometheus;
using System;
using System.Linq;
namespace DockerExporter
{
sealed class ContainerTrackerResourceMetrics : IDisposable
{
public Gauge.Child CpuUsage { get; private set; }
public Gauge.Child CpuCapacity { get; private set; }
public Gauge.Child MemoryUsage { get; private set; }
public Gauge.Child TotalNetworkBytesIn { get; private set; }
public Gauge.Child TotalNetworkBytesOut { get; private set; }
public Gauge.Child TotalDiskBytesRead { get; private set; }
public Gauge.Child TotalDiskBytesWrite { get; private set; }
public ContainerTrackerResourceMetrics(string 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()
{
CpuUsage.Remove();
CpuCapacity.Remove();
MemoryUsage.Remove();
TotalNetworkBytesIn.Remove();
TotalNetworkBytesOut.Remove();
TotalDiskBytesRead.Remove();
TotalDiskBytesWrite.Remove();
}
public void Unpublish()
{
CpuUsage.Unpublish();
CpuCapacity.Unpublish();
MemoryUsage.Unpublish();
TotalNetworkBytesIn.Unpublish();
TotalNetworkBytesOut.Unpublish();
TotalDiskBytesRead.Unpublish();
TotalDiskBytesWrite.Unpublish();
}
// While logically counters, all of these are gauges because we do not know when Docker might reset the values.
private static readonly Gauge BaseCpuUsage = Metrics
.CreateGauge("docker_container_cpu_used_total", "Accumulated CPU usage of a container, in unspecified units, averaged for all logical CPUs usable by the container.", ConfigureGauge());
private static readonly Gauge BaseCpuCapacity = Metrics
.CreateGauge("docker_container_cpu_capacity_total", "All potential CPU usage available to a container, in unspecified units, averaged for all logical CPUs usable by the container. Start point of measurement is undefined - only relative values should be used in analytics.", ConfigureGauge());
private static readonly Gauge BaseMemoryUsage = Metrics
.CreateGauge("docker_container_memory_used_bytes", "Memory usage of a container.", ConfigureGauge());
private static readonly Gauge BaseTotalNetworkBytesIn = Metrics
.CreateGauge("docker_container_network_in_bytes", "Total bytes received by the container's network interfaces.", ConfigureGauge());
private static readonly Gauge BaseTotalNetworkBytesOut = Metrics
.CreateGauge("docker_container_network_out_bytes", "Total bytes sent by the container's network interfaces.", ConfigureGauge());
private static readonly Gauge BaseTotalDiskBytesRead = Metrics
.CreateGauge("docker_container_disk_read_bytes", "Total bytes read from disk by a container.", ConfigureGauge());
private static readonly Gauge BaseTotalDiskBytesWrite = Metrics
.CreateGauge("docker_container_disk_write_bytes", "Total bytes written to disk by a container.", ConfigureGauge());
private static GaugeConfiguration ConfigureGauge() => new GaugeConfiguration
{
LabelNames = new[] { "name" },
SuppressInitialValue = true
};
}
}