From 760a25b94f52cab5c74e3f66382b805895aa8214 Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Thu, 10 Sep 2015 11:59:16 +0100 Subject: [PATCH] Add machine memory & cpu cores to prometheus metrics --- metrics/prometheus.go | 22 ++++++++++++++++++++-- metrics/prometheus_test.go | 7 +++++++ metrics/testdata/prometheus_metrics | 6 ++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/metrics/prometheus.go b/metrics/prometheus.go index 077853a9..2e8ec06a 100644 --- a/metrics/prometheus.go +++ b/metrics/prometheus.go @@ -27,8 +27,10 @@ import ( type infoProvider interface { // Get information about all subcontainers of the specified container (includes self). SubcontainersInfo(containerName string, query *info.ContainerInfoRequest) ([]*info.ContainerInfo, error) - // Get information about the machine. + // Get information about the version. GetVersionInfo() (*info.VersionInfo, error) + // Get information about the machine. + GetMachineInfo() (*info.MachineInfo, error) } // metricValue describes a single metric value for a given set of label values @@ -444,7 +446,9 @@ func NewPrometheusCollector(infoProvider infoProvider) *PrometheusCollector { } var ( - versionInfoDesc = prometheus.NewDesc("cadvisor_version_info", "A metric with a constant '1' value labeled by kernel version, OS version, docker version & cadvisor version.", []string{"kernelVersion", "osVersion", "dockerVersion", "cadvisorVersion"}, nil) + versionInfoDesc = prometheus.NewDesc("cadvisor_version_info", "A metric with a constant '1' value labeled by kernel version, OS version, docker version & cadvisor version.", []string{"kernelVersion", "osVersion", "dockerVersion", "cadvisorVersion"}, nil) + machineInfoCoresDesc = prometheus.NewDesc("machine_cpu_cores", "Number of CPU cores on the machine.", nil, nil) + machineInfoMemoryDesc = prometheus.NewDesc("machine_memory_bytes", "Amount of memory installed on the machine.", nil, nil) ) // Describe describes all the metrics ever exported by cadvisor. It @@ -455,11 +459,14 @@ func (c *PrometheusCollector) Describe(ch chan<- *prometheus.Desc) { ch <- cm.desc([]string{}) } ch <- versionInfoDesc + ch <- machineInfoCoresDesc + ch <- machineInfoMemoryDesc } // Collect fetches the stats from all containers and delivers them as // Prometheus metrics. It implements prometheus.PrometheusCollector. func (c *PrometheusCollector) Collect(ch chan<- prometheus.Metric) { + c.collectMachineInfo(ch) c.collectVersionInfo(ch) c.collectContainersInfo(ch) c.errors.Collect(ch) @@ -523,6 +530,17 @@ func (c *PrometheusCollector) collectVersionInfo(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric(versionInfoDesc, prometheus.GaugeValue, 1, []string{versionInfo.KernelVersion, versionInfo.ContainerOsVersion, versionInfo.DockerVersion, versionInfo.CadvisorVersion}...) } +func (c *PrometheusCollector) collectMachineInfo(ch chan<- prometheus.Metric) { + machineInfo, err := c.infoProvider.GetMachineInfo() + if err != nil { + c.errors.Set(1) + glog.Warningf("Couldn't get machine info: %s", err) + return + } + ch <- prometheus.MustNewConstMetric(machineInfoCoresDesc, prometheus.GaugeValue, float64(machineInfo.NumCores)) + ch <- prometheus.MustNewConstMetric(machineInfoMemoryDesc, prometheus.GaugeValue, float64(machineInfo.MemoryCapacity)) +} + // Size after which we consider memory to be "unlimited". This is not // MaxInt64 due to rounding by the kernel. const maxMemorySize = uint64(1 << 62) diff --git a/metrics/prometheus_test.go b/metrics/prometheus_test.go index 8c7370be..2ca5fa81 100644 --- a/metrics/prometheus_test.go +++ b/metrics/prometheus_test.go @@ -38,6 +38,13 @@ func (p testSubcontainersInfoProvider) GetVersionInfo() (*info.VersionInfo, erro }, nil } +func (p testSubcontainersInfoProvider) GetMachineInfo() (*info.MachineInfo, error) { + return &info.MachineInfo{ + NumCores: 4, + MemoryCapacity: 1024, + }, nil +} + func (p testSubcontainersInfoProvider) SubcontainersInfo(string, *info.ContainerInfoRequest) ([]*info.ContainerInfo, error) { return []*info.ContainerInfo{ { diff --git a/metrics/testdata/prometheus_metrics b/metrics/testdata/prometheus_metrics index 9413fc20..b4d089bf 100644 --- a/metrics/testdata/prometheus_metrics +++ b/metrics/testdata/prometheus_metrics @@ -138,6 +138,12 @@ http_response_size_bytes{handler="prometheus",quantile="0.9"} 0 http_response_size_bytes{handler="prometheus",quantile="0.99"} 0 http_response_size_bytes_sum{handler="prometheus"} 0 http_response_size_bytes_count{handler="prometheus"} 0 +# HELP machine_cpu_cores Number of CPU cores on the machine. +# TYPE machine_cpu_cores gauge +machine_cpu_cores 4 +# HELP machine_memory_bytes Amount of memory installed on the machine. +# TYPE machine_memory_bytes gauge +machine_memory_bytes 1024 # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds. # TYPE process_cpu_seconds_total counter process_cpu_seconds_total 0