Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #9589 to 6.x: Add setting to disable cgroup metrics per core #9591

Merged
merged 1 commit into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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]
- Add settings to disable docker and cgroup cpu metrics per core. {issue}9187[9187] {pull}9194[9194] {pull}9589[9589]
- The `elasticsearch/node` metricset now reports the Elasticsearch cluster UUID. {pull}8771[8771]
- Support GET requests in Jolokia module. {issue}8566[8566] {pull}9226[9226]
- Add freebsd support for the uptime metricset. {pull}9413[9413]
Expand Down
4 changes: 4 additions & 0 deletions metricbeat/module/system/process/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ metricbeat.modules:
process.include_cpu_ticks: true
----

*`process.include_per_cpu`*:: By default metrics per cpu are reported when
available. Setting this option to false will disable the reporting of these
metrics.

*`process.include_top_n`*:: These options allow you to filter out all processes
that are not in the top N by CPU or memory, in order to reduce the number of
documents created. If both the `by_cpu` and `by_memory` options are used, the
Expand Down
24 changes: 14 additions & 10 deletions metricbeat/module/system/process/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// cgroupStatsToMap returns a MapStr containing the data from the stats object.
// If stats is nil then nil is returned.
func cgroupStatsToMap(stats *cgroup.Stats) common.MapStr {
func cgroupStatsToMap(stats *cgroup.Stats, perCPU bool) common.MapStr {
if stats == nil {
return nil
}
Expand All @@ -44,7 +44,7 @@ func cgroupStatsToMap(stats *cgroup.Stats) common.MapStr {
if cpu := cgroupCPUToMapStr(stats.CPU); cpu != nil {
cgroup["cpu"] = cpu
}
if cpuacct := cgroupCPUAccountingToMapStr(stats.CPUAccounting); cpuacct != nil {
if cpuacct := cgroupCPUAccountingToMapStr(stats.CPUAccounting, perCPU); cpuacct != nil {
cgroup["cpuacct"] = cpuacct
}
if memory := cgroupMemoryToMapStr(stats.Memory); memory != nil {
Expand Down Expand Up @@ -97,23 +97,17 @@ func cgroupCPUToMapStr(cpu *cgroup.CPUSubsystem) common.MapStr {
// cgroupCPUAccountingToMapStr returns a MapStr containing
// CPUAccountingSubsystem data. If the cpuacct parameter is nil then nil is
// returned.
func cgroupCPUAccountingToMapStr(cpuacct *cgroup.CPUAccountingSubsystem) common.MapStr {
func cgroupCPUAccountingToMapStr(cpuacct *cgroup.CPUAccountingSubsystem, perCPU bool) common.MapStr {
if cpuacct == nil {
return nil
}

perCPUUsage := common.MapStr{}
for i, usage := range cpuacct.UsagePerCPU {
perCPUUsage[strconv.Itoa(i+1)] = usage
}

return common.MapStr{
event := common.MapStr{
"id": cpuacct.ID,
"path": cpuacct.Path,
"total": common.MapStr{
"ns": cpuacct.TotalNanos,
},
"percpu": perCPUUsage,
"stats": common.MapStr{
"system": common.MapStr{
"ns": cpuacct.Stats.SystemNanos,
Expand All @@ -123,6 +117,16 @@ func cgroupCPUAccountingToMapStr(cpuacct *cgroup.CPUAccountingSubsystem) common.
},
},
}

if perCPU {
perCPUUsage := common.MapStr{}
for i, usage := range cpuacct.UsagePerCPU {
perCPUUsage[strconv.Itoa(i+1)] = usage
}
event["percpu"] = perCPUUsage
}

return event
}

// cgroupMemoryToMapStr returns a MapStr containing MemorySubsystem data. If the
Expand Down
2 changes: 2 additions & 0 deletions metricbeat/module/system/process/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
CacheCmdLine bool `config:"process.cmdline.cache.enabled"`
IncludeTop process.IncludeTopConfig `config:"process.include_top_n"`
IncludeCPUTicks bool `config:"process.include_cpu_ticks"`
IncludePerCPU bool `config:"process.include_per_cpu"`
CPUTicks *bool `config:"cpu_ticks"` // Deprecated
}

Expand All @@ -47,4 +48,5 @@ var defaultConfig = Config{
ByCPU: 0,
ByMemory: 0,
},
IncludePerCPU: true,
}
9 changes: 5 additions & 4 deletions metricbeat/module/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func init() {
// MetricSet that fetches process metrics.
type MetricSet struct {
mb.BaseMetricSet
stats *process.Stats
cgroup *cgroup.Reader
cacheCmdLine bool
stats *process.Stats
cgroup *cgroup.Reader
perCPU bool
}

// New creates and returns a new MetricSet.
Expand All @@ -67,6 +67,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
CacheCmdLine: config.CacheCmdLine,
IncludeTop: config.IncludeTop,
},
perCPU: config.IncludePerCPU,
}
err := m.stats.Init()
if err != nil {
Expand Down Expand Up @@ -116,7 +117,7 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {
continue
}

if statsMap := cgroupStatsToMap(stats); statsMap != nil {
if statsMap := cgroupStatsToMap(stats, m.perCPU); statsMap != nil {
proc["cgroup"] = statsMap
}
}
Expand Down
7 changes: 3 additions & 4 deletions metricbeat/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ def test_process(self):
"extras": {
"process.env.whitelist": ["PATH"],
"process.include_cpu_ticks": True,

# Remove 'percpu' prior to checking documented fields because its keys are dynamic.
"process.include_per_cpu": False,
}
}])
proc = self.start_beat()
Expand All @@ -395,10 +398,6 @@ def test_process(self):
if env is not None:
found_env = True

# Remove 'percpu' prior to checking documented fields because its keys are dynamic.
if "cgroup" in process and "cpuacct" in process["cgroup"]:
del process["cgroup"]["cpuacct"]["percpu"]

self.assert_fields_are_documented(evt)

# Remove optional keys.
Expand Down