From 52df22dfee9c28865e8d8cf7eb88c50aeb527de9 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Fri, 23 Nov 2018 14:47:37 +0100 Subject: [PATCH] Add flag to disable docker cpu metrics collection per core (#9194) --- 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 4928526c566..6ef2846ec35 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -67,6 +67,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha1...master[Check the HEAD d *Metricbeat* +- Add setting to disable docker cpu metrics per core. {pull}9194[9194] - The `elasticsearch/node` metricset now reports the Elasticsearch cluster UUID. {pull}8771[8771] *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 4db8f49b71f..24449a776bf 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