示例#1
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))
	}
}
示例#2
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))
	}
}
示例#3
0
func TestRedisGeneratesMetrics(t *testing.T) {
	l, err := net.Listen("tcp", ":0")
	require.NoError(t, err)

	defer l.Close()

	go func() {
		c, err := l.Accept()
		if err != nil {
			return
		}

		buf := bufio.NewReader(c)

		for {
			line, err := buf.ReadString('\n')
			if err != nil {
				return
			}

			if line != "info\r\n" {
				return
			}

			fmt.Fprintf(c, "$%d\n", len(testOutput))
			c.Write([]byte(testOutput))
		}
	}()

	addr := fmt.Sprintf("redis://%s", l.Addr().String())

	r := &Redis{
		Servers: []string{addr},
	}

	var acc testutil.Accumulator

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

	checkInt := []struct {
		name  string
		value uint64
	}{
		{"uptime", 238},
		{"clients", 1},
		{"used_memory", 1003936},
		{"used_memory_rss", 811008},
		{"used_memory_peak", 1003936},
		{"used_memory_lua", 33792},
		{"rdb_changes_since_last_save", 0},
		{"total_connections_received", 2},
		{"total_commands_processed", 1},
		{"instantaneous_ops_per_sec", 0},
		{"sync_full", 0},
		{"sync_partial_ok", 0},
		{"sync_partial_err", 0},
		{"expired_keys", 0},
		{"evicted_keys", 0},
		{"keyspace_hits", 0},
		{"keyspace_misses", 0},
		{"pubsub_channels", 0},
		{"pubsub_patterns", 0},
		{"latest_fork_usec", 0},
		{"connected_slaves", 0},
		{"master_repl_offset", 0},
		{"repl_backlog_active", 0},
		{"repl_backlog_size", 1048576},
		{"repl_backlog_histlen", 0},
	}

	for _, c := range checkInt {
		assert.NoError(t, acc.ValidateValue(c.name, c.value))
	}

	checkFloat := []struct {
		name  string
		value float64
	}{
		{"mem_fragmentation_ratio", 0.81},
		{"used_cpu_sys", 0.14},
		{"used_cpu_user", 0.05},
		{"used_cpu_sys_children", 0.00},
		{"used_cpu_user_children", 0.00},
	}

	for _, c := range checkFloat {
		assert.NoError(t, acc.ValidateValue(c.name, c.value))
	}
}
示例#4
0
func TestSystemStats_GenerateStats(t *testing.T) {
	var mps MockPS

	defer mps.AssertExpectations(t)

	var acc testutil.Accumulator

	cts := cpu.CPUTimesStat{
		CPU:       "cpu0",
		User:      3.1,
		System:    8.2,
		Idle:      80.1,
		Nice:      1.3,
		Iowait:    0.2,
		Irq:       0.1,
		Softirq:   0.11,
		Steal:     0.0511,
		Guest:     8.1,
		GuestNice: 0.324,
	}

	cts2 := cpu.CPUTimesStat{
		CPU:       "cpu0",
		User:      11.4,     // increased by 8.3
		System:    10.9,     // increased by 2.7
		Idle:      158.8699, // increased by 78.7699 (for total increase of 100)
		Nice:      2.5,      // increased by 1.2
		Iowait:    0.7,      // increased by 0.5
		Irq:       1.2,      // increased by 1.1
		Softirq:   0.31,     // increased by 0.2
		Steal:     0.2812,   // increased by 0.0001
		Guest:     12.9,     // increased by 4.8
		GuestNice: 2.524,    // increased by 2.2
	}

	mps.On("CPUTimes").Return([]cpu.CPUTimesStat{cts}, nil)

	du := []*disk.DiskUsageStat{
		{
			Path:        "/",
			Fstype:      "ext4",
			Total:       128,
			Free:        23,
			InodesTotal: 1234,
			InodesFree:  234,
		},
		{
			Path:        "/home",
			Fstype:      "ext4",
			Total:       256,
			Free:        46,
			InodesTotal: 2468,
			InodesFree:  468,
		},
	}

	mps.On("DiskUsage").Return(du, nil)

	diskio1 := disk.DiskIOCountersStat{

		ReadCount:    888,
		WriteCount:   5341,
		ReadBytes:    100000,
		WriteBytes:   200000,
		ReadTime:     7123,
		WriteTime:    9087,
		Name:         "sda1",
		IoTime:       123552,
		SerialNumber: "ab-123-ad",
	}
	diskio2 := disk.DiskIOCountersStat{
		ReadCount:    444,
		WriteCount:   2341,
		ReadBytes:    200000,
		WriteBytes:   400000,
		ReadTime:     3123,
		WriteTime:    6087,
		Name:         "sdb1",
		IoTime:       246552,
		SerialNumber: "bb-123-ad",
	}

	mps.On("DiskIO").Return(map[string]disk.DiskIOCountersStat{"sda1": diskio1, "sdb1": diskio2}, nil)

	netio := net.NetIOCountersStat{
		Name:        "eth0",
		BytesSent:   1123,
		BytesRecv:   8734422,
		PacketsSent: 781,
		PacketsRecv: 23456,
		Errin:       832,
		Errout:      8,
		Dropin:      7,
		Dropout:     1,
	}

	mps.On("NetIO").Return([]net.NetIOCountersStat{netio}, nil)

	netprotos := []net.NetProtoCountersStat{
		net.NetProtoCountersStat{
			Protocol: "Udp",
			Stats: map[string]int64{
				"InDatagrams": 4655,
				"NoPorts":     892592,
			},
		},
	}
	mps.On("NetProto").Return(netprotos, nil)

	vms := &mem.VirtualMemoryStat{
		Total:     12400,
		Available: 7600,
		Used:      5000,
		Free:      1235,
		// Active:      8134,
		// Inactive:    1124,
		// Buffers:     771,
		// Cached:      4312,
		// Wired:       134,
		// Shared:      2142,
	}

	mps.On("VMStat").Return(vms, nil)

	sms := &mem.SwapMemoryStat{
		Total:       8123,
		Used:        1232,
		Free:        6412,
		UsedPercent: 12.2,
		Sin:         7,
		Sout:        830,
	}

	mps.On("SwapStat").Return(sms, nil)

	netstats := []net.NetConnectionStat{
		net.NetConnectionStat{
			Type: syscall.SOCK_DGRAM,
		},
		net.NetConnectionStat{
			Status: "ESTABLISHED",
		},
		net.NetConnectionStat{
			Status: "ESTABLISHED",
		},
		net.NetConnectionStat{
			Status: "CLOSE",
		},
	}

	mps.On("NetConnections").Return(netstats, nil)

	cs := NewCPUStats(&mps)

	cputags := map[string]string{
		"cpu": "cpu0",
	}

	preCPUPoints := len(acc.Points)
	err := cs.Gather(&acc)
	require.NoError(t, err)
	numCPUPoints := len(acc.Points) - preCPUPoints

	expectedCPUPoints := 10
	assert.Equal(t, expectedCPUPoints, numCPUPoints)

	// Computed values are checked with delta > 0 becasue of floating point arithmatic
	// imprecision
	assertContainsTaggedFloat(t, &acc, "time_user", 3.1, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_system", 8.2, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_idle", 80.1, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_nice", 1.3, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_iowait", 0.2, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_irq", 0.1, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_softirq", 0.11, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_steal", 0.0511, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_guest", 8.1, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_guest_nice", 0.324, 0, cputags)

	mps2 := MockPS{}
	mps2.On("CPUTimes").Return([]cpu.CPUTimesStat{cts2}, nil)
	cs.ps = &mps2

	// Should have added cpu percentages too
	err = cs.Gather(&acc)
	require.NoError(t, err)

	numCPUPoints = len(acc.Points) - (preCPUPoints + numCPUPoints)
	expectedCPUPoints = 20
	assert.Equal(t, expectedCPUPoints, numCPUPoints)

	assertContainsTaggedFloat(t, &acc, "time_user", 11.4, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_system", 10.9, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_idle", 158.8699, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_nice", 2.5, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_iowait", 0.7, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_irq", 1.2, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_softirq", 0.31, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_steal", 0.2812, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_guest", 12.9, 0, cputags)
	assertContainsTaggedFloat(t, &acc, "time_guest_nice", 2.524, 0, cputags)

	assertContainsTaggedFloat(t, &acc, "usage_user", 8.3, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_system", 2.7, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_idle", 78.7699, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_nice", 1.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_iowait", 0.5, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_irq", 1.1, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_softirq", 0.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_steal", 0.2301, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_guest", 4.8, 0.0005, cputags)
	assertContainsTaggedFloat(t, &acc, "usage_guest_nice", 2.2, 0.0005, cputags)

	preDiskPoints := len(acc.Points)

	err = (&DiskStats{ps: &mps}).Gather(&acc)
	require.NoError(t, err)

	numDiskPoints := len(acc.Points) - preDiskPoints
	expectedAllDiskPoints := 12
	assert.Equal(t, expectedAllDiskPoints, numDiskPoints)

	tags1 := map[string]string{
		"path":   "/",
		"fstype": "ext4",
	}
	tags2 := map[string]string{
		"path":   "/home",
		"fstype": "ext4",
	}

	assert.True(t, acc.CheckTaggedValue("total", uint64(128), tags1))
	assert.True(t, acc.CheckTaggedValue("used", uint64(105), tags1))
	assert.True(t, acc.CheckTaggedValue("free", uint64(23), tags1))
	assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(1234), tags1))
	assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(234), tags1))
	assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(1000), tags1))
	assert.True(t, acc.CheckTaggedValue("total", uint64(256), tags2))
	assert.True(t, acc.CheckTaggedValue("used", uint64(210), tags2))
	assert.True(t, acc.CheckTaggedValue("free", uint64(46), tags2))
	assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(2468), tags2))
	assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(468), tags2))
	assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(2000), tags2))

	// We expect 6 more DiskPoints to show up with an explicit match on "/"
	// and /home not matching the /dev in Mountpoints
	err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/dev"}}).Gather(&acc)
	assert.Equal(t, preDiskPoints+expectedAllDiskPoints+6, len(acc.Points))

	// We should see all the diskpoints as Mountpoints includes both
	// / and /home
	err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/home"}}).Gather(&acc)
	assert.Equal(t, preDiskPoints+2*expectedAllDiskPoints+6, len(acc.Points))

	err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc)
	require.NoError(t, err)

	ntags := map[string]string{
		"interface": "eth0",
	}

	assert.NoError(t, acc.ValidateTaggedValue("bytes_sent", uint64(1123), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("bytes_recv", uint64(8734422), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("packets_sent", uint64(781), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("packets_recv", uint64(23456), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("err_in", uint64(832), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("err_out", uint64(8), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("drop_in", uint64(7), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("drop_out", uint64(1), ntags))
	assert.NoError(t, acc.ValidateValue("udp_noports", int64(892592)))
	assert.NoError(t, acc.ValidateValue("udp_indatagrams", int64(4655)))

	preDiskIOPoints := len(acc.Points)

	err = (&DiskIOStats{ps: &mps}).Gather(&acc)
	require.NoError(t, err)

	numDiskIOPoints := len(acc.Points) - preDiskIOPoints
	expectedAllDiskIOPoints := 14
	assert.Equal(t, expectedAllDiskIOPoints, numDiskIOPoints)

	dtags1 := map[string]string{
		"name":   "sda1",
		"serial": "ab-123-ad",
	}
	dtags2 := map[string]string{
		"name":   "sdb1",
		"serial": "bb-123-ad",
	}

	assert.True(t, acc.CheckTaggedValue("reads", uint64(888), dtags1))
	assert.True(t, acc.CheckTaggedValue("writes", uint64(5341), dtags1))
	assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(100000), dtags1))
	assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(200000), dtags1))
	assert.True(t, acc.CheckTaggedValue("read_time", uint64(7123), dtags1))
	assert.True(t, acc.CheckTaggedValue("write_time", uint64(9087), dtags1))
	assert.True(t, acc.CheckTaggedValue("io_time", uint64(123552), dtags1))
	assert.True(t, acc.CheckTaggedValue("reads", uint64(444), dtags2))
	assert.True(t, acc.CheckTaggedValue("writes", uint64(2341), dtags2))
	assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(200000), dtags2))
	assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(400000), dtags2))
	assert.True(t, acc.CheckTaggedValue("read_time", uint64(3123), dtags2))
	assert.True(t, acc.CheckTaggedValue("write_time", uint64(6087), dtags2))
	assert.True(t, acc.CheckTaggedValue("io_time", uint64(246552), dtags2))

	// We expect 7 more DiskIOPoints to show up with an explicit match on "sdb1"
	// and serial should be missing from the tags with SkipSerialNumber set
	err = (&DiskIOStats{ps: &mps, Devices: []string{"sdb1"}, SkipSerialNumber: true}).Gather(&acc)
	assert.Equal(t, preDiskIOPoints+expectedAllDiskIOPoints+7, len(acc.Points))

	dtags3 := map[string]string{
		"name": "sdb1",
	}

	assert.True(t, acc.CheckTaggedValue("reads", uint64(444), dtags3))
	assert.True(t, acc.CheckTaggedValue("writes", uint64(2341), dtags3))
	assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(200000), dtags3))
	assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(400000), dtags3))
	assert.True(t, acc.CheckTaggedValue("read_time", uint64(3123), dtags3))
	assert.True(t, acc.CheckTaggedValue("write_time", uint64(6087), dtags3))
	assert.True(t, acc.CheckTaggedValue("io_time", uint64(246552), dtags3))

	err = (&MemStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	vmtags := map[string]string(nil)

	assert.True(t, acc.CheckTaggedValue("total", uint64(12400), vmtags))
	assert.True(t, acc.CheckTaggedValue("available", uint64(7600), vmtags))
	assert.True(t, acc.CheckTaggedValue("used", uint64(5000), vmtags))
	assert.True(t, acc.CheckTaggedValue("available_percent",
		float64(7600)/float64(12400)*100,
		vmtags))
	assert.True(t, acc.CheckTaggedValue("used_percent",
		float64(5000)/float64(12400)*100,
		vmtags))
	assert.True(t, acc.CheckTaggedValue("free", uint64(1235), vmtags))

	acc.Points = nil

	err = (&SwapStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	swaptags := map[string]string(nil)

	assert.NoError(t, acc.ValidateTaggedValue("total", uint64(8123), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("used", uint64(1232), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("used_percent", float64(12.2), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("free", uint64(6412), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("in", uint64(7), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("out", uint64(830), swaptags))

	acc.Points = nil

	err = (&NetStats{&mps}).Gather(&acc)
	require.NoError(t, err)
	netstattags := map[string]string(nil)

	assert.NoError(t, acc.ValidateTaggedValue("tcp_established", 2, netstattags))
	assert.NoError(t, acc.ValidateTaggedValue("tcp_close", 1, netstattags))
	assert.NoError(t, acc.ValidateTaggedValue("udp_socket", 1, netstattags))

}