feat(timeout): Use timeout setting

* Use timeout as parameter in the http request
* A bit of cleanup
* More tests
This commit is contained in:
Sergio Jimenez 2016-02-09 23:49:30 +01:00
parent 9770802901
commit babecb6d49
2 changed files with 54 additions and 12 deletions

View File

@ -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{}

View File

@ -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)
}
}