Skip to content

Commit

Permalink
Added heartbeat stat (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored May 7, 2019
1 parent 61ffe5e commit 5794ca8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
12 changes: 3 additions & 9 deletions stats/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@ func TestAggregateStatter_Inc(t *testing.T) {
m.On("Close").Return(nil)
s := stats.NewAggregateStatter(m, time.Millisecond)

time.Sleep(time.Millisecond)

s.Inc("test", 1, 1.0, "foo", "bar")
s.Inc("test", 1, 1.0, "foo", "bar")
s.Inc("test1", 1, 1.0, "foo", "bar")
s.Inc("test", 1, 1.0, "foo1", "bar")
s.Inc("rate", 1, 0.1)

time.Sleep(2 * time.Millisecond)
time.Sleep(10 * time.Millisecond)

_ = s.Close()

Expand All @@ -94,14 +92,12 @@ func TestAggregateStatter_Gauge(t *testing.T) {
m.On("Close").Return(nil)
s := stats.NewAggregateStatter(m, time.Millisecond)

time.Sleep(time.Millisecond)

s.Gauge("test", 1, 1.0, "foo", "bar")
s.Gauge("test", 3, 1.0, "foo", "bar")
s.Gauge("test1", 4, 1.0, "foo", "bar")
s.Gauge("test", 5, 1.0, "foo1", "bar")

time.Sleep(2 * time.Millisecond)
time.Sleep(10 * time.Millisecond)

_ = s.Close()

Expand All @@ -117,15 +113,13 @@ func TestAggregateStatter_Timing(t *testing.T) {
m.On("Close").Return(nil)
s := stats.NewAggregateStatter(m, time.Millisecond)

time.Sleep(time.Millisecond)

s.Timing("test", time.Millisecond, 1.0, "foo", "bar")
s.Timing("test", time.Second, 1.0, "foo", "bar")
s.Timing("test1", time.Millisecond, 1.0, "foo", "bar")
s.Timing("test", time.Millisecond, 1.0, "foo1", "bar")
s.Timing("rate", time.Millisecond, 0.1)

time.Sleep(2 * time.Millisecond)
time.Sleep(10 * time.Millisecond)

_ = s.Close()

Expand Down
26 changes: 26 additions & 0 deletions stats/heartbeat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package stats

import (
"time"
)

// DefaultHeartbeatInterval is the default heartbeat ticker interval.
var DefaultHeartbeatInterval = time.Second

// Heartbeat enters a loop, reporting a heartbeat counter periodically.
func Heartbeat(stats Statter) {
HeartbeatEvery(stats, DefaultHeartbeatInterval)
}

// HeartbeatEvery enters a loop, reporting a heartbeat counter at the specified interval.
func HeartbeatEvery(stats Statter, t time.Duration) {
c := time.Tick(t)
for range c {
stats.Inc("heartbeat", 1, 1.0)
}
}

// HeartbeatFromStatable is the same as HeartbeatEvery but from context.
func HeartbeatFromStatable(sable Statable, t time.Duration) {
HeartbeatEvery(sable.Statter(), t)
}
33 changes: 33 additions & 0 deletions stats/heartbeat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package stats_test

import (
"testing"
"time"

"github.com/hamba/pkg/stats"
"github.com/stretchr/testify/mock"
)

func TestHeartbeat(t *testing.T) {
m := new(MockStats)
m.On("Inc", "heartbeat", int64(1), float32(1.0), mock.Anything).Return(nil)
stats.DefaultHeartbeatInterval = time.Millisecond

go stats.Heartbeat(m)

time.Sleep(100 * time.Millisecond)

m.AssertCalled(t, "Inc", "heartbeat", int64(1), float32(1.0), mock.Anything)
}

func TestHeartbeatFromStatable(t *testing.T) {
m := new(MockStats)
m.On("Inc", "heartbeat", int64(1), float32(1.0), mock.Anything).Return(nil)
sable := stats.NewMockStatable(m)

go stats.HeartbeatFromStatable(sable, time.Millisecond)

time.Sleep(100 * time.Millisecond)

m.AssertCalled(t, "Inc", "heartbeat", int64(1), float32(1.0), mock.Anything)
}
2 changes: 1 addition & 1 deletion stats/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestRuntime(t *testing.T) {
m.AssertCalled(t, "Gauge", "runtime.cpu.goroutines", mock.Anything, mock.Anything, mock.Anything)
}

func TestRuntimeFromContext(t *testing.T) {
func TestRuntimeFromStatable(t *testing.T) {
m := new(MockStats)
m.On("Gauge", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
m.On("Timing", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
Expand Down

0 comments on commit 5794ca8

Please sign in to comment.