gostats
is a Go metrics library with support for Counters, Gauges, and Timers.
go get github.com/lyft/gostats
go test ./...
In order to start using gostats
, import it into your project with:
import "github.com/lyft/gostats"
A thread-safe mock sink is provided by the gostats/mock package. The mock sink also provides methods that are useful for testing (as demonstrated below).
package mock_test
import (
"testing"
"github.com/lyft/gostats"
"github.com/lyft/gostats/mock"
)
type Config struct {
Stats stats.Store
}
func TestMockExample(t *testing.T) {
sink := mock.NewSink()
conf := Config{
Stats: stats.NewStore(sink, false),
}
conf.Stats.NewCounter("name").Inc()
conf.Stats.Flush()
sink.AssertCounterEquals(t, "name", 1)
}
If you do not need to assert on the contents of the sink the below example can be used to quickly create a thread-safe stats.Scope
:
package config
import (
"github.com/lyft/gostats"
"github.com/lyft/gostats/mock"
)
type Config struct {
Stats stats.Store
}
func NewConfig() *Config {
return &Config{
Stats: stats.NewDefaultStore(),
}
}
func NewMockConfig() *Config {
sink := mock.NewSink()
return &Config{
Stats: stats.NewStore(sink, false),
}
}