stats

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 7 Imported by: 10

Documentation

Overview

Package stats implements interfaces and helpers for metrics gathering.

Index

Examples

Constants

View Source
const (
	IncType int = iota
	GaugeType
	TimingType
)

Metric types.

Variables

View Source
var DefaultHeartbeatInterval = time.Second

DefaultHeartbeatInterval is the default heartbeat ticker interval.

View Source
var DefaultRuntimeInterval = 30 * time.Second

DefaultRuntimeInterval is the default runtime ticker interval.

View Source
var (
	// Null is the null Statter instance.
	Null = &nullStatter{}
)

Functions

func Close

func Close(sable Statable) error

Close closes the client and flushes buffered stats, if applicable.

func Gauge

func Gauge(sable Statable, name string, value float64, rate float32, tags ...string)

Gauge measures the value of a metric.

func Group

func Group(sable Statable, prefix string, fn func(s Statter))

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"
}
Output:

func Heartbeat

func Heartbeat(stats Statter)

Heartbeat enters a loop, reporting a heartbeat counter periodically.

func HeartbeatEvery

func HeartbeatEvery(stats Statter, t time.Duration)

HeartbeatEvery enters a loop, reporting a heartbeat counter at the specified interval.

func HeartbeatFromStatable

func HeartbeatFromStatable(sable Statable, t time.Duration)

HeartbeatFromStatable is the same as HeartbeatEvery but from context.

func Inc

func Inc(sable Statable, name string, value int64, rate float32, tags ...string)

Inc increments a count by the value.

func Runtime

func Runtime(stats Statter)

Runtime enters a loop, reporting runtime stats periodically.

func RuntimeEvery

func RuntimeEvery(s Statter, t time.Duration)

RuntimeEvery enters a loop, reporting runtime stats at the specified interval.

func RuntimeFromStatable

func RuntimeFromStatable(sable Statable, t time.Duration)

RuntimeFromStatable is the same as RuntimeEvery but from a Statable.

func Timing

func Timing(sable Statable, name string, value time.Duration, rate float32, tags ...string)

Timing sends the value of a Duration.

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) Timing

func (s *AggregateStatter) Timing(name string, value time.Duration, rate float32, tags ...string)

Timing sends the value of a Duration.

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.

func Unwrap

func Unwrap(stats Statter) Statter

Unwrap returns the result of calling the Unwrap method on stats. If stats does not have an Unwrap method, nil will be returned.

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"
}
Output:

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) Timing

func (s TaggedStatter) Timing(name string, value time.Duration, rate float32, tags ...string)

Timing sends the value of a Duration.

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

func Time(sable Statable, name string, rate float32, tags ...string) Timer

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")
}
Output:

type Wrapper

type Wrapper interface {
	Unwrap() Statter
}

Wrapper represents a statter that can return its wrapped statter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL