func TestAddEngineStatsPartial(t *testing.T) {
	engine := &Engine{
		ClientConns:   0,
		ClientActive:  0,
		QueriesPerSec: 0,
		ReadsPerSec:   0,
		WritesPerSec:  0,
	}

	var acc testutil.Accumulator

	keys := []string{
		"active_clients",
		"clients",
		"queries_per_sec",
		"read_docs_per_sec",
		"written_docs_per_sec",
	}

	missing_keys := []string{
		"total_queries",
		"total_reads",
		"total_writes",
	}
	engine.AddEngineStats(keys, &acc, tags)

	for _, metric := range missing_keys {
		assert.False(t, acc.HasIntValue(metric))
	}
}
Example #2
0
func TestGatherClusterStats(t *testing.T) {
	es := NewElasticsearch()
	es.Servers = []string{"http://example.com:9200"}
	es.ClusterHealth = true
	es.client.Transport = newTransportMock(http.StatusOK, clusterResponse)

	var acc testutil.Accumulator
	require.NoError(t, es.Gather(&acc))

	var clusterHealthTests = []struct {
		measurement string
		fields      map[string]interface{}
		tags        map[string]string
	}{
		{
			"cluster_health",
			clusterHealthExpected,
			map[string]string{"name": "elasticsearch_telegraf"},
		},
		{
			"indices",
			v1IndexExpected,
			map[string]string{"index": "v1"},
		},
		{
			"indices",
			v2IndexExpected,
			map[string]string{"index": "v2"},
		},
	}

	for _, exp := range clusterHealthTests {
		assert.NoError(t, acc.ValidateTaggedFields(exp.measurement, exp.fields, exp.tags))
	}
}
Example #3
0
func TestAerospikeStatistics(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	a := &Aerospike{
		Servers: []string{testutil.GetLocalHost() + ":3000"},
	}

	var acc testutil.Accumulator

	err := a.Gather(&acc)
	require.NoError(t, err)

	// Only use a few of the metrics
	asMetrics := []string{
		"transactions",
		"stat_write_errs",
		"stat_read_reqs",
		"stat_write_reqs",
	}

	for _, metric := range asMetrics {
		assert.True(t, acc.HasIntValue(metric), metric)
	}

}
Example #4
0
func TestAddNonReplStats(t *testing.T) {
	d := NewMongodbData(
		&StatLine{
			StorageEngine:  "",
			Time:           time.Now(),
			Insert:         0,
			Query:          0,
			Update:         0,
			Delete:         0,
			GetMore:        0,
			Command:        0,
			Flushes:        0,
			Virtual:        0,
			Resident:       0,
			QueuedReaders:  0,
			QueuedWriters:  0,
			ActiveReaders:  0,
			ActiveWriters:  0,
			NetIn:          0,
			NetOut:         0,
			NumConnections: 0,
		},
		tags,
	)
	var acc testutil.Accumulator

	d.AddDefaultStats()
	d.flush(&acc)

	for key, _ := range DefaultStats {
		assert.True(t, acc.HasIntField("mongodb", key))
	}
}
func TestAddTableStats(t *testing.T) {
	var acc testutil.Accumulator

	err := server.addTableStats(&acc)
	require.NoError(t, err)

	for _, metric := range TableTracking {
		assert.True(t, acc.HasIntValue(metric))
	}

	keys := []string{
		"cache_bytes_in_use",
		"disk_read_bytes_per_sec",
		"disk_read_bytes_total",
		"disk_written_bytes_per_sec",
		"disk_written_bytes_total",
		"disk_usage_data_bytes",
		"disk_usage_garbage_bytes",
		"disk_usage_metadata_bytes",
		"disk_usage_preallocated_bytes",
	}

	for _, metric := range keys {
		assert.True(t, acc.HasIntValue(metric))
	}
}
Example #6
0
func testMain(t *testing.T, code string, endpoint string, serverType ServerType) {
	// Build the fake snmpwalk for test
	src := makeFakeSNMPSrc(code)
	defer os.Remove(src)
	buildFakeSNMPCmd(src)
	defer os.Remove("./snmpwalk")
	envPathOrigin := os.Getenv("PATH")
	// Refer to the fake snmpwalk
	os.Setenv("PATH", ".")
	defer os.Setenv("PATH", envPathOrigin)

	l := &LeoFS{
		Servers: []string{endpoint},
	}

	var acc testutil.Accumulator
	acc.SetDebug(true)

	err := l.Gather(&acc)
	require.NoError(t, err)

	floatMetrics := KeyMapping[serverType]

	for _, metric := range floatMetrics {
		assert.True(t, acc.HasFloatField("leofs", metric), metric)
	}
}
Example #7
0
func TestMemcachedGeneratesMetrics(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	m := &Memcached{
		Servers: []string{testutil.GetLocalHost()},
	}

	var acc testutil.Accumulator

	err := m.Gather(&acc)
	require.NoError(t, err)

	intMetrics := []string{"get_hits", "get_misses", "evictions",
		"limit_maxbytes", "bytes", "uptime", "curr_items", "total_items",
		"curr_connections", "total_connections", "connection_structures", "cmd_get",
		"cmd_set", "delete_hits", "delete_misses", "incr_hits", "incr_misses",
		"decr_hits", "decr_misses", "cas_hits", "cas_misses", "evictions",
		"bytes_read", "bytes_written", "threads", "conn_yields"}

	for _, metric := range intMetrics {
		assert.True(t, acc.HasIntValue(metric), metric)
	}
}
Example #8
0
func TestZookeeperGeneratesMetrics(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	z := &Zookeeper{
		Servers: []string{testutil.GetLocalHost()},
	}

	var acc testutil.Accumulator

	err := z.Gather(&acc)
	require.NoError(t, err)

	intMetrics := []string{
		"avg_latency",
		"max_latency",
		"min_latency",
		"packets_received",
		"packets_sent",
		"outstanding_requests",
		"znode_count",
		"watch_count",
		"ephemerals_count",
		"approximate_data_size",
		"open_file_descriptor_count",
		"max_file_descriptor_count",
	}

	for _, metric := range intMetrics {
		assert.True(t, acc.HasIntValue(metric), metric)
	}
}
Example #9
0
// Test that the proper values are ignored or collected
func TestHttpJson200(t *testing.T) {
	httpjson := genMockHttpJson(validJSON, 200)

	var acc testutil.Accumulator
	err := httpjson.Gather(&acc)
	require.NoError(t, err)

	assert.Equal(t, 8, len(acc.Points))

	for _, service := range httpjson.Services {
		for _, srv := range service.Servers {
			require.NoError(t,
				acc.ValidateTaggedValue(
					fmt.Sprintf("%s_parent_child", service.Name),
					3.0,
					map[string]string{"server": srv},
				),
			)
			require.NoError(t,
				acc.ValidateTaggedValue(
					fmt.Sprintf("%s_integer", service.Name),
					4.0,
					map[string]string{"server": srv},
				),
			)
		}
	}
}
Example #10
0
func TestPrometheusGeneratesMetrics(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, sampleTextFormat)
	}))
	defer ts.Close()

	p := &Prometheus{
		Urls: []string{ts.URL},
	}

	var acc testutil.Accumulator

	err := p.Gather(&acc)
	require.NoError(t, err)

	expected := []struct {
		name  string
		value float64
		tags  map[string]string
	}{
		{"go_gc_duration_seconds_count", 7, map[string]string{}},
		{"go_goroutines", 15, map[string]string{}},
	}

	for _, e := range expected {
		assert.NoError(t, acc.ValidateValue(e.name, e.value))
	}
}
Example #11
0
func TestAddEngineStats(t *testing.T) {
	engine := &Engine{
		ClientConns:   0,
		ClientActive:  0,
		QueriesPerSec: 0,
		TotalQueries:  0,
		ReadsPerSec:   0,
		TotalReads:    0,
		WritesPerSec:  0,
		TotalWrites:   0,
	}

	var acc testutil.Accumulator

	keys := []string{
		"active_clients",
		"clients",
		"queries_per_sec",
		"total_queries",
		"read_docs_per_sec",
		"total_reads",
		"written_docs_per_sec",
		"total_writes",
	}
	engine.AddEngineStats(keys, &acc, tags)

	for _, metric := range keys {
		assert.True(t, acc.HasIntField("rethinkdb_engine", metric))
	}
}
Example #12
0
func TestPrometheusWritePointEmptyTag(t *testing.T) {

	p := &prometheus.Prometheus{
		Urls: []string{"http://localhost:9126/metrics"},
	}
	tags := make(map[string]string)
	var points = []*client.Point{
		client.NewPoint(
			"test_point_1",
			tags,
			map[string]interface{}{"value": 0.0}),
		client.NewPoint(
			"test_point_2",
			tags,
			map[string]interface{}{"value": 1.0}),
	}
	require.NoError(t, pTesting.Write(points))

	expected := []struct {
		name  string
		value float64
		tags  map[string]string
	}{
		{"test_point_1", 0.0, tags},
		{"test_point_2", 1.0, tags},
	}

	var acc testutil.Accumulator

	require.NoError(t, p.Gather(&acc))
	for _, e := range expected {
		assert.NoError(t, acc.ValidateValue(e.name, e.value))
	}
}
Example #13
0
func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	p := &Postgresql{
		Servers: []*Server{
			{
				Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
					testutil.GetLocalHost()),
				Databases: []string{"postgres"},
			},
		},
	}

	var acc testutil.Accumulator

	err := p.Gather(&acc)
	require.NoError(t, err)

	point, ok := acc.Get("xact_commit")
	require.True(t, ok)

	assert.Equal(t, "postgres", point.Tags["db"])
}
Example #14
0
func TestStateTag(t *testing.T) {
	d := NewMongodbData(
		&StatLine{
			StorageEngine: "",
			Time:          time.Now(),
			Insert:        0,
			Query:         0,
			NodeType:      "PRI",
		},
		tags,
	)

	stats := []string{"inserts_per_sec", "queries_per_sec"}

	stateTags := make(map[string]string)
	stateTags["state"] = "PRI"

	var acc testutil.Accumulator

	d.AddDefaultStats(&acc)

	for _, key := range stats {
		err := acc.ValidateTaggedValue(key, int64(0), stateTags)
		require.NoError(t, err)
	}
}
Example #15
0
func TestPhpFpmGeneratesMetrics_From_Http(t *testing.T) {
	sv := statServer{}
	ts := httptest.NewServer(sv)
	defer ts.Close()

	r := &phpfpm{
		Urls: []string{ts.URL},
	}

	var acc testutil.Accumulator

	err := r.Gather(&acc)
	require.NoError(t, err)

	tags := map[string]string{
		"pool": "www",
	}

	fields := map[string]interface{}{
		"accepted_conn":        int64(3),
		"listen_queue":         int64(1),
		"max_listen_queue":     int64(0),
		"listen_queue_len":     int64(0),
		"idle_processes":       int64(1),
		"active_processes":     int64(1),
		"total_processes":      int64(2),
		"max_active_processes": int64(1),
		"max_children_reached": int64(2),
		"slow_requests":        int64(1),
	}

	acc.AssertContainsTaggedFields(t, "phpfpm", fields, tags)
}
Example #16
0
func TestExec(t *testing.T) {
	runner := newRunnerMock([]byte(validJson), nil)
	command := Command{Command: "testcommand arg1", Name: "mycollector"}
	e := &Exec{runner: runner, Commands: []*Command{&command}}

	var acc testutil.Accumulator
	err := e.Gather(&acc)
	require.NoError(t, err)

	checkFloat := []struct {
		name  string
		value float64
	}{
		{"mycollector_num_processes", 82},
		{"mycollector_cpu_used", 8234},
		{"mycollector_cpu_free", 32},
		{"mycollector_percent", 0.81},
	}

	for _, c := range checkFloat {
		assert.True(t, acc.CheckValue(c.name, c.value))
	}

	assert.Equal(t, len(acc.Points), 4, "non-numeric measurements should be ignored")
}
Example #17
0
func TestPrometheusWritePointTag(t *testing.T) {

	p := &prometheus.Prometheus{
		Urls: []string{"http://localhost:9126/metrics"},
	}
	tags := make(map[string]string)
	tags["testtag"] = "testvalue"
	var points = []*client.Point{
		client.NewPoint(
			"test_point_3",
			tags,
			map[string]interface{}{"value": 0.0}),
		client.NewPoint(
			"test_point_4",
			tags,
			map[string]interface{}{"value": 1.0}),
	}
	require.NoError(t, pTesting.Write(points))

	expected := []struct {
		name  string
		value float64
	}{
		{"test_point_3", 0.0},
		{"test_point_4", 1.0},
	}

	var acc testutil.Accumulator

	require.NoError(t, p.Gather(&acc))
	for _, e := range expected {
		assert.True(t, acc.CheckTaggedValue(e.name, e.value, tags))
	}
}
Example #18
0
func TestNginxGeneratesMetrics(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var rsp string

		if r.URL.Path == "/stub_status" {
			rsp = sampleResponse
		} else {
			panic("Cannot handle request")
		}

		fmt.Fprintln(w, rsp)
	}))
	defer ts.Close()

	n := &Nginx{
		Urls: []string{fmt.Sprintf("%s/stub_status", ts.URL)},
	}

	var acc testutil.Accumulator

	err := n.Gather(&acc)
	require.NoError(t, err)

	metrics := []struct {
		name  string
		value uint64
	}{
		{"active", 585},
		{"accepts", 85340},
		{"handled", 85340},
		{"requests", 35085},
		{"reading", 4},
		{"writing", 135},
		{"waiting", 446},
	}
	addr, err := url.Parse(ts.URL)
	if err != nil {
		panic(err)
	}

	host, port, err := net.SplitHostPort(addr.Host)
	if err != nil {
		host = addr.Host
		if addr.Scheme == "http" {
			port = "80"
		} else if addr.Scheme == "https" {
			port = "443"
		} else {
			port = ""
		}
	}

	tags := map[string]string{"server": host, "port": port}

	for _, m := range metrics {
		assert.NoError(t, acc.ValidateTaggedValue(m.name, m.value, tags))
	}
}
Example #19
0
// Test response to malformed JSON
func TestHttpJsonBadJson(t *testing.T) {
	httpjson := genMockHttpJson(invalidJSON, 200)

	var acc testutil.Accumulator
	err := httpjson[0].Gather(&acc)

	assert.NotNil(t, err)
	assert.Equal(t, 0, acc.NFields())
}
Example #20
0
// Test response to empty string as response objectgT
func TestHttpJsonEmptyResponse(t *testing.T) {
	httpjson := genMockHttpJson(empty, 200)

	var acc testutil.Accumulator
	err := httpjson[0].Gather(&acc)

	assert.NotNil(t, err)
	assert.Equal(t, 0, acc.NFields())
}
Example #21
0
func TestMysqlGeneratesMetrics(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	m := &Mysql{
		Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
	}

	var acc testutil.Accumulator

	err := m.Gather(&acc)
	require.NoError(t, err)

	prefixes := []struct {
		prefix string
		count  int
	}{
		{"commands", 139},
		{"handler", 16},
		{"bytes", 2},
		{"innodb", 46},
		{"threads", 4},
		{"aborted", 2},
		{"created", 3},
		{"key", 7},
		{"open", 7},
		{"opened", 3},
		{"qcache", 8},
		{"table", 1},
	}

	intMetrics := []string{
		"queries",
		"slow_queries",
		"connections",
	}

	for _, prefix := range prefixes {
		var count int

		for _, p := range acc.Points {
			if strings.HasPrefix(p.Measurement, prefix.prefix) {
				count++
			}
		}

		if prefix.count > count {
			t.Errorf("Expected less than %d measurements with prefix %s, got %d",
				count, prefix.prefix, prefix.count)
		}
	}

	for _, metric := range intMetrics {
		assert.True(t, acc.HasIntValue(metric))
	}
}
func TestAddMemberStats(t *testing.T) {
	var acc testutil.Accumulator

	err := server.addMemberStats(&acc)
	require.NoError(t, err)

	for _, metric := range MemberTracking {
		assert.True(t, acc.HasIntValue(metric))
	}
}
Example #23
0
// Test response to HTTP 405
func TestHttpJsonBadMethod(t *testing.T) {
	httpjson := genMockHttpJson(validJSON, 200)
	httpjson[0].Method = "NOT_A_REAL_METHOD"

	var acc testutil.Accumulator
	err := httpjson[0].Gather(&acc)

	assert.NotNil(t, err)
	assert.Equal(t, 0, acc.NFields())
}
Example #24
0
func TestHaproxyGeneratesMetricsWithoutAuthentication(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, csvOutputSample)
	}))
	defer ts.Close()

	r := &haproxy{
		Servers: []string{ts.URL},
	}

	var acc testutil.Accumulator

	err := r.Gather(&acc)
	require.NoError(t, err)

	tags := map[string]string{
		"proxy":  "be_app",
		"server": ts.Listener.Addr().String(),
		"sv":     "host0",
	}

	fields := map[string]interface{}{
		"active_servers":    uint64(1),
		"backup_servers":    uint64(0),
		"bin":               uint64(510913516),
		"bout":              uint64(2193856571),
		"check_duration":    uint64(10),
		"cli_abort":         uint64(73),
		"ctime":             uint64(2),
		"downtime":          uint64(0),
		"dresp":             uint64(0),
		"econ":              uint64(0),
		"eresp":             uint64(1),
		"http_response.1xx": uint64(0),
		"http_response.2xx": uint64(119534),
		"http_response.3xx": uint64(48051),
		"http_response.4xx": uint64(2345),
		"http_response.5xx": uint64(1056),
		"lbtot":             uint64(171013),
		"qcur":              uint64(0),
		"qmax":              uint64(0),
		"qtime":             uint64(0),
		"rate":              uint64(3),
		"rate_max":          uint64(12),
		"rtime":             uint64(312),
		"scur":              uint64(1),
		"smax":              uint64(32),
		"srv_abort":         uint64(1),
		"stot":              uint64(171014),
		"ttime":             uint64(2341),
		"wredis":            uint64(0),
		"wretr":             uint64(1),
	}
	acc.AssertContainsTaggedFields(t, "haproxy", fields, tags)
}
Example #25
0
func TestExecMalformed(t *testing.T) {
	e := &Exec{
		runner:  newRunnerMock([]byte(malformedJson), nil),
		Command: "badcommand arg1",
	}

	var acc testutil.Accumulator
	err := e.Gather(&acc)
	require.Error(t, err)
	assert.Equal(t, acc.NFields(), 0, "No new points should have been added")
}
Example #26
0
func TestCommandError(t *testing.T) {
	e := &Exec{
		runner:  newRunnerMock(nil, fmt.Errorf("exit status code 1")),
		Command: "badcommand",
	}

	var acc testutil.Accumulator
	err := e.Gather(&acc)
	require.Error(t, err)
	assert.Equal(t, acc.NFields(), 0, "No new points should have been added")
}
Example #27
0
// Test that the proper values are ignored or collected
func TestHttpJsonSingleValueTags(t *testing.T) {

	jolokia := genJolokiaClientStub(validSingleValueJSON, 200, Servers, []Metric{UsedHeapMetric})

	var acc testutil.Accumulator
	err := jolokia.Gather(&acc)

	assert.Nil(t, err)
	assert.Equal(t, 1, len(acc.Points))
	assert.NoError(t, acc.ValidateTaggedFieldsValue("heap_memory_usage", map[string]interface{}{"value": 209274376.0}, map[string]string{"host": "127.0.0.1", "port": "8080", "server": "as1"}))
}
Example #28
0
// Test that the proper values are ignored or collected
func TestHttpJsonOn404(t *testing.T) {

	jolokia := genJolokiaClientStub(validMultiValueJSON, 404, Servers,
		[]Metric{UsedHeapMetric})

	var acc testutil.Accumulator
	acc.SetDebug(true)
	err := jolokia.Gather(&acc)

	assert.Nil(t, err)
	assert.Equal(t, 0, len(acc.Points))
}
Example #29
0
// Test that the proper values are ignored or collected
func TestHttpJsonMultiValueWithPass(t *testing.T) {

	jolokia := genJolokiaClientStub(validMultiValueJSON, 200, Servers, []Metric{UsedHeapMetric})

	var acc testutil.Accumulator
	err := jolokia.Gather(&acc)

	assert.Nil(t, err)
	assert.Equal(t, 1, len(acc.Points))

	assert.True(t, acc.CheckFieldsValue("heap_memory_usage", map[string]interface{}{"used": 203288528.0}))
}
Example #30
0
func TestNginxGeneratesMetrics(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var rsp string

		if r.URL.Path == "/stub_status" {
			rsp = sampleResponse
		} else {
			panic("Cannot handle request")
		}

		fmt.Fprintln(w, rsp)
	}))
	defer ts.Close()

	n := &Nginx{
		Urls: []string{fmt.Sprintf("%s/stub_status", ts.URL)},
	}

	var acc testutil.Accumulator

	err := n.Gather(&acc)
	require.NoError(t, err)

	fields := map[string]interface{}{
		"active":   uint64(585),
		"accepts":  uint64(85340),
		"handled":  uint64(85340),
		"requests": uint64(35085),
		"reading":  uint64(4),
		"writing":  uint64(135),
		"waiting":  uint64(446),
	}
	addr, err := url.Parse(ts.URL)
	if err != nil {
		panic(err)
	}

	host, port, err := net.SplitHostPort(addr.Host)
	if err != nil {
		host = addr.Host
		if addr.Scheme == "http" {
			port = "80"
		} else if addr.Scheme == "https" {
			port = "443"
		} else {
			port = ""
		}
	}

	tags := map[string]string{"server": host, "port": port}
	acc.AssertContainsTaggedFields(t, "nginx", fields, tags)
}