Resolve gopsutil & unit test issues with net proto stats

This commit is contained in:
Cameron Sparr 2015-12-04 15:09:07 -07:00
parent 0d0a8e9b68
commit c83f220fc4
4 changed files with 43 additions and 9 deletions

View File

@ -7,6 +7,7 @@
- [#415](https://github.com/influxdb/telegraf/issues/415): memcached plugin: support unix sockets - [#415](https://github.com/influxdb/telegraf/issues/415): memcached plugin: support unix sockets
- [#418](https://github.com/influxdb/telegraf/pull/418): memcached plugin additional unit tests. - [#418](https://github.com/influxdb/telegraf/pull/418): memcached plugin additional unit tests.
- [#408](https://github.com/influxdb/telegraf/pull/408): MailChimp plugin. - [#408](https://github.com/influxdb/telegraf/pull/408): MailChimp plugin.
- [#382](https://github.com/influxdb/telegraf/pull/382): Add system wide network protocol stats to `net` plugin.
### Bugfixes ### Bugfixes
- [#405](https://github.com/influxdb/telegraf/issues/405): Prometheus output cardinality issue - [#405](https://github.com/influxdb/telegraf/issues/405): Prometheus output cardinality issue
@ -83,7 +84,6 @@ same type.
- [#364](https://github.com/influxdb/telegraf/pull/364): Support InfluxDB UDP output. - [#364](https://github.com/influxdb/telegraf/pull/364): Support InfluxDB UDP output.
- [#370](https://github.com/influxdb/telegraf/pull/370): Support specifying multiple outputs, as lists. - [#370](https://github.com/influxdb/telegraf/pull/370): Support specifying multiple outputs, as lists.
- [#372](https://github.com/influxdb/telegraf/pull/372): Remove gosigar and update go-dockerclient for FreeBSD support. Thanks @MerlinDMC! - [#372](https://github.com/influxdb/telegraf/pull/372): Remove gosigar and update go-dockerclient for FreeBSD support. Thanks @MerlinDMC!
- [#382](https://github.com/influxdb/telegraf/pull/382): Add system wide network protocol stats to `net` plugin.
### Bugfixes ### Bugfixes
- [#331](https://github.com/influxdb/telegraf/pull/331): Dont overwrite host tag in redis plugin. - [#331](https://github.com/influxdb/telegraf/pull/331): Dont overwrite host tag in redis plugin.

View File

@ -1,13 +1,15 @@
package system package system
import "github.com/stretchr/testify/mock" import (
"github.com/stretchr/testify/mock"
import "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
import "github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/disk"
import "github.com/shirou/gopsutil/load" "github.com/shirou/gopsutil/load"
import "github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/mem"
import "github.com/shirou/gopsutil/net" "github.com/shirou/gopsutil/net"
)
type MockPS struct { type MockPS struct {
mock.Mock mock.Mock
@ -21,6 +23,7 @@ func (m *MockPS) LoadAvg() (*load.LoadAvgStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) { func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
ret := m.Called() ret := m.Called()
@ -29,6 +32,7 @@ func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) { func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
ret := m.Called() ret := m.Called()
@ -37,6 +41,7 @@ func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) { func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {
ret := m.Called() ret := m.Called()
@ -45,6 +50,16 @@ func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) NetProto() ([]net.NetProtoCountersStat, error) {
ret := m.Called()
r0 := ret.Get(0).([]net.NetProtoCountersStat)
r1 := ret.Error(1)
return r0, r1
}
func (m *MockPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) { func (m *MockPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) {
ret := m.Called() ret := m.Called()
@ -53,6 +68,7 @@ func (m *MockPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) { func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) {
ret := m.Called() ret := m.Called()
@ -61,6 +77,7 @@ func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) { func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {
ret := m.Called() ret := m.Called()
@ -69,6 +86,7 @@ func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) { func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) {
ret := m.Called() ret := m.Called()
@ -77,6 +95,7 @@ func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) NetConnections() ([]net.NetConnectionStat, error) { func (m *MockPS) NetConnections() ([]net.NetConnectionStat, error) {
ret := m.Called() ret := m.Called()

View File

@ -81,10 +81,12 @@ func (s *NetIOStats) Gather(acc plugins.Accumulator) error {
} }
// Get system wide stats for different network protocols // Get system wide stats for different network protocols
netprotos, err := s.ps.NetProto() // (ignore these stats if the call fails)
netprotos, _ := s.ps.NetProto()
for _, proto := range netprotos { for _, proto := range netprotos {
for stat, value := range proto.Stats { for stat, value := range proto.Stats {
name := fmt.Sprintf("%s_%s", proto.Protocol, strings.ToLower(stat)) name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol),
strings.ToLower(stat))
acc.Add(name, value, nil) acc.Add(name, value, nil)
} }
} }

View File

@ -113,6 +113,17 @@ func TestSystemStats_GenerateStats(t *testing.T) {
mps.On("NetIO").Return([]net.NetIOCountersStat{netio}, nil) 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{ vms := &mem.VirtualMemoryStat{
Total: 12400, Total: 12400,
Available: 7600, Available: 7600,
@ -273,6 +284,8 @@ func TestSystemStats_GenerateStats(t *testing.T) {
assert.NoError(t, acc.ValidateTaggedValue("err_out", uint64(8), 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_in", uint64(7), ntags))
assert.NoError(t, acc.ValidateTaggedValue("drop_out", uint64(1), 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) preDiskIOPoints := len(acc.Points)