From ae816dcc17fe6b2dffe9cf3fb888c1822f06fcf4 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Fri, 23 Nov 2018 20:17:57 +0100 Subject: [PATCH] Cherry-pick #9194 to 6.x: Add flag to disable docker cpu metrics collection per core (#9218) (cherry picked from commit 52df22dfee9c28865e8d8cf7eb88c50aeb527de9) --- CHANGELOG.asciidoc | 1 + metricbeat/docs/modules/docker.asciidoc | 3 +++ metricbeat/metricbeat.reference.yml | 3 +++ .../module/docker/_meta/config.reference.yml | 3 +++ metricbeat/module/docker/cpu/cpu.go | 11 ++++++++++- metricbeat/module/docker/cpu/helper.go | 14 +++++++++++--- 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index be9c348197c..4600536b225 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -89,6 +89,7 @@ https://github.com/elastic/beats/compare/v6.5.0...6.x[Check the HEAD diff] - Collect custom cluster `display_name` in `elasticsearch/cluster_stats` metricset. {pull}8445[8445] - Test etcd module with etcd 3.3. {pull}9068[9068] +- Add setting to disable docker cpu metrics per core. {pull}9194[9194] *Packetbeat* diff --git a/metricbeat/docs/modules/docker.asciidoc b/metricbeat/docs/modules/docker.asciidoc index 4ba7b5ee641..689b1d31de1 100644 --- a/metricbeat/docs/modules/docker.asciidoc +++ b/metricbeat/docs/modules/docker.asciidoc @@ -50,6 +50,9 @@ metricbeat.modules: # If set to true, replace dots in labels with `_`. #labels.dedot: false + # If set to true, collects metrics per core. + #cpu.cores: true + # To connect to Docker over TLS you must specify a client and CA certificate. #ssl: #certificate_authority: "/etc/pki/root/ca.pem" diff --git a/metricbeat/metricbeat.reference.yml b/metricbeat/metricbeat.reference.yml index cddddfeca51..59e436fa1a8 100644 --- a/metricbeat/metricbeat.reference.yml +++ b/metricbeat/metricbeat.reference.yml @@ -178,6 +178,9 @@ metricbeat.modules: # If set to true, replace dots in labels with `_`. #labels.dedot: false + # If set to true, collects metrics per core. + #cpu.cores: true + # To connect to Docker over TLS you must specify a client and CA certificate. #ssl: #certificate_authority: "/etc/pki/root/ca.pem" diff --git a/metricbeat/module/docker/_meta/config.reference.yml b/metricbeat/module/docker/_meta/config.reference.yml index b06bafddfa1..2ac7c913ab3 100644 --- a/metricbeat/module/docker/_meta/config.reference.yml +++ b/metricbeat/module/docker/_meta/config.reference.yml @@ -15,6 +15,9 @@ # If set to true, replace dots in labels with `_`. #labels.dedot: false + # If set to true, collects metrics per core. + #cpu.cores: true + # To connect to Docker over TLS you must specify a client and CA certificate. #ssl: #certificate_authority: "/etc/pki/root/ca.pem" diff --git a/metricbeat/module/docker/cpu/cpu.go b/metricbeat/module/docker/cpu/cpu.go index 4e78f031ac1..954eb37d6d2 100644 --- a/metricbeat/module/docker/cpu/cpu.go +++ b/metricbeat/module/docker/cpu/cpu.go @@ -51,10 +51,19 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { return nil, err } + cpuConfig := struct { + Cores bool `config:"cpu.cores"` + }{ + Cores: true, + } + if err := base.Module().UnpackConfig(&cpuConfig); err != nil { + return nil, err + } + return &MetricSet{ BaseMetricSet: base, dockerClient: client, - cpuService: &CPUService{}, + cpuService: &CPUService{Cores: cpuConfig.Cores}, dedot: config.DeDot, }, nil } diff --git a/metricbeat/module/docker/cpu/helper.go b/metricbeat/module/docker/cpu/helper.go index 03accc2245a..cfee7ac0d08 100644 --- a/metricbeat/module/docker/cpu/helper.go +++ b/metricbeat/module/docker/cpu/helper.go @@ -38,7 +38,10 @@ type CPUStats struct { SystemUsagePercentage float64 } -type CPUService struct{} +// CPUService is a helper to collect docker CPU metrics +type CPUService struct { + Cores bool +} func NewCpuService() *CPUService { return &CPUService{} @@ -57,10 +60,9 @@ func (c *CPUService) getCPUStatsList(rawStats []docker.Stat, dedot bool) []CPUSt func (c *CPUService) getCPUStats(myRawStat *docker.Stat, dedot bool) CPUStats { usage := cpuUsage{Stat: myRawStat} - return CPUStats{ + stats := CPUStats{ Time: common.Time(myRawStat.Stats.Read), Container: docker.NewContainer(myRawStat.Container, dedot), - PerCpuUsage: usage.PerCPU(), TotalUsage: usage.Total(), UsageInKernelmode: myRawStat.Stats.CPUStats.CPUUsage.UsageInKernelmode, UsageInKernelmodePercentage: usage.InKernelMode(), @@ -69,6 +71,12 @@ func (c *CPUService) getCPUStats(myRawStat *docker.Stat, dedot bool) CPUStats { SystemUsage: myRawStat.Stats.CPUStats.SystemUsage, SystemUsagePercentage: usage.System(), } + + if c.Cores { + stats.PerCpuUsage = usage.PerCPU() + } + + return stats } // TODO: These helper should be merged with the cpu helper in system/cpu