Documentation
¶
Overview ¶
Package stats implements interfaces and helpers for metrics gathering.
Index ¶
- Constants
- Variables
- func Close(sable Statable) error
- func Gauge(sable Statable, name string, value float64, rate float32, tags ...string)
- func Group(sable Statable, prefix string, fn func(s Statter))
- func Heartbeat(stats Statter)
- func HeartbeatEvery(stats Statter, t time.Duration)
- func HeartbeatFromStatable(sable Statable, t time.Duration)
- func Inc(sable Statable, name string, value int64, rate float32, tags ...string)
- func Runtime(stats Statter)
- func RuntimeEvery(s Statter, t time.Duration)
- func RuntimeFromStatable(sable Statable, t time.Duration)
- func Timing(sable Statable, name string, value time.Duration, rate float32, tags ...string)
- type AggregateFunc
- type AggregateStatter
- func (s *AggregateStatter) Close() error
- func (s *AggregateStatter) Gauge(name string, value float64, rate float32, tags ...string)
- func (s *AggregateStatter) Inc(name string, value int64, rate float32, tags ...string)
- func (s *AggregateStatter) Timing(name string, value time.Duration, rate float32, tags ...string)
- func (s *AggregateStatter) Unwrap() Statter
- type Aggregator
- type Metric
- type MockStatable
- type Statable
- type Statter
- type TaggedStatter
- func (s TaggedStatter) Close() error
- func (s TaggedStatter) Gauge(name string, value float64, rate float32, tags ...string)
- func (s TaggedStatter) Inc(name string, value int64, rate float32, tags ...string)
- func (s TaggedStatter) Timing(name string, value time.Duration, rate float32, tags ...string)
- func (s TaggedStatter) Unwrap() Statter
- type Timer
- type Wrapper
Examples ¶
Constants ¶
const ( IncType int = iota GaugeType TimingType )
Metric types.
Variables ¶
var DefaultHeartbeatInterval = time.Second
DefaultHeartbeatInterval is the default heartbeat ticker interval.
var DefaultRuntimeInterval = 30 * time.Second
DefaultRuntimeInterval is the default runtime ticker interval.
var (
// Null is the null Statter instance.
Null = &nullStatter{}
)
Functions ¶
func Group ¶
Group adds a common prefix to a set of stats.
Example ¶
package main
import (
"github.com/hamba/pkg/stats"
)
func main() {
var statable stats.Statable
// Set your Statter implementation
stats.Group(statable, "prefix", func(s stats.Statter) {
s.Inc("test", 1, 1.0, "tag", "foobar")
})
// Output name: "prefix.test"
}
func Heartbeat ¶
func Heartbeat(stats Statter)
Heartbeat enters a loop, reporting a heartbeat counter periodically.
func HeartbeatEvery ¶
HeartbeatEvery enters a loop, reporting a heartbeat counter at the specified interval.
func HeartbeatFromStatable ¶
HeartbeatFromStatable is the same as HeartbeatEvery but from context.
func Runtime ¶
func Runtime(stats Statter)
Runtime enters a loop, reporting runtime stats periodically.
func RuntimeEvery ¶
RuntimeEvery enters a loop, reporting runtime stats at the specified interval.
func RuntimeFromStatable ¶
RuntimeFromStatable is the same as RuntimeEvery but from a Statable.
Types ¶
type AggregateFunc ¶
type AggregateFunc func(*AggregateStatter)
AggregateFunc represents a configuration function for an AggregateStatter.
func WithCounterAggregator ¶
func WithCounterAggregator(a Aggregator) AggregateFunc
WithCounterAggregator sets the aggregator to use with counters.
func WithGaugeAggregator ¶
func WithGaugeAggregator(a Aggregator) AggregateFunc
WithGaugeAggregator sets the aggregator to use with gauges.
func WithTimingAggregator ¶
func WithTimingAggregator(a Aggregator) AggregateFunc
WithTimingAggregator sets the aggregator to use with timings.
type AggregateStatter ¶
type AggregateStatter struct {
// contains filtered or unexported fields
}
AggregateStatter aggregates the incoming stats for a given interval.
By default counters will be summed, gauges will take the last value and timings will be averaged.
func NewAggregateStatter ¶
func NewAggregateStatter(stats Statter, interval time.Duration, opts ...AggregateFunc) *AggregateStatter
NewAggregateStatter creates a new aggregate statter, with the given interval.
func (*AggregateStatter) Close ¶
func (s *AggregateStatter) Close() error
Close closes the client and flushes aggregated stats.
func (*AggregateStatter) Gauge ¶
func (s *AggregateStatter) Gauge(name string, value float64, rate float32, tags ...string)
Gauge measures the value of a Metric.
func (*AggregateStatter) Inc ¶
func (s *AggregateStatter) Inc(name string, value int64, rate float32, tags ...string)
Inc increments a count by the value.
func (*AggregateStatter) Unwrap ¶
func (s *AggregateStatter) Unwrap() Statter
Unwrap returns the underlying statter.
type Aggregator ¶
type Aggregator interface {
// Aggregate aggregates the given metric.
Aggregate(Metric)
// Flush flushes the aggregated metrics to the given Statter.
Flush(Statter)
}
Aggregator represents a metric aggregator.
type Metric ¶
type Metric struct {
Type int
Hash string
Name string
IntVal int64
FloatVal float64
DurVal time.Duration
Rate float32
Tags []string
}
Metric represents a stats metric.
type MockStatable ¶
type MockStatable struct {
// contains filtered or unexported fields
}
MockStatable implements the Statable interface.
func NewMockStatable ¶
func NewMockStatable(s Statter) *MockStatable
NewMockStatable creates a new MockLoggable.
func (*MockStatable) Statter ¶
func (m *MockStatable) Statter() Statter
Statter implements the Statable interface.
type Statable ¶
type Statable interface {
Statter() Statter
}
Statable represents an object that has a Statter.
type Statter ¶
type Statter interface {
io.Closer
// Inc increments a count by the value.
Inc(name string, value int64, rate float32, tags ...string)
// Gauge measures the value of a metric.
Gauge(name string, value float64, rate float32, tags ...string)
// Timing sends the value of a Duration.
Timing(name string, value time.Duration, rate float32, tags ...string)
}
Statter represents a stats instance.
type TaggedStatter ¶
type TaggedStatter struct {
// contains filtered or unexported fields
}
TaggedStatter wraps a Statter instance applying tags to all metrics.
Example ¶
package main
import (
"github.com/hamba/pkg/stats"
)
func main() {
var stat stats.Statter
// Set your Statter implementation
stat = stats.NewTaggedStatter(stat, "app_env", "stag")
stat.Inc("test", 1, 1.0, "tag", "foobar")
// Output tags: "tag", "foobar", "app_env", "stag"
}
func NewTaggedStatter ¶
func NewTaggedStatter(stats Statter, tags ...string) *TaggedStatter
NewTaggedStatter creates a new TaggedStatter instance.
func (TaggedStatter) Close ¶
func (s TaggedStatter) Close() error
Close closes the client and flushes buffered stats, if applicable.
func (TaggedStatter) Gauge ¶
func (s TaggedStatter) Gauge(name string, value float64, rate float32, tags ...string)
Gauge measures the value of a metric.
func (TaggedStatter) Inc ¶
func (s TaggedStatter) Inc(name string, value int64, rate float32, tags ...string)
Inc increments a count by the value.
func (TaggedStatter) Unwrap ¶
func (s TaggedStatter) Unwrap() Statter
Unwrap returns the underlying statter.
type Timer ¶
type Timer interface {
// Start starts the timer.
Start()
// Done stops the timer and submits the Timing metric.
Done()
}
Timer represents a timer.
func Time ¶
Time is a shorthand for Timing.
Example ¶
package main
import (
"github.com/hamba/pkg/stats"
)
func main() {
var statable stats.Statable
// Set your Statter implementation
timer := stats.Time(statable, "latency", 1.0, "tag", "foobar")
// Do something
timer.Done()
// Output mertic: stats.Timing(ctx, "latency", duration, 1.0, "tag", "foobar")
}