Documentation
¶
Index ¶
- func Fatal(tb testing.TB) testing.TB
- func ParseTags(stat string) (string, map[string]string)
- func SerializeTags(name string, tagsm map[string]string) string
- type Sink
- func (s *Sink) AssertCounterCallCount(tb testing.TB, name string, exp int)
- func (s *Sink) AssertCounterEquals(tb testing.TB, name string, exp uint64)
- func (s *Sink) AssertCounterExists(tb testing.TB, name string)
- func (s *Sink) AssertCounterNotExists(tb testing.TB, name string)
- func (s *Sink) AssertGaugeCallCount(tb testing.TB, name string, exp int)
- func (s *Sink) AssertGaugeEquals(tb testing.TB, name string, exp uint64)
- func (s *Sink) AssertGaugeExists(tb testing.TB, name string)
- func (s *Sink) AssertGaugeNotExists(tb testing.TB, name string)
- func (s *Sink) AssertTimerCallCount(tb testing.TB, name string, exp int)
- func (s *Sink) AssertTimerEquals(tb testing.TB, name string, exp float64)
- func (s *Sink) AssertTimerExists(tb testing.TB, name string)
- func (s *Sink) AssertTimerNotExists(tb testing.TB, name string)
- func (s *Sink) Counter(name string) uint64
- func (s *Sink) CounterCallCount(name string) int64
- func (s *Sink) Counters() map[string]uint64
- func (*Sink) Flush()
- func (s *Sink) FlushCounter(name string, val uint64)
- func (s *Sink) FlushGauge(name string, val uint64)
- func (s *Sink) FlushTimer(name string, val float64)
- func (s *Sink) Gauge(name string) uint64
- func (s *Sink) GaugeCallCount(name string) int64
- func (s *Sink) Gauges() map[string]uint64
- func (s *Sink) ListCounters() []string
- func (s *Sink) ListGauges() []string
- func (s *Sink) ListTimers() []string
- func (s *Sink) LoadCounter(name string) (uint64, bool)
- func (s *Sink) LoadGauge(name string) (uint64, bool)
- func (s *Sink) LoadTimer(name string) (float64, bool)
- func (s *Sink) Reset()
- func (s *Sink) Timer(name string) float64
- func (s *Sink) TimerCallCount(name string) int64
- func (s *Sink) Timers() map[string]float64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fatal ¶
Fatal is a wrapper around *testing.T and *testing.B that causes Sink Assert* methods to immediately fail a test and stop execution. Otherwise, the Assert methods call tb.Errorf(), which marks the test as failed, but allows execution to continue.
Examples of Fatal() can be found in the sink test code.
var sink Sink var t *testing.T sink.AssertCounterEquals(Must(t), "name", 1)
Example ¶
package main
import (
"testing"
stats "github.com/lyft/gostats"
"github.com/lyft/gostats/mock"
)
func main() {
sink := mock.NewSink()
store := stats.NewStore(sink, false)
store.NewCounter("c").Set(2)
store.Flush()
// In real test code you would use the *testing.T or *testing.B
// passed to the test function.
t := &testing.T{}
// This will cause the test to immediately fail with .Fatal()
// if the assertion is false.
sink.AssertCounterEquals(mock.Fatal(t), "c", 2)
}
func ParseTags ¶ added in v0.4.4
ParseTags extracts the name and tags from a statsd stat.
Example of parsing tags and the stat name from a statsd stat:
expected := map[string]string{
"_f": "i",
"tag1": "value1",
}
name, tags := mock.ParseTags("prefix.c.___f=i.__tag1=value1")
if name != "panic.c" {
panic(fmt.Sprintf("Name: got: %q want: %q", name, "panic.c"))
}
if !reflect.DeepEqual(tags, expected) {
panic(fmt.Sprintf("Tags: got: %q want: %q", tags, expected))
}
Example ¶
package main
import (
"fmt"
"reflect"
stats "github.com/lyft/gostats"
"github.com/lyft/gostats/mock"
)
func main() {
sink := mock.NewSink()
store := stats.NewStore(sink, false)
tags := map[string]string{
"key_1": "val_1",
"key_2": "val_2",
}
for i := 0; i < 4; i++ {
store.NewCounterWithTags(fmt.Sprintf("c_%d", i), tags).Inc()
}
store.Flush()
// Check that the counters all have the same tags
for stat := range sink.Counters() {
name, m := mock.ParseTags(stat)
if !reflect.DeepEqual(m, tags) {
panic(fmt.Sprintf("Tags: got: %q want: %q", m, tags))
}
fmt.Printf("%s: okay\n", name)
}
}
Output: c_0: okay c_1: okay c_2: okay c_3: okay
func SerializeTags ¶ added in v0.4.4
SerializeTags serializes name and tags into a statsd stat.
tags := map[string]string{
"key_1": "val_1"
"key_2": "val_2"
}
s.AssertCounterExists(tb, SerializeTags("name", tags))
Example ¶
package main
import (
"fmt"
stats "github.com/lyft/gostats"
"github.com/lyft/gostats/mock"
)
func main() {
sink := mock.NewSink()
store := stats.NewStore(sink, false)
tags := map[string]string{
"key_1": "val_1",
"key_2": "val_2",
}
counter := store.NewCounterWithTags("counter", tags)
counter.Add(2)
store.Flush()
n := sink.Counter(mock.SerializeTags("counter", tags))
fmt.Println("counter:", n, n == 2)
}
Output: counter: 2 true
Types ¶
type Sink ¶
type Sink struct {
// contains filtered or unexported fields
}
A Sink is a mock sink meant for testing that is safe for concurrent use.
func NewSink ¶
func NewSink() *Sink
NewSink returns a new Sink which implements the stats.Sink interface and is suitable for testing.
func (*Sink) AssertCounterCallCount ¶
AssertCounterCallCount asserts that Counter name was called exp times.
func (*Sink) AssertCounterEquals ¶
AssertCounterEquals asserts that Counter name is present and has value exp.
func (*Sink) AssertCounterExists ¶
AssertCounterExists asserts that Counter name exists.
func (*Sink) AssertCounterNotExists ¶
AssertCounterNotExists asserts that Counter name does not exist.
func (*Sink) AssertGaugeCallCount ¶
AssertGaugeCallCount asserts that Gauge name was called exp times.
func (*Sink) AssertGaugeEquals ¶
AssertGaugeEquals asserts that Gauge name is present and has value exp.
func (*Sink) AssertGaugeExists ¶
AssertGaugeExists asserts that Gauge name exists.
func (*Sink) AssertGaugeNotExists ¶
AssertGaugeNotExists asserts that Gauge name does not exist.
func (*Sink) AssertTimerCallCount ¶
AssertTimerCallCount asserts that Timer name was called exp times.
func (*Sink) AssertTimerEquals ¶
AssertTimerEquals asserts that Timer name is present and has value exp.
func (*Sink) AssertTimerExists ¶
AssertTimerExists asserts that Timer name exists.
func (*Sink) AssertTimerNotExists ¶
AssertTimerNotExists asserts that Timer name does not exist.
func (*Sink) Counter ¶
Counter is shorthand for LoadCounter, zero is returned if the stat is not found.
func (*Sink) CounterCallCount ¶
CounterCallCount returns the number of times stat name has been called/updated.
func (*Sink) Counters ¶ added in v0.4.2
Counters returns all the counters currently stored by the sink.
func (*Sink) FlushCounter ¶
FlushCounter implements the stats.Sink.FlushCounter method and adds val to stat name.
func (*Sink) FlushGauge ¶
FlushGauge implements the stats.Sink.FlushGauge method and adds val to stat name.
func (*Sink) FlushTimer ¶
FlushTimer implements the stats.Sink.FlushTimer method and adds val to stat name.
func (*Sink) GaugeCallCount ¶
GaugeCallCount returns the number of times stat name has been called/updated.
func (*Sink) ListCounters ¶ added in v0.4.5
ListCounters returns a list of existing counter names.
func (*Sink) ListGauges ¶ added in v0.4.5
ListGauges returns a list of existing gauge names.
func (*Sink) ListTimers ¶ added in v0.4.5
ListTimers returns a list of existing timer names.
func (*Sink) LoadCounter ¶
LoadCounter returns the value for stat name and if it was found.
func (*Sink) Reset ¶
func (s *Sink) Reset()
Reset resets the Sink's counters, timers and gauges to zero.
func (*Sink) TimerCallCount ¶
TimerCallCount returns the number of times stat name has been called/updated.