feat(timeout): Use timeout setting
* Use timeout as parameter in the http request * A bit of cleanup * More tests
This commit is contained in:
		
							parent
							
								
									9770802901
								
							
						
					
					
						commit
						babecb6d49
					
				|  | @ -7,6 +7,7 @@ import ( | ||||||
| 	"log" | 	"log" | ||||||
| 	"net" | 	"net" | ||||||
| 	"net/http" | 	"net/http" | ||||||
|  | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"sync" | 	"sync" | ||||||
| 
 | 
 | ||||||
|  | @ -16,7 +17,7 @@ import ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type Mesos struct { | type Mesos struct { | ||||||
| 	Timeout    string | 	Timeout    int | ||||||
| 	Servers    []string | 	Servers    []string | ||||||
| 	MetricsCol []string `toml:"metrics_collection"` | 	MetricsCol []string `toml:"metrics_collection"` | ||||||
| } | } | ||||||
|  | @ -225,7 +226,7 @@ func masterBlocks(g string) []string { | ||||||
| 	ret, ok := m[g] | 	ret, ok := m[g] | ||||||
| 
 | 
 | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		log.Println("Unkown metrics group: ", g) | 		log.Println("[mesos] Unkown metrics group: ", g) | ||||||
| 		return []string{} | 		return []string{} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -234,7 +235,7 @@ func masterBlocks(g string) []string { | ||||||
| 
 | 
 | ||||||
| var sampleConfig = ` | var sampleConfig = ` | ||||||
|   # Timeout, in ms. |   # Timeout, in ms. | ||||||
|   timeout = 2000 |   timeout = 100 | ||||||
|   # A list of Mesos masters. e.g. master1:5050, master2:5080, etc. |   # A list of Mesos masters. e.g. master1:5050, master2:5080, etc. | ||||||
|   # The port can be skipped if using the default (5050) |   # The port can be skipped if using the default (5050) | ||||||
|   # Default value is localhost:5050. |   # Default value is localhost:5050. | ||||||
|  | @ -244,7 +245,7 @@ var sampleConfig = ` | ||||||
|   metrics_collection = ["resources","master","system","slaves","frameworks","messages","evqueue","registrar"] |   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{}) { | func (m *Mesos) removeGroup(j *map[string]interface{}) { | ||||||
| 	var ok bool | 	var ok bool | ||||||
| 
 | 
 | ||||||
|  | @ -273,8 +274,14 @@ func (m *Mesos) gatherMetrics(a string, acc telegraf.Accumulator) error { | ||||||
| 		"server": host, | 		"server": host, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// TODO: Use Timeout
 | 	if m.Timeout == 0 { | ||||||
| 	resp, err := http.Get("http://" + a + "/metrics/snapshot") | 		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 { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
|  | @ -290,9 +297,7 @@ func (m *Mesos) gatherMetrics(a string, acc telegraf.Accumulator) error { | ||||||
| 		return errors.New("Error decoding JSON response") | 		return errors.New("Error decoding JSON response") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	//if len(m.Blacklist) > 0 {
 | 	m.removeGroup(&jsonOut) | ||||||
| 	//	m.removeGroup(&jsonOut)
 |  | ||||||
| 	//}
 |  | ||||||
| 
 | 
 | ||||||
| 	jf := internal.JSONFlattener{} | 	jf := internal.JSONFlattener{} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ import ( | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"net/http/httptest" | 	"net/http/httptest" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"reflect" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/influxdata/telegraf/testutil" | 	"github.com/influxdata/telegraf/testutil" | ||||||
|  | @ -86,9 +87,6 @@ func TestMesosMaster(t *testing.T) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestRemoveGroup(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() | 	generateMetrics() | ||||||
| 
 | 
 | ||||||
| 	m := Mesos{ | 	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) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue