Skip to content

Commit

Permalink
Resolve gopsutil & unit test issues with net proto stats
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Dec 4, 2015
1 parent 0d0a8e9 commit c83f220
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#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.
- [#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
- [#405](https://github.com/influxdb/telegraf/issues/405): Prometheus output cardinality issue
Expand Down Expand Up @@ -83,7 +84,6 @@ same type.
- [#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.
- [#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
- [#331](https://github.com/influxdb/telegraf/pull/331): Dont overwrite host tag in redis plugin.
Expand Down
31 changes: 25 additions & 6 deletions plugins/system/mock_PS.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package system

import "github.com/stretchr/testify/mock"
import (
"github.com/stretchr/testify/mock"

import "github.com/shirou/gopsutil/cpu"
import "github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"

import "github.com/shirou/gopsutil/load"
import "github.com/shirou/gopsutil/mem"
import "github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)

type MockPS struct {
mock.Mock
Expand All @@ -21,6 +23,7 @@ func (m *MockPS) LoadAvg() (*load.LoadAvgStat, error) {

return r0, r1
}

func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
ret := m.Called()

Expand All @@ -29,6 +32,7 @@ func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {

return r0, r1
}

func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
ret := m.Called()

Expand All @@ -37,6 +41,7 @@ func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {

return r0, r1
}

func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {
ret := m.Called()

Expand All @@ -45,6 +50,16 @@ func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {

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) {
ret := m.Called()

Expand All @@ -53,6 +68,7 @@ func (m *MockPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) {

return r0, r1
}

func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) {
ret := m.Called()

Expand All @@ -61,6 +77,7 @@ func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) {

return r0, r1
}

func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {
ret := m.Called()

Expand All @@ -69,6 +86,7 @@ func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {

return r0, r1
}

func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) {
ret := m.Called()

Expand All @@ -77,6 +95,7 @@ func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) {

return r0, r1
}

func (m *MockPS) NetConnections() ([]net.NetConnectionStat, error) {
ret := m.Called()

Expand Down
6 changes: 4 additions & 2 deletions plugins/system/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ func (s *NetIOStats) Gather(acc plugins.Accumulator) error {
}

// 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 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)
}
}
Expand Down
13 changes: 13 additions & 0 deletions plugins/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ func TestSystemStats_GenerateStats(t *testing.T) {

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,
Expand Down Expand Up @@ -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("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)

Expand Down

0 comments on commit c83f220

Please sign in to comment.