From babecb6d4925215f6143b2ffcec79543e2eea080 Mon Sep 17 00:00:00 2001 From: Sergio Jimenez Date: Tue, 9 Feb 2016 23:49:30 +0100 Subject: [PATCH] feat(timeout): Use timeout setting * Use timeout as parameter in the http request * A bit of cleanup * More tests --- plugins/inputs/mesos/mesos.go | 23 +++++++++------- plugins/inputs/mesos/mesos_test.go | 43 +++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/plugins/inputs/mesos/mesos.go b/plugins/inputs/mesos/mesos.go index 8f59e4bef..29c424815 100644 --- a/plugins/inputs/mesos/mesos.go +++ b/plugins/inputs/mesos/mesos.go @@ -7,6 +7,7 @@ import ( "log" "net" "net/http" + "strconv" "strings" "sync" @@ -16,7 +17,7 @@ import ( ) type Mesos struct { - Timeout string + Timeout int Servers []string MetricsCol []string `toml:"metrics_collection"` } @@ -225,7 +226,7 @@ func masterBlocks(g string) []string { ret, ok := m[g] if !ok { - log.Println("Unkown metrics group: ", g) + log.Println("[mesos] Unkown metrics group: ", g) return []string{} } @@ -234,7 +235,7 @@ func masterBlocks(g string) []string { var sampleConfig = ` # Timeout, in ms. - timeout = 2000 + timeout = 100 # A list of Mesos masters. e.g. master1:5050, master2:5080, etc. # The port can be skipped if using the default (5050) # Default value is localhost:5050. @@ -244,7 +245,7 @@ var sampleConfig = ` metrics_collection = ["resources","master","system","slaves","frameworks","messages","evqueue","registrar"] ` -// removeGroup(), remove blacklisted groups +// removeGroup(), remove unwanted groups func (m *Mesos) removeGroup(j *map[string]interface{}) { var ok bool @@ -273,8 +274,14 @@ func (m *Mesos) gatherMetrics(a string, acc telegraf.Accumulator) error { "server": host, } - // TODO: Use Timeout - resp, err := http.Get("http://" + a + "/metrics/snapshot") + if m.Timeout == 0 { + log.Println("[mesos] Missing timeout value, setting default value (100ms)") + m.Timeout = 100 + } + + ts := strconv.Itoa(m.Timeout) + "ms" + + resp, err := http.Get("http://" + a + "/metrics/snapshot?timeout=" + ts) if err != nil { return err @@ -290,9 +297,7 @@ func (m *Mesos) gatherMetrics(a string, acc telegraf.Accumulator) error { return errors.New("Error decoding JSON response") } - //if len(m.Blacklist) > 0 { - // m.removeGroup(&jsonOut) - //} + m.removeGroup(&jsonOut) jf := internal.JSONFlattener{} diff --git a/plugins/inputs/mesos/mesos_test.go b/plugins/inputs/mesos/mesos_test.go index f85f94f0f..6b0a1beae 100644 --- a/plugins/inputs/mesos/mesos_test.go +++ b/plugins/inputs/mesos/mesos_test.go @@ -6,6 +6,7 @@ import ( "net/http" "net/http/httptest" "os" + "reflect" "testing" "github.com/influxdata/telegraf/testutil" @@ -86,9 +87,6 @@ func TestMesosMaster(t *testing.T) { } func TestRemoveGroup(t *testing.T) { - //t.Skip("needs refactoring") - // FIXME: removeGroup() behavior is the opposite as it was, - // this test has to be refactored generateMetrics() m := Mesos{ @@ -111,3 +109,42 @@ func TestRemoveGroup(t *testing.T) { } } } + +func TestMasterBlocks(t *testing.T) { + a := "wrong_key" + expect := []string{} + got := masterBlocks(a) + + if !reflect.DeepEqual(got, expect) { + t.Errorf("Expected empty string slice, got: %v", got) + } +} + +func TestSampleConfig(t *testing.T) { + expect := ` + # Timeout, in ms. + timeout = 100 + # A list of Mesos masters. e.g. master1:5050, master2:5080, etc. + # The port can be skipped if using the default (5050) + # Default value is localhost:5050. + servers = ["localhost:5050"] + # Metrics groups to be collected. + # Default, all enabled. + metrics_collection = ["resources","master","system","slaves","frameworks","messages","evqueue","registrar"] +` + + got := new(Mesos).SampleConfig() + + if expect != got { + t.Errorf("Got %s", got) + } +} + +func TestDescription(t *testing.T) { + expect := "Telegraf plugin for gathering metrics from N Mesos masters" + got := new(Mesos).Description() + + if expect != got { + t.Errorf("Got %s", got) + } +}