telegraf/plugins/inputs/kapacitor/kapacitor_test.go

142 lines
27 KiB
Go
Raw Normal View History

2017-04-27 18:47:22 +00:00
package kapacitor_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/influxdata/telegraf/plugins/inputs/kapacitor"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
func TestKapacitor(t *testing.T) {
fakeInfluxServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/endpoint" {
_, _ = w.Write([]byte(kapacitorReturn))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer fakeInfluxServer.Close()
plugin := &kapacitor.Kapacitor{
URLs: []string{fakeInfluxServer.URL + "/endpoint"},
}
var acc testutil.Accumulator
require.NoError(t, plugin.Gather(&acc))
require.Len(t, acc.Metrics, 63)
fields := map[string]interface{}{
"alloc_bytes": int64(6950624),
"buck_hash_sys_bytes": int64(1446737),
"frees": int64(129656),
"gcc_pu_fraction": float64(0.006757149597237818),
"gc_sys_bytes": int64(575488),
"heap_alloc_bytes": int64(6950624),
"heap_idle_bytes": int64(499712),
"heap_in_use_bytes": int64(9166848),
"heap_objects": int64(28070),
"heap_released_bytes": int64(0),
"heap_sys_bytes": int64(9666560),
"last_gc_ns": int64(1478813691405406556),
"lookups": int64(40),
"mallocs": int64(157726),
"mcache_in_use_bytes": int64(9600),
"mcache_sys_bytes": int64(16384),
"mspan_in_use_bytes": int64(105600),
"mspan_sys_bytes": int64(114688),
"next_gc_ns": int64(10996691),
"num_gc": int64(4),
"other_sys_bytes": int64(1985959),
"pause_total_ns": int64(767327),
"stack_in_use_bytes": int64(819200),
"stack_sys_bytes": int64(819200),
"sys_bytes": int64(14625016),
"total_alloc_bytes": int64(13475176),
}
tags := map[string]string{
"kap_version": "1.1.0~rc2",
"url": fakeInfluxServer.URL + "/endpoint",
}
acc.AssertContainsTaggedFields(t, "kapacitor_memstats", fields, tags)
acc.AssertContainsTaggedFields(t, "kapacitor",
map[string]interface{}{
"num_enabled_tasks": 5,
"num_subscriptions": 6,
"num_tasks": 5,
}, tags)
}
func TestMissingStats(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{}`))
}))
defer server.Close()
plugin := &kapacitor.Kapacitor{
URLs: []string{server.URL},
}
var acc testutil.Accumulator
plugin.Gather(&acc)
require.False(t, acc.HasField("kapacitor_memstats", "alloc_bytes"))
require.True(t, acc.HasField("kapacitor", "num_tasks"))
}
func TestErrorHandling(t *testing.T) {
badServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/endpoint" {
_, _ = w.Write([]byte("not json"))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer badServer.Close()
plugin := &kapacitor.Kapacitor{
URLs: []string{badServer.URL + "/endpoint"},
}
var acc testutil.Accumulator
plugin.Gather(&acc)
acc.WaitError(1)
require.Equal(t, uint64(0), acc.NMetrics())
}
func TestErrorHandling404(t *testing.T) {
badServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer badServer.Close()
plugin := &kapacitor.Kapacitor{
URLs: []string{badServer.URL},
}
var acc testutil.Accumulator
plugin.Gather(&acc)
acc.WaitError(1)
require.Equal(t, uint64(0), acc.NMetrics())
}
const kapacitorReturn = `{
"cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842",
"cmdline": ["./build/kapacitord","-config","/Users/ross/.kapacitor/config"],
"host": "localhost",
"kapacitor": {"4360b884-4f1b-4915-90b9-e40a27cd366a": {"name": "ingress", "tags": {"task_master": "main", "database": "_internal", "retention_policy": "monitor", "measurement": "cq", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost"}, "values": {"points_received": 1}}, "b5ca6839-5cda-4330-b7aa-fccc50ce1320": {"name": "edges", "tags": {"type": "stream", "task": "task_master:main", "parent": "write_points", "child": "stream", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost"}, "values": {"collected": 72, "emitted": 72}}, "e4f4dd29-96fe-41ca-9107-43030f67d47c": {"name": "edges", "tags": {"host": "localhost", "task": "deadman-test", "parent": "log3", "child": "mean4", "type": "batch", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff"}, "values": {"collected": 0, "emitted": 0}}, "f8bb9712-52ea-44c8-a0d9-8b6ee47b9cb8": {"name": "edges", "tags": {"task": "derivative-test", "parent": "sum5", "child": "log6", "type": "stream", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost"}, "values": {"collected": 0, "emitted": 0}}, "340391e7-8fc7-447d-ad07-547c5dd51f2c": {"tags": {"child": "from1", "type": "stream", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost", "task": "sys-stats", "parent": "stream0"}, "values": {"collected": 0, "emitted": 0}, "name": "edges"}, "2fd00546-3b38-4118-97e6-38489e5732a8": {"name": "edges", "tags": {"task": "sys-stats", "parent": "stream", "child": "stream0", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost", "type": "stream"}, "values": {"collected": 0, "emitted": 0}}, "5467712d-b3b9-45c5-acae-43a686c345e3": {"name": "edges", "tags": {"host": "localhost", "task": "test", "parent": "stream", "child": "stream0", "type": "stream", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff"}, "values": {"collected": 0, "emitted": 0}}, "2ce99d1d-d26f-4daa-8857-36568a9effc1": {"name": "nodes", "tags": {"task": "deadman-test", "node": "window2", "type": "stream", "kind": "window", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost"}, "values": {"avg_exec_time_ns": "0s"}}, "975e59b2-d0ab-4ebf-9054-4de9751e0e87": {"name": "edges", "tags": {"task": "deadman-test", "parent": "derivative5", "child": "window6", "type": "stream", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost"}, "values": {"collected": 0, "emitted": 0}}, "14fc028b-bda9-4b29-bf8e-8f682dd42bab": {"name": "nodes", "tags": {"task": "derivative-test", "node": "from1", "type": "stream", "kind": "from", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost"}, "values": {"avg_exec_time_ns": "0s"}}, "69f374fb-1289-457a-9ae6-79263e2dc430": {"tags": {"child": "derivative2", "type": "stream", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost", "task": "derivative-test", "parent": "from1"}, "values": {"collected": 0, "emitted": 0}, "name": "edges"}, "0c0a0463-4588-494b-b871-f20dea1b4c71": {"name": "nodes", "tags": {"task": "derivative-test", "node": "log6", "type": "stream", "kind": "log", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f18-4201-8d47-db603bfceeff", "host": "localhost"}, "values": {"avg_exec_time_ns": "0s"}}, "e6eedf33-a07c-4daf-87fb-9429ee38847f": {"name": "nodes", "tags": {"host": "localhost", "kind": "from", "task": "deadman-test", "node": "from1", "type": "stream", "cluster_id": "aaa1cb78-8277-4886-a8ea-4706bca20842", "server_id": "81e38258-5f
"memstats": {"Alloc":6950624,"TotalAlloc":13475176,"Sys":14625016,"Lookups":40,"Mallocs":157726,"Frees":129656,"HeapAlloc":6950624,"HeapSys":9666560,"HeapIdle":499712,"HeapInuse":9166848,"HeapReleased":0,"HeapObjects":28070,"StackInuse":819200,"StackSys":819200,"MSpanInuse":105600,"MSpanSys":114688,"MCacheInuse":9600,"MCacheSys":16384,"BuckHashSys":1446737,"GCSys":575488,"OtherSys":1985959,"NextGC":10996691,"LastGC":1478813691405406556,"PauseTotalNs":767327,"PauseNs":[106224,484338,77201,99564,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"PauseEnd":[1478813691247788813,1478813691256256020,1478813691262214751,1478813691405406556,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"NumGC":4,"GCCPUFraction":0.006757149597237818,"EnableGC":true,"DebugGC":false,"BySize":[{"Size":0,"Mallocs":0,"Frees":0},{"Size":8,"Mallocs":16193,"Frees":15733},{"Size":16,"Mallocs":54267,"Frees":45512},{"Size":32,"Mallocs":35006,"Frees":25861},{"Size":48,"Mallocs":7130,"Frees":5066},{"Size":64,"Mallocs":3421,"Frees":2554},{"Size":80,"Mallocs":3749,"Frees":3225},{"Size":96,"Mallocs":13973,"Frees":13413},{"Size":112,"Mallocs":1269,"Frees":825},{"Size":128,"Mallocs":1851,"Frees":1755},{"Size":144,"Mallocs":181,"Frees":95},{"Size":160,"Mallocs":558,"Frees":184},{"Size":176,"Mallocs":303,"Frees":249},{"Size":192,"Mallocs":2485,"Frees":278},{"Size":208,"Mallocs":255,"Frees":124},{"Size":224,"Mallocs":316,"Frees":131},{"Size":240,"Mallocs":64,"Frees":28},{"Size":256,"Mallocs":128,"Frees":91},{"Size":288,"Mallocs":734,"Frees":194},{"Size":320,"Mallocs":257,"Frees":15},{"Size":352,"Mallocs":531,"Frees":284},{"Size":384,"Mallocs":63,"Frees":7},{"Size":416,"Mallocs":189,"Frees":66},{"Size":448,"Mallocs":60,"Frees":11},{"Size":480,"Mallocs":19,"Frees":6},{"Size":512,"Mallocs":72,"Frees":56},{"Size":576,"Mallocs":157,"Frees":61},{"Size":640,"Mallocs":27,"Frees":14},{"Size":704,"Mallocs":159,"Frees":146},{"Size":768,"Mallocs":182,"Frees":179},{"Size":896,"Mallocs":82,"Frees":43},{"Size":1024,"Mallocs":108,"Frees":39},{"Size":1152,"Mallocs":245,"Frees":27},{"Size":1280,"Mallocs":49,"Frees":32},{"Size":1408,"Mallocs":70,"Frees":37},{"Size":1536,"Mallocs":61,"Frees":31},{"Size":1664,"Mallocs":29,"Frees":17},{"Size":2048,"Mallocs":102,"Frees":74},{"Size":2304,"Mallocs":27,"Frees":15},{"Size":2560,"Mallocs":17,"Frees":14},{"Size":2816,"Mallocs":92,"Frees":90},{"Size":3072,"Mallocs":4,"Frees":4},{"Size":3328,"Mallocs":20,"Frees":12},{"Size":4096,"Mallocs":85,"Frees":63},{"Size":4608,"Mallocs":25,"Frees":10},{"Size":5376,"Mallocs":50,"Frees":7},{"Size":6144,"Mallocs":13,"Frees":2},{"Size":6400,"Mallocs":0,"Frees":0},{"Size":6656,"Mallocs":2,"Frees":0},{"Size":6912,"Mallocs":0,"Frees":0},{"Size":8192,"Mallocs":31,"Frees":1},{"Size":8448,"Mallocs":0,"Frees":0},{"Size":8704,"Mallocs":1,"Frees":1},{"Size":9472,"Mallocs":0,"Frees":0},{"Size":10496,"Mallocs":0,"Frees":0},{"Size":12288,"Mallocs":5,"Frees":1},{"Size":13568,"Mallocs":0,"Frees":0},{"Size":14080,"Mallocs":0,"Frees":0},{"Size":16384,"Mallocs":4,"Frees":0},{"Size":16640,"Mallocs":0,"Frees":0},{"Size":17664,"Mallocs":0,"Frees":0}]},
"num_enabled_tasks": 5,
"num_subscriptions": 6,
"num_tasks": 5,
"product": "kapacitor",
"server_id": "81e38258-5f18-4201-8d47-db603bfceeff",
"version": "1.1.0~rc2"
}
`