Initial spike
This is mostly to solidify the overall structure with the agent, plugins, and testing philosphy.
This commit is contained in:
5
plugins/all/all.go
Normal file
5
plugins/all/all.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package all
|
||||
|
||||
import (
|
||||
_ "github.com/influxdb/tivan/plugins/system"
|
||||
)
|
||||
18
plugins/mock_Plugin.go
Normal file
18
plugins/mock_Plugin.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package plugins
|
||||
|
||||
import "github.com/stretchr/testify/mock"
|
||||
|
||||
import "github.com/vektra/cypress"
|
||||
|
||||
type MockPlugin struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockPlugin) Read() ([]*cypress.Message, error) {
|
||||
ret := m.Called()
|
||||
|
||||
r0 := ret.Get(0).([]*cypress.Message)
|
||||
r1 := ret.Error(1)
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
15
plugins/registry.go
Normal file
15
plugins/registry.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package plugins
|
||||
|
||||
import "github.com/vektra/cypress"
|
||||
|
||||
type Plugin interface {
|
||||
Read() ([]*cypress.Message, error)
|
||||
}
|
||||
|
||||
type Creator func() Plugin
|
||||
|
||||
var Plugins = map[string]Creator{}
|
||||
|
||||
func Add(name string, creator Creator) {
|
||||
Plugins[name] = creator
|
||||
}
|
||||
18
plugins/system/mock_PS.go
Normal file
18
plugins/system/mock_PS.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package system
|
||||
|
||||
import "github.com/stretchr/testify/mock"
|
||||
|
||||
import "github.com/shirou/gopsutil/load"
|
||||
|
||||
type MockPS struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockPS) LoadAvg() (*load.LoadAvgStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
r0 := ret.Get(0).(*load.LoadAvgStat)
|
||||
r1 := ret.Error(1)
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
64
plugins/system/system.go
Normal file
64
plugins/system/system.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"github.com/influxdb/tivan/plugins"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/vektra/cypress"
|
||||
)
|
||||
|
||||
type PS interface {
|
||||
LoadAvg() (*load.LoadAvgStat, error)
|
||||
}
|
||||
|
||||
type SystemStats struct {
|
||||
ps PS
|
||||
tags map[string]string
|
||||
}
|
||||
|
||||
func (s *SystemStats) Read() ([]*cypress.Message, error) {
|
||||
lv, err := s.ps.LoadAvg()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m1 := cypress.Metric()
|
||||
m1.Add("type", "gauge")
|
||||
m1.Add("name", "load1")
|
||||
m1.Add("value", lv.Load1)
|
||||
|
||||
for k, v := range s.tags {
|
||||
m1.AddTag(k, v)
|
||||
}
|
||||
|
||||
m2 := cypress.Metric()
|
||||
m2.Add("type", "gauge")
|
||||
m2.Add("name", "load5")
|
||||
m2.Add("value", lv.Load5)
|
||||
|
||||
for k, v := range s.tags {
|
||||
m2.AddTag(k, v)
|
||||
}
|
||||
|
||||
m3 := cypress.Metric()
|
||||
m3.Add("type", "gauge")
|
||||
m3.Add("name", "load15")
|
||||
m3.Add("value", lv.Load15)
|
||||
|
||||
for k, v := range s.tags {
|
||||
m3.AddTag(k, v)
|
||||
}
|
||||
|
||||
return []*cypress.Message{m1, m2, m3}, nil
|
||||
}
|
||||
|
||||
type systemPS struct{}
|
||||
|
||||
func (s *systemPS) LoadAvg() (*load.LoadAvgStat, error) {
|
||||
return load.LoadAvg()
|
||||
}
|
||||
|
||||
func init() {
|
||||
plugins.Add("system", func() plugins.Plugin {
|
||||
return &SystemStats{ps: &systemPS{}}
|
||||
})
|
||||
}
|
||||
98
plugins/system/system_test.go
Normal file
98
plugins/system/system_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/vektra/neko"
|
||||
)
|
||||
|
||||
func TestSystemStats(t *testing.T) {
|
||||
n := neko.Start(t)
|
||||
|
||||
var mps MockPS
|
||||
|
||||
n.CheckMock(&mps.Mock)
|
||||
|
||||
n.It("generates metrics from the system information", func() {
|
||||
ss := &SystemStats{ps: &mps}
|
||||
|
||||
lv := &load.LoadAvgStat{
|
||||
Load1: 0.3,
|
||||
Load5: 1.5,
|
||||
Load15: 0.8,
|
||||
}
|
||||
|
||||
mps.On("LoadAvg").Return(lv, nil)
|
||||
|
||||
msgs, err := ss.Read()
|
||||
require.NoError(t, err)
|
||||
|
||||
name, ok := msgs[0].GetString("name")
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, "load1", name)
|
||||
|
||||
val, ok := msgs[0].GetFloat("value")
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, 0.3, val)
|
||||
|
||||
name, ok = msgs[1].GetString("name")
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, "load5", name)
|
||||
|
||||
val, ok = msgs[1].GetFloat("value")
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, 1.5, val)
|
||||
|
||||
name, ok = msgs[2].GetString("name")
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, "load15", name)
|
||||
|
||||
val, ok = msgs[2].GetFloat("value")
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, 0.8, val)
|
||||
})
|
||||
|
||||
n.It("adds any tags registered", func() {
|
||||
ss := &SystemStats{
|
||||
ps: &mps,
|
||||
tags: map[string]string{
|
||||
"host": "my.test",
|
||||
"dc": "us-west-1",
|
||||
},
|
||||
}
|
||||
|
||||
lv := &load.LoadAvgStat{
|
||||
Load1: 0.3,
|
||||
Load5: 1.5,
|
||||
Load15: 0.8,
|
||||
}
|
||||
|
||||
mps.On("LoadAvg").Return(lv, nil)
|
||||
|
||||
msgs, err := ss.Read()
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, m := range msgs {
|
||||
val, ok := m.GetTag("host")
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, val, "my.test")
|
||||
|
||||
val, ok = m.GetTag("dc")
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, val, "us-west-1")
|
||||
}
|
||||
})
|
||||
|
||||
n.Meow()
|
||||
}
|
||||
Reference in New Issue
Block a user