From 80648f783ecad4e8fe33a36cf576056f950ff91f Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Fri, 28 Aug 2015 22:51:10 +0100 Subject: [PATCH] Expose all interfaces in Prometheus metrics. Fixes #871 --- metrics/prometheus.go | 128 +++++++++++++++++++++------- metrics/prometheus_test.go | 13 +++ metrics/testdata/prometheus_metrics | 16 ++-- 3 files changed, 117 insertions(+), 40 deletions(-) diff --git a/metrics/prometheus.go b/metrics/prometheus.go index d592dd1f..a0c05779 100644 --- a/metrics/prometheus.go +++ b/metrics/prometheus.go @@ -287,60 +287,124 @@ func NewPrometheusCollector(infoProvider subcontainersInfoProvider) *PrometheusC }) }, }, { - name: "container_network_receive_bytes_total", - help: "Cumulative count of bytes received", - valueType: prometheus.CounterValue, + name: "container_network_receive_bytes_total", + help: "Cumulative count of bytes received", + valueType: prometheus.CounterValue, + extraLabels: []string{"interface"}, getValues: func(s *info.ContainerStats) metricValues { - return metricValues{{value: float64(s.Network.RxBytes)}} + values := make(metricValues, 0, len(s.Network.Interfaces)) + for _, value := range s.Network.Interfaces { + values = append(values, metricValue{ + value: float64(value.RxBytes), + labels: []string{value.Name}, + }) + } + return values }, }, { - name: "container_network_receive_packets_total", - help: "Cumulative count of packets received", - valueType: prometheus.CounterValue, + name: "container_network_receive_packets_total", + help: "Cumulative count of packets received", + valueType: prometheus.CounterValue, + extraLabels: []string{"interface"}, getValues: func(s *info.ContainerStats) metricValues { - return metricValues{{value: float64(s.Network.RxPackets)}} + values := make(metricValues, 0, len(s.Network.Interfaces)) + for _, value := range s.Network.Interfaces { + values = append(values, metricValue{ + value: float64(value.RxPackets), + labels: []string{value.Name}, + }) + } + return values }, }, { - name: "container_network_receive_packets_dropped_total", - help: "Cumulative count of packets dropped while receiving", - valueType: prometheus.CounterValue, + name: "container_network_receive_packets_dropped_total", + help: "Cumulative count of packets dropped while receiving", + valueType: prometheus.CounterValue, + extraLabels: []string{"interface"}, getValues: func(s *info.ContainerStats) metricValues { - return metricValues{{value: float64(s.Network.RxDropped)}} + values := make(metricValues, 0, len(s.Network.Interfaces)) + for _, value := range s.Network.Interfaces { + values = append(values, metricValue{ + value: float64(value.RxDropped), + labels: []string{value.Name}, + }) + } + return values }, }, { - name: "container_network_receive_errors_total", - help: "Cumulative count of errors encountered while receiving", - valueType: prometheus.CounterValue, + name: "container_network_receive_errors_total", + help: "Cumulative count of errors encountered while receiving", + valueType: prometheus.CounterValue, + extraLabels: []string{"interface"}, getValues: func(s *info.ContainerStats) metricValues { - return metricValues{{value: float64(s.Network.RxErrors)}} + values := make(metricValues, 0, len(s.Network.Interfaces)) + for _, value := range s.Network.Interfaces { + values = append(values, metricValue{ + value: float64(value.RxErrors), + labels: []string{value.Name}, + }) + } + return values }, }, { - name: "container_network_transmit_bytes_total", - help: "Cumulative count of bytes transmitted", - valueType: prometheus.CounterValue, + name: "container_network_transmit_bytes_total", + help: "Cumulative count of bytes transmitted", + valueType: prometheus.CounterValue, + extraLabels: []string{"interface"}, getValues: func(s *info.ContainerStats) metricValues { - return metricValues{{value: float64(s.Network.TxBytes)}} + values := make(metricValues, 0, len(s.Network.Interfaces)) + for _, value := range s.Network.Interfaces { + values = append(values, metricValue{ + value: float64(value.TxBytes), + labels: []string{value.Name}, + }) + } + return values }, }, { - name: "container_network_transmit_packets_total", - help: "Cumulative count of packets transmitted", - valueType: prometheus.CounterValue, + name: "container_network_transmit_packets_total", + help: "Cumulative count of packets transmitted", + valueType: prometheus.CounterValue, + extraLabels: []string{"interface"}, getValues: func(s *info.ContainerStats) metricValues { - return metricValues{{value: float64(s.Network.TxPackets)}} + values := make(metricValues, 0, len(s.Network.Interfaces)) + for _, value := range s.Network.Interfaces { + values = append(values, metricValue{ + value: float64(value.TxPackets), + labels: []string{value.Name}, + }) + } + return values }, }, { - name: "container_network_transmit_packets_dropped_total", - help: "Cumulative count of packets dropped while transmitting", - valueType: prometheus.CounterValue, + name: "container_network_transmit_packets_dropped_total", + help: "Cumulative count of packets dropped while transmitting", + valueType: prometheus.CounterValue, + extraLabels: []string{"interface"}, getValues: func(s *info.ContainerStats) metricValues { - return metricValues{{value: float64(s.Network.TxDropped)}} + values := make(metricValues, 0, len(s.Network.Interfaces)) + for _, value := range s.Network.Interfaces { + values = append(values, metricValue{ + value: float64(value.TxDropped), + labels: []string{value.Name}, + }) + } + return values }, }, { - name: "container_network_transmit_errors_total", - help: "Cumulative count of errors encountered while transmitting", - valueType: prometheus.CounterValue, + name: "container_network_transmit_errors_total", + help: "Cumulative count of errors encountered while transmitting", + valueType: prometheus.CounterValue, + extraLabels: []string{"interface"}, getValues: func(s *info.ContainerStats) metricValues { - return metricValues{{value: float64(s.Network.TxErrors)}} + values := make(metricValues, 0, len(s.Network.Interfaces)) + for _, value := range s.Network.Interfaces { + values = append(values, metricValue{ + value: float64(value.TxErrors), + labels: []string{value.Name}, + }) + } + return values }, }, { name: "container_tasks_state", diff --git a/metrics/prometheus_test.go b/metrics/prometheus_test.go index 1b8393c3..0bb6c5e1 100644 --- a/metrics/prometheus_test.go +++ b/metrics/prometheus_test.go @@ -71,6 +71,19 @@ func (p testSubcontainersInfoProvider) SubcontainersInfo(string, *info.Container TxErrors: 20, TxDropped: 21, }, + Interfaces: []info.InterfaceStats{ + { + Name: "eth0", + RxBytes: 14, + RxPackets: 15, + RxErrors: 16, + RxDropped: 17, + TxBytes: 18, + TxPackets: 19, + TxErrors: 20, + TxDropped: 21, + }, + }, }, Filesystem: []info.FsStats{ { diff --git a/metrics/testdata/prometheus_metrics b/metrics/testdata/prometheus_metrics index 106f663b..5e7de5ef 100644 --- a/metrics/testdata/prometheus_metrics +++ b/metrics/testdata/prometheus_metrics @@ -79,28 +79,28 @@ container_memory_usage_bytes{id="testcontainer",image="test",name="testcontainer container_memory_working_set_bytes{id="testcontainer",image="test",name="testcontainer"} 9 # HELP container_network_receive_bytes_total Cumulative count of bytes received # TYPE container_network_receive_bytes_total counter -container_network_receive_bytes_total{id="testcontainer",image="test",name="testcontainer"} 14 +container_network_receive_bytes_total{id="testcontainer",image="test",interface="eth0",name="testcontainer"} 14 # HELP container_network_receive_errors_total Cumulative count of errors encountered while receiving # TYPE container_network_receive_errors_total counter -container_network_receive_errors_total{id="testcontainer",image="test",name="testcontainer"} 16 +container_network_receive_errors_total{id="testcontainer",image="test",interface="eth0",name="testcontainer"} 16 # HELP container_network_receive_packets_dropped_total Cumulative count of packets dropped while receiving # TYPE container_network_receive_packets_dropped_total counter -container_network_receive_packets_dropped_total{id="testcontainer",image="test",name="testcontainer"} 17 +container_network_receive_packets_dropped_total{id="testcontainer",image="test",interface="eth0",name="testcontainer"} 17 # HELP container_network_receive_packets_total Cumulative count of packets received # TYPE container_network_receive_packets_total counter -container_network_receive_packets_total{id="testcontainer",image="test",name="testcontainer"} 15 +container_network_receive_packets_total{id="testcontainer",image="test",interface="eth0",name="testcontainer"} 15 # HELP container_network_transmit_bytes_total Cumulative count of bytes transmitted # TYPE container_network_transmit_bytes_total counter -container_network_transmit_bytes_total{id="testcontainer",image="test",name="testcontainer"} 18 +container_network_transmit_bytes_total{id="testcontainer",image="test",interface="eth0",name="testcontainer"} 18 # HELP container_network_transmit_errors_total Cumulative count of errors encountered while transmitting # TYPE container_network_transmit_errors_total counter -container_network_transmit_errors_total{id="testcontainer",image="test",name="testcontainer"} 20 +container_network_transmit_errors_total{id="testcontainer",image="test",interface="eth0",name="testcontainer"} 20 # HELP container_network_transmit_packets_dropped_total Cumulative count of packets dropped while transmitting # TYPE container_network_transmit_packets_dropped_total counter -container_network_transmit_packets_dropped_total{id="testcontainer",image="test",name="testcontainer"} 21 +container_network_transmit_packets_dropped_total{id="testcontainer",image="test",interface="eth0",name="testcontainer"} 21 # HELP container_network_transmit_packets_total Cumulative count of packets transmitted # TYPE container_network_transmit_packets_total counter -container_network_transmit_packets_total{id="testcontainer",image="test",name="testcontainer"} 19 +container_network_transmit_packets_total{id="testcontainer",image="test",interface="eth0",name="testcontainer"} 19 # HELP container_scrape_error 1 if there was an error while getting container metrics, 0 otherwise # TYPE container_scrape_error gauge container_scrape_error 0