From f7dda9742f1992647ce03bc503baea3a702da3a1 Mon Sep 17 00:00:00 2001 From: Sergio Jimenez Date: Thu, 4 Feb 2016 02:46:20 +0100 Subject: [PATCH] feat(whitelist): Converted black to whitelist * Defined global var for holding default metric groups * Refactor removeGroup() to work with the whitelist * Refactor TestRemoveGroup() --- plugins/inputs/mesos/mesos.go | 43 +++++++++++++++++++++++------- plugins/inputs/mesos/mesos_test.go | 30 +++++++++------------ 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/plugins/inputs/mesos/mesos.go b/plugins/inputs/mesos/mesos.go index 81e85ed66..8f59e4bef 100644 --- a/plugins/inputs/mesos/mesos.go +++ b/plugins/inputs/mesos/mesos.go @@ -21,6 +21,11 @@ type Mesos struct { MetricsCol []string `toml:"metrics_collection"` } +var defaultMetrics = []string{ + "resources", "master", "system", "slaves", "frameworks", + "tasks", "messages", "evqueue", "messages", "registrar", +} + // SampleConfig returns a sample configuration block func (m *Mesos) SampleConfig() string { return sampleConfig @@ -66,6 +71,27 @@ func (m *Mesos) Gather(acc telegraf.Accumulator) error { return nil } +func metricsDiff(w []string) []string { + b := []string{} + s := make(map[string]bool) + + if len(w) == 0 { + return b + } + + for _, v := range w { + s[v] = true + } + + for _, d := range defaultMetrics { + if _, ok := s[d]; !ok { + b = append(b, d) + } + } + + return b +} + func masterBlocks(g string) []string { var m map[string][]string @@ -215,23 +241,20 @@ var sampleConfig = ` servers = ["localhost:5050"] # Metrics groups to be collected. # Default, all enabled. - metrics_collection = ["resources","master","system","slaves","frameworks","messages","evqueues","registrar"] + metrics_collection = ["resources","master","system","slaves","frameworks","messages","evqueue","registrar"] ` // removeGroup(), remove blacklisted groups func (m *Mesos) removeGroup(j *map[string]interface{}) { var ok bool - u := map[string]bool{} - for _, v := range m.MetricsCol { - for _, k := range masterBlocks(v) { - u[k] = true - } - } + b := metricsDiff(m.MetricsCol) - for k, _ := range u { - if _, ok = (*j)[k]; ok { - delete((*j), k) + for _, k := range b { + for _, v := range masterBlocks(k) { + if _, ok = (*j)[v]; ok { + delete((*j), v) + } } } } diff --git a/plugins/inputs/mesos/mesos_test.go b/plugins/inputs/mesos/mesos_test.go index 1f69e4ebb..f85f94f0f 100644 --- a/plugins/inputs/mesos/mesos_test.go +++ b/plugins/inputs/mesos/mesos_test.go @@ -89,29 +89,25 @@ func TestRemoveGroup(t *testing.T) { //t.Skip("needs refactoring") // FIXME: removeGroup() behavior is the opposite as it was, // this test has to be refactored - j := []string{ - "resources", "master", - "system", "slaves", "frameworks", - "tasks", "messages", "evqueue", - "messages", "registrar", - } - generateMetrics() - for _, v := range j { - m := Mesos{ - MetricsCol: []string{v}, - } - m.removeGroup(&mesosMetrics) + m := Mesos{ + MetricsCol: []string{ + "resources", "master", "registrar", + }, + } + b := []string{ + "system", "slaves", "frameworks", + "messages", "evqueue", + } + + m.removeGroup(&mesosMetrics) + + for _, v := range b { for _, x := range masterBlocks(v) { if _, ok := mesosMetrics[x]; ok { t.Errorf("Found key %s, it should be gone.", x) } } } - - if len(mesosMetrics) > 0 { - t.Error("Keys were left at slice sample") - } - //Test for wrong keys }