// Test that the proper values are ignored or collected for class=Java func TestHttpJsonJavaMultiValue(t *testing.T) { cassandra := genJolokiaClientStub(validJavaMultiValueJSON, 200, MultipleServers, []string{HeapMetric}) var acc testutil.Accumulator acc.SetDebug(true) err := cassandra.Gather(&acc) assert.Nil(t, err) assert.Equal(t, 2, len(acc.Metrics)) fields := map[string]interface{}{ "HeapMemoryUsage_init": 67108864.0, "HeapMemoryUsage_committed": 456130560.0, "HeapMemoryUsage_max": 477626368.0, "HeapMemoryUsage_used": 203288528.0, } tags1 := map[string]string{ "cassandra_host": "10.10.10.10", "mname": "HeapMemoryUsage", } tags2 := map[string]string{ "cassandra_host": "10.10.10.11", "mname": "HeapMemoryUsage", } acc.AssertContainsTaggedFields(t, "javaMemory", fields, tags1) acc.AssertContainsTaggedFields(t, "javaMemory", fields, tags2) }
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 }{ {"prometheus_go_gc_duration_seconds_count", 7, map[string]string{}}, {"prometheus_go_goroutines", 15, map[string]string{}}, } for _, e := range expected { assert.True(t, acc.HasFloatField(e.name, "value")) } }
func TestExec(t *testing.T) { parser, _ := parsers.NewJSONParser("exec", []string{}, nil) e := &Exec{ runner: newRunnerMock([]byte(validJson), nil), Commands: []string{"testcommand arg1"}, parser: parser, } var acc testutil.Accumulator err := e.Gather(&acc) require.NoError(t, err) assert.Equal(t, acc.NFields(), 8, "non-numeric measurements should be ignored") fields := map[string]interface{}{ "num_processes": float64(82), "cpu_used": float64(8234), "cpu_free": float64(32), "percent": float64(0.81), "users_0": float64(0), "users_1": float64(1), "users_2": float64(2), "users_3": float64(3), } acc.AssertContainsFields(t, "exec", fields) }
func TestMetricContainsServerAndDomainAndRecordTypeTags(t *testing.T) { if testing.Short() { t.Skip("Skipping network-dependent test in short mode.") } var dnsConfig = DnsQuery{ Servers: servers, Domains: domains, } var acc testutil.Accumulator tags := map[string]string{ "server": "8.8.8.8", "domain": "google.com", "record_type": "NS", } fields := map[string]interface{}{} err := dnsConfig.Gather(&acc) assert.NoError(t, err) metric, ok := acc.Get("dns_query") require.True(t, ok) queryTime, _ := metric.Fields["query_time_ms"].(float64) fields["query_time_ms"] = queryTime acc.AssertContainsTaggedFields(t, "dns_query", fields, tags) }
func TestSNMPEasyGet6(t *testing.T) { h := Host{ Address: "127.0.0.1:31161", Community: "telegraf", Version: 2, Timeout: 2.0, Retries: 2, GetOids: []string{"1.3.6.1.2.1.2.1.0"}, } s := Snmp{ SnmptranslateFile: "./testdata/oids.txt", Host: []Host{h}, } var acc testutil.Accumulator err := s.Gather(&acc) require.NoError(t, err) acc.AssertContainsTaggedFields(t, "ifNumber", map[string]interface{}{ "ifNumber": int(4), }, map[string]string{ "instance": "0", "host": "127.0.0.1", }, ) }
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.HasIntField("memcached", metric), metric) } }
func TestTailFromEnd(t *testing.T) { tmpfile, err := ioutil.TempFile("", "") require.NoError(t, err) defer os.Remove(tmpfile.Name()) _, err = tmpfile.WriteString("cpu,mytag=foo usage_idle=100\n") require.NoError(t, err) tt := NewTail() tt.Files = []string{tmpfile.Name()} p, _ := parsers.NewInfluxParser() tt.SetParser(p) defer tt.Stop() defer tmpfile.Close() acc := testutil.Accumulator{} require.NoError(t, tt.Start(&acc)) time.Sleep(time.Millisecond * 100) _, err = tmpfile.WriteString("cpu,othertag=foo usage_idle=100\n") require.NoError(t, err) require.NoError(t, tt.Gather(&acc)) time.Sleep(time.Millisecond * 50) acc.AssertContainsTaggedFields(t, "cpu", map[string]interface{}{ "usage_idle": float64(100), }, map[string]string{ "othertag": "foo", }) assert.Len(t, acc.Metrics, 1) }
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) }
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) } }
// Test that test_a.log line gets parsed even though we don't have the correct // pattern available for test_b.log func TestGrokParseLogFilesOneBad(t *testing.T) { thisdir := getCurrentDir() p := &grok.Parser{ Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_BAD}"}, CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"}, } assert.NoError(t, p.Compile()) logparser := &LogParserPlugin{ FromBeginning: true, Files: []string{thisdir + "grok/testdata/test_a.log"}, GrokParser: p, } acc := testutil.Accumulator{} acc.SetDebug(true) assert.NoError(t, logparser.Start(&acc)) time.Sleep(time.Millisecond * 500) logparser.Stop() acc.AssertContainsTaggedFields(t, "logparser_grok", map[string]interface{}{ "clientip": "192.168.1.1", "myfloat": float64(1.25), "response_time": int64(5432), "myint": int64(101), }, map[string]string{"response_code": "200"}) }
func TestSNMPGet2(t *testing.T) { get1 := Data{ Name: "oid1", Oid: "ifNumber", } h := Host{ Address: testutil.GetLocalHost() + ":31161", Community: "telegraf", Version: 2, Timeout: 2.0, Retries: 2, Collect: []string{"oid1"}, } s := Snmp{ SnmptranslateFile: "./testdata/oids.txt", Host: []Host{h}, Get: []Data{get1}, } var acc testutil.Accumulator err := s.Gather(&acc) require.NoError(t, err) acc.AssertContainsTaggedFields(t, "ifNumber", map[string]interface{}{ "ifNumber": int(4), }, map[string]string{ "instance": "0", "host": testutil.GetLocalHost(), }, ) }
func TestGatherGlob(t *testing.T) { dir := getTestdataDir() fs := NewFileStat() fs.Md5 = true fs.Files = []string{ dir + "*.log", } acc := testutil.Accumulator{} fs.Gather(&acc) tags1 := map[string]string{ "file": dir + "log1.log", } fields1 := map[string]interface{}{ "size_bytes": int64(0), "exists": int64(1), "md5_sum": "d41d8cd98f00b204e9800998ecf8427e", } acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1) tags2 := map[string]string{ "file": dir + "log2.log", } fields2 := map[string]interface{}{ "size_bytes": int64(0), "exists": int64(1), "md5_sum": "d41d8cd98f00b204e9800998ecf8427e", } acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2) }
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.HasIntField("rethinkdb", metric)) } }
// Test that the proper values are ignored or collected for class=Cassandra func TestHttpJsonCassandraMultiValue(t *testing.T) { cassandra := genJolokiaClientStub(validCassandraMultiValueJSON, 200, Servers, []string{ReadLatencyMetric}) var acc testutil.Accumulator err := cassandra.Gather(&acc) assert.Nil(t, err) assert.Equal(t, 1, len(acc.Metrics)) fields := map[string]interface{}{ "ReadLatency_999thPercentile": 20.0, "ReadLatency_99thPercentile": 10.0, "ReadLatency_Count": 400.0, "ReadLatency_DurationUnit": "microseconds", "ReadLatency_Max": 30.0, "ReadLatency_MeanRate": 3.0, "ReadLatency_Min": 1.0, "ReadLatency_RateUnit": "events/second", } tags := map[string]string{ "cassandra_host": "10.10.10.10", "mname": "ReadLatency", "keyspace": "test_keyspace1", "scope": "test_table", } acc.AssertContainsTaggedFields(t, "cassandraTable", fields, tags) }
func TestRunParser(t *testing.T) { log.SetOutput(ioutil.Discard) var testmsg = []byte("cpu_load_short,host=server01 value=12.0 1422568543702900257") listener, in := newTestUdpListener() acc := testutil.Accumulator{} listener.acc = &acc defer close(listener.done) listener.parser, _ = parsers.NewInfluxParser() listener.wg.Add(1) go listener.udpParser() in <- testmsg time.Sleep(time.Millisecond * 25) listener.Gather(&acc) if a := acc.NFields(); a != 1 { t.Errorf("got %v, expected %v", a, 1) } acc.AssertContainsTaggedFields(t, "cpu_load_short", map[string]interface{}{"value": float64(12)}, map[string]string{"host": "server01"}, ) }
func TestMesosSlave(t *testing.T) { var acc testutil.Accumulator m := Mesos{ Masters: []string{}, Slaves: []string{slaveTestServer.Listener.Addr().String()}, // SlaveTasks: true, Timeout: 10, } err := m.Gather(&acc) if err != nil { t.Errorf(err.Error()) } acc.AssertContainsFields(t, "mesos", slaveMetrics) // expectedFields := make(map[string]interface{}, len(slaveTaskMetrics["statistics"].(map[string]interface{}))+1) // for k, v := range slaveTaskMetrics["statistics"].(map[string]interface{}) { // expectedFields[k] = v // } // expectedFields["executor_id"] = slaveTaskMetrics["executor_id"] // acc.AssertContainsTaggedFields( // t, // "mesos_tasks", // expectedFields, // map[string]string{"server": "127.0.0.1", "framework_id": slaveTaskMetrics["framework_id"].(string)}) }
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, Passes: 0, DeletedDocuments: 0, }, tags, ) var acc testutil.Accumulator d.AddDefaultStats() d.flush(&acc) for key, _ := range DefaultStats { assert.True(t, acc.HasIntField("mongodb", key)) } }
func TestMissingDelayColumnNTPQ(t *testing.T) { resetVars() tt := tester{ ret: []byte(missingDelayNTPQ), err: nil, } n := &NTPQ{ runQ: tt.runqTest, } acc := testutil.Accumulator{} assert.NoError(t, n.Gather(&acc)) fields := map[string]interface{}{ "when": int64(101), "poll": int64(256), "reach": int64(37), "offset": float64(233.010), "jitter": float64(17.462), } tags := map[string]string{ "remote": "*uschi5-ntp-002.", "refid": "10.177.80.46", "type": "u", } acc.AssertContainsTaggedFields(t, "ntpq", fields, tags) }
func TestTailFromBeginning(t *testing.T) { tmpfile, err := ioutil.TempFile("", "") require.NoError(t, err) defer os.Remove(tmpfile.Name()) tt := NewTail() tt.FromBeginning = true tt.Files = []string{tmpfile.Name()} p, _ := parsers.NewInfluxParser() tt.SetParser(p) defer tt.Stop() defer tmpfile.Close() acc := testutil.Accumulator{} require.NoError(t, tt.Start(&acc)) _, err = tmpfile.WriteString("cpu,mytag=foo usage_idle=100\n") require.NoError(t, err) require.NoError(t, tt.Gather(&acc)) // arbitrary sleep to wait for message to show up time.Sleep(time.Millisecond * 250) acc.AssertContainsTaggedFields(t, "cpu", map[string]interface{}{ "usage_idle": float64(100), }, map[string]string{ "mytag": "foo", }) }
func TestRaindropsGeneratesMetrics(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var rsp string if r.URL.Path == "/_raindrops" { rsp = sampleResponse } else { panic("Cannot handle request") } fmt.Fprintln(w, rsp) })) defer ts.Close() n := &Raindrops{ Urls: []string{fmt.Sprintf("%s/_raindrops", ts.URL)}, http_client: &http.Client{Transport: &http.Transport{ ResponseHeaderTimeout: time.Duration(3 * time.Second), }}, } var acc testutil.Accumulator err := n.Gather(&acc) require.NoError(t, err) fields := map[string]interface{}{ "calling": uint64(100), "writing": uint64(200), } 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, "raindrops", fields, tags) tags = map[string]string{ "port": "8081", "ip": "0.0.0.0", } fields = map[string]interface{}{ "active": uint64(3), "queued": uint64(4), } acc.AssertContainsTaggedFields(t, "raindrops_listen", fields, tags) }
func TestZookeeperGeneratesMetrics(t *testing.T) { if testing.Short() { t.Skip("Skipping integration test in short mode") } z := &Zookeeper{ Servers: []string{testutil.GetLocalHost() + ":2181"}, } 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.HasIntField("zookeeper", metric), metric) } }
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)) } }
func TestMesosSlave(t *testing.T) { var acc testutil.Accumulator m := Mesos{ Masters: []string{}, Slaves: []string{slaveTestServer.Listener.Addr().String()}, SlaveTasks: true, Timeout: 10, } err := m.Gather(&acc) if err != nil { t.Errorf(err.Error()) } acc.AssertContainsFields(t, "mesos", slaveMetrics) jf := jsonparser.JSONFlattener{} err = jf.FlattenJSON("", slaveTaskMetrics) if err != nil { t.Errorf(err.Error()) } acc.AssertContainsFields(t, "mesos-tasks", jf.Fields) }
func TestBadIntNTPQ(t *testing.T) { tt := tester{ ret: []byte(badIntParseNTPQ), err: nil, } n := &NTPQ{ runQ: tt.runqTest, } acc := testutil.Accumulator{} assert.NoError(t, n.Gather(&acc)) fields := map[string]interface{}{ "when": int64(101), "reach": int64(37), "delay": float64(51.016), "offset": float64(233.010), "jitter": float64(17.462), } tags := map[string]string{ "remote": "uschi5-ntp-002.", "state_prefix": "*", "refid": "10.177.80.46", "stratum": "2", "type": "u", } acc.AssertContainsTaggedFields(t, "ntpq", fields, tags) }
func TestSNMPGet1(t *testing.T) { get1 := Data{ Name: "oid1", Unit: "octets", Oid: ".1.3.6.1.2.1.2.2.1.16.1", } h := Host{ Address: "127.0.0.1:31161", Community: "telegraf", Version: 2, Timeout: 2.0, Retries: 2, Collect: []string{"oid1"}, } s := Snmp{ Host: []Host{h}, Get: []Data{get1}, } var acc testutil.Accumulator err := s.Gather(&acc) require.NoError(t, err) acc.AssertContainsTaggedFields(t, "oid1", map[string]interface{}{ "oid1": uint(543846), }, map[string]string{ "unit": "octets", "host": "127.0.0.1", }, ) }
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.HasIntField("aerospike", metric), metric) } }
func TestLineProtocolParseMultiple(t *testing.T) { parser, _ := parsers.NewInfluxParser() e := &Exec{ runner: newRunnerMock([]byte(lineProtocolMulti), nil), Commands: []string{"line-protocol"}, parser: parser, } var acc testutil.Accumulator err := e.Gather(&acc) require.NoError(t, err) fields := map[string]interface{}{ "usage_idle": float64(99), "usage_busy": float64(1), } tags := map[string]string{ "host": "foo", "datacenter": "us-east", } cpuTags := []string{"cpu0", "cpu1", "cpu2", "cpu3", "cpu4", "cpu5", "cpu6"} for _, cpu := range cpuTags { tags["cpu"] = cpu acc.AssertContainsTaggedFields(t, "cpu", fields, tags) } }
func TestGather(t *testing.T) { duration, _ := time.ParseDuration("1m") internalDuration := internal.Duration{ Duration: duration, } c := &CloudWatch{ Region: "us-east-1", Namespace: "AWS/ELB", Delay: internalDuration, Period: internalDuration, RateLimit: 10, } var acc testutil.Accumulator c.client = &mockCloudWatchClient{} c.Gather(&acc) fields := map[string]interface{}{} fields["latency_minimum"] = 0.1 fields["latency_maximum"] = 0.3 fields["latency_average"] = 0.2 fields["latency_sum"] = 123.0 fields["latency_sample_count"] = 100.0 tags := map[string]string{} tags["unit"] = "seconds" tags["region"] = "us-east-1" tags["load_balancer_name"] = "p-example" assert.True(t, acc.HasMeasurement("cloudwatch_aws_elb")) acc.AssertContainsTaggedFields(t, "cloudwatch_aws_elb", fields, tags) }
func TestRunParser(t *testing.T) { var testmsg = []byte(testMsg) listener, in := newTestTcpListener() acc := testutil.Accumulator{} listener.acc = &acc defer close(listener.done) listener.parser, _ = parsers.NewInfluxParser() listener.wg.Add(1) go listener.tcpParser() in <- testmsg time.Sleep(time.Millisecond * 25) listener.Gather(&acc) if a := acc.NFields(); a != 1 { t.Errorf("got %v, expected %v", a, 1) } acc.AssertContainsTaggedFields(t, "cpu_load_short", map[string]interface{}{"value": float64(12)}, map[string]string{"host": "server01"}, ) }
func TestPartialVmStatProcFile(t *testing.T) { tmpfile := makeFakeStatFile([]byte(vmStatFile_Partial)) defer os.Remove(tmpfile) k := KernelVmstat{ statFile: tmpfile, } acc := testutil.Accumulator{} err := k.Gather(&acc) assert.NoError(t, err) fields := map[string]interface{}{ "unevictable_pgs_culled": int64(1531), "unevictable_pgs_scanned": int64(0), "unevictable_pgs_rescued": int64(5426), "unevictable_pgs_mlocked": int64(6988), "unevictable_pgs_munlocked": int64(6988), "unevictable_pgs_cleared": int64(0), "unevictable_pgs_stranded": int64(0), "unevictable_pgs_mlockfreed": int64(0), "thp_fault_alloc": int64(346219), "thp_fault_fallback": int64(895453), "thp_collapse_alloc": int64(24857), "thp_collapse_alloc_failed": int64(102214), "thp_split": int64(9817), } acc.AssertContainsFields(t, "kernel_vmstat", fields) }