From f9ce7e86b7905a5e4bfa9354b95b4e3d59e548b2 Mon Sep 17 00:00:00 2001 From: Alex Kristiansen Date: Fri, 5 Apr 2019 14:28:38 -0500 Subject: [PATCH 1/2] check for a valid limit before we process a memory event --- metricbeat/module/docker/memory/helper.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/metricbeat/module/docker/memory/helper.go b/metricbeat/module/docker/memory/helper.go index b7c63b91881..00c11173d7b 100644 --- a/metricbeat/module/docker/memory/helper.go +++ b/metricbeat/module/docker/memory/helper.go @@ -22,6 +22,7 @@ import ( "github.com/elastic/beats/metricbeat/module/docker" ) +//MemoryData contains parsed container memory info type MemoryData struct { Time common.Time Container *docker.Container @@ -34,11 +35,18 @@ type MemoryData struct { UsageP float64 } +//MemoryService is placeholder for the the memory stat parsers type MemoryService struct{} func (s *MemoryService) getMemoryStatsList(rawStats []docker.Stat, dedot bool) []MemoryData { formattedStats := []MemoryData{} for _, myRawStats := range rawStats { + //There appears to be a race where a container will report with a stat object before it actually starts + //during this time, there doesn't appear to be any meaningful data, + // and Limit will never be 0 unless the container is not running & there's no cgroup data + if myRawStats.Stats.MemoryStats.Limit == 0 { + continue + } formattedStats = append(formattedStats, s.getMemoryStats(myRawStats, dedot)) } From 6cad5e77eddc96a4e048debcc4addab2fdadd5e9 Mon Sep 17 00:00:00 2001 From: Alex Kristiansen Date: Mon, 8 Apr 2019 09:59:57 -0500 Subject: [PATCH 2/2] add changelog entries, clean up varable names/docs --- CHANGELOG.next.asciidoc | 1 + metricbeat/module/docker/memory/helper.go | 12 ++++++------ metricbeat/module/docker/memory/memory.go | 1 + 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 2ec5df0e91b..5c9a484f11c 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -76,6 +76,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d *Metricbeat* - Add _bucket to histogram metrics in Prometheus Collector {pull}11578[11578] +- Prevent the docker/memory metricset from processing invalid events before container start {pull}11676[11676] *Packetbeat* diff --git a/metricbeat/module/docker/memory/helper.go b/metricbeat/module/docker/memory/helper.go index 00c11173d7b..69f26c370ac 100644 --- a/metricbeat/module/docker/memory/helper.go +++ b/metricbeat/module/docker/memory/helper.go @@ -22,7 +22,7 @@ import ( "github.com/elastic/beats/metricbeat/module/docker" ) -//MemoryData contains parsed container memory info +// MemoryData contains parsed container memory info type MemoryData struct { Time common.Time Container *docker.Container @@ -35,19 +35,19 @@ type MemoryData struct { UsageP float64 } -//MemoryService is placeholder for the the memory stat parsers +// MemoryService is placeholder for the the memory stat parsers type MemoryService struct{} -func (s *MemoryService) getMemoryStatsList(rawStats []docker.Stat, dedot bool) []MemoryData { +func (s *MemoryService) getMemoryStatsList(containers []docker.Stat, dedot bool) []MemoryData { formattedStats := []MemoryData{} - for _, myRawStats := range rawStats { + for _, containerStats := range containers { //There appears to be a race where a container will report with a stat object before it actually starts //during this time, there doesn't appear to be any meaningful data, // and Limit will never be 0 unless the container is not running & there's no cgroup data - if myRawStats.Stats.MemoryStats.Limit == 0 { + if containerStats.Stats.MemoryStats.Limit == 0 { continue } - formattedStats = append(formattedStats, s.getMemoryStats(myRawStats, dedot)) + formattedStats = append(formattedStats, s.getMemoryStats(containerStats, dedot)) } return formattedStats diff --git a/metricbeat/module/docker/memory/memory.go b/metricbeat/module/docker/memory/memory.go index 142de586a19..873f556268d 100644 --- a/metricbeat/module/docker/memory/memory.go +++ b/metricbeat/module/docker/memory/memory.go @@ -32,6 +32,7 @@ func init() { ) } +// MetricSet type defines all fields of the MetricSet type MetricSet struct { mb.BaseMetricSet memoryService *MemoryService