stats

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: Apache-2.0 Imports: 14 Imported by: 29

Documentation

Overview

Package stats is a wrapper for expvar. It additionally exports new types that can be used to track performance. It also provides a callback hook that allows a program to export the variables using methods other than /debug/vars. All variables support a String function that is expected to return a JSON representation of the variable. Any function named Add will add the specified number to the variable. Any function named Counts returns a map of counts that can be used by Rates to track rates over time.

Index

Constants

View Source
const StatsAllStr = "all"

StatsAllStr is the consolidated name if a dimension gets combined.

Variables

View Source
var CommonTags = flag.String("stats_common_tags", "", `Comma-separated list of common tags for the stats backend. It provides both label and values. Example: label1:value1,label2:value2`)

CommonTags is a comma-separated list of common tags for stats backends

Functions

func GetSnakeName

func GetSnakeName(name string) string

GetSnakeName calls toSnakeName on the passed in string. It produces a snake-cased name from the provided camel-cased name. It memoizes the transformation and returns the stored result if available.

func IsDimensionCombined

func IsDimensionCombined(name string) bool

IsDimensionCombined returns true if the specified dimension should be combined.

func ParseCommonTags added in v0.11.0

func ParseCommonTags(s string) map[string]string

ParseCommonTags parses a comma-separated string into map of tags If you want to global service values like host, service name, git revision, etc, this is the place to do it.

func Publish

func Publish(name string, v expvar.Var)

Publish is expvar.Publish+hook

func PublishJSONFunc

func PublishJSONFunc(name string, f func() string)

PublishJSONFunc publishes any function that returns a JSON string as a variable. The string is sent to expvar as is.

func Register

func Register(nvh NewVarHook)

Register allows you to register a callback function that will be called whenever a new stats variable gets created. This can be used to build alternate methods of exporting stats variables.

func RegisterHistogramHook added in v0.10.0

func RegisterHistogramHook(hook func(string, int64))

RegisterHistogramHook registers timer hook

func RegisterPushBackend

func RegisterPushBackend(name string, backend PushBackend)

RegisterPushBackend allows modules to register PushBackend implementations. Should be called on init().

func RegisterTimerHook added in v0.10.0

func RegisterTimerHook(hook func(string, string, int64, *Timings))

RegisterTimerHook registers timer hook

Types

type CountTracker

type CountTracker interface {
	// Counts returns a map which maps each category to a count.
	// Subsequent calls must return a monotonously increasing count for the same
	// category.
	// Optionally, an implementation may include the "All" category which has
	// the total count across all categories (e.g. timing.go does this).
	Counts() map[string]int64
}

CountTracker defines the interface that needs to be supported by a variable for being tracked by Rates.

func CounterForDimension

func CounterForDimension(mt MultiTracker, dimension string) CountTracker

CounterForDimension returns a CountTracker for the provided dimension. It will panic if the dimension isn't a legal label for mt.

type Counter

type Counter struct {
	// contains filtered or unexported fields
}

Counter tracks a cumulative count of a metric. For a one-dimensional or multi-dimensional counter, please use CountersWithSingleLabel or CountersWithMultiLabels instead.

func NewCounter

func NewCounter(name string, help string) *Counter

NewCounter returns a new Counter.

func (*Counter) Add

func (v *Counter) Add(delta int64)

Add adds the provided value to the Counter.

func (*Counter) Get

func (v *Counter) Get() int64

Get returns the value.

func (*Counter) Help

func (v *Counter) Help() string

Help returns the help string.

func (*Counter) Reset

func (v *Counter) Reset()

Reset resets the counter value to 0.

func (*Counter) String

func (v *Counter) String() string

String implements the expvar.Var interface.

type CounterDuration

type CounterDuration struct {
	// contains filtered or unexported fields
}

CounterDuration exports a time.Duration as counter.

func NewCounterDuration

func NewCounterDuration(name, help string) *CounterDuration

NewCounterDuration returns a new CounterDuration.

func (*CounterDuration) Add

func (cd *CounterDuration) Add(delta time.Duration)

Add adds the provided value to the CounterDuration.

func (*CounterDuration) Get

func (cd *CounterDuration) Get() time.Duration

Get returns the value.

func (CounterDuration) Help

func (cd CounterDuration) Help() string

Help implements the Variable interface.

func (CounterDuration) String

func (cd CounterDuration) String() string

String is the implementation of expvar.var.

type CounterDurationFunc

type CounterDurationFunc struct {
	F func() time.Duration
	// contains filtered or unexported fields
}

CounterDurationFunc allows to provide the value via a custom function.

func NewCounterDurationFunc

func NewCounterDurationFunc(name string, help string, f func() time.Duration) *CounterDurationFunc

NewCounterDurationFunc creates a new CounterDurationFunc instance and publishes it if name is set.

func (CounterDurationFunc) Get

func (cf CounterDurationFunc) Get() int64

Get returns the value.

func (CounterDurationFunc) Help

func (cf CounterDurationFunc) Help() string

Help implements the Variable interface.

func (CounterDurationFunc) String

func (cf CounterDurationFunc) String() string

String is the implementation of expvar.var.

type CounterFunc

type CounterFunc struct {
	F func() int64
	// contains filtered or unexported fields
}

CounterFunc allows to provide the counter value via a custom function. For implementations that differentiate between Counters/Gauges, CounterFunc's values only go up (or are reset to 0).

func NewCounterFunc

func NewCounterFunc(name string, help string, f func() int64) *CounterFunc

NewCounterFunc creates a new CounterFunc instance and publishes it if name is set.

func (CounterFunc) Get

func (cf CounterFunc) Get() int64

Get returns the value.

func (CounterFunc) Help

func (cf CounterFunc) Help() string

Help returns the help string.

func (CounterFunc) String

func (cf CounterFunc) String() string

String implements expvar.Var.

type CountersFuncWithMultiLabels

type CountersFuncWithMultiLabels struct {
	// contains filtered or unexported fields
}

CountersFuncWithMultiLabels is a multidimensional counters implementation where names of categories are compound names made with joining multiple strings with '.'. Since the map is returned by the function, we assume it's in the right format (meaning each key is of the form 'aaa.bbb.ccc' with as many elements as there are in Labels).

Note that there is no CountersFuncWithSingleLabel object. That this because such an object would be identical to this one because these function-based counters have no Add() or Set() method which are different for the single vs. multiple labels cases. If you have only a single label, pass an array with a single element.

func NewCountersFuncWithMultiLabels

func NewCountersFuncWithMultiLabels(name, help string, labels []string, f func() map[string]int64) *CountersFuncWithMultiLabels

NewCountersFuncWithMultiLabels creates a new CountersFuncWithMultiLabels mapping to the provided function.

func (CountersFuncWithMultiLabels) Counts

func (c CountersFuncWithMultiLabels) Counts() map[string]int64

Counts returns a copy of the counters' map.

func (CountersFuncWithMultiLabels) Help

Help returns the help string.

func (CountersFuncWithMultiLabels) Labels

func (c CountersFuncWithMultiLabels) Labels() []string

Labels returns the list of labels.

func (CountersFuncWithMultiLabels) String

String implements the expvar.Var interface.

type CountersWithMultiLabels

type CountersWithMultiLabels struct {
	// contains filtered or unexported fields
}

CountersWithMultiLabels is a multidimensional counters implementation. Internally, each tuple of dimensions ("labels") is stored as a single label value where all label values are joined with ".".

func NewCountersWithMultiLabels

func NewCountersWithMultiLabels(name, help string, labels []string) *CountersWithMultiLabels

NewCountersWithMultiLabels creates a new CountersWithMultiLabels instance, and publishes it if name is set.

func (*CountersWithMultiLabels) Add

func (mc *CountersWithMultiLabels) Add(names []string, value int64)

Add adds a value to a named counter. len(names) must be equal to len(Labels)

func (*CountersWithMultiLabels) Counts

func (mc *CountersWithMultiLabels) Counts() map[string]int64

Counts returns a copy of the Counters' map. The key is a single string where all labels are joined by a "." e.g. "label1.label2".

func (*CountersWithMultiLabels) Help

func (c *CountersWithMultiLabels) Help() string

Help returns the help string.

func (*CountersWithMultiLabels) Labels

func (mc *CountersWithMultiLabels) Labels() []string

Labels returns the list of labels.

func (*CountersWithMultiLabels) Reset

func (mc *CountersWithMultiLabels) Reset(names []string)

Reset resets the value of a named counter back to 0. len(names) must be equal to len(Labels).

func (*CountersWithMultiLabels) ResetAll

func (mc *CountersWithMultiLabels) ResetAll()

ResetAll clears the counters

func (*CountersWithMultiLabels) String

func (c *CountersWithMultiLabels) String() string

func (*CountersWithMultiLabels) ZeroAll

func (c *CountersWithMultiLabels) ZeroAll()

ZeroAll zeroes out all values

type CountersWithSingleLabel

type CountersWithSingleLabel struct {
	// contains filtered or unexported fields
}

CountersWithSingleLabel tracks multiple counter values for a single dimension ("label"). It provides a Counts method which can be used for tracking rates.

func NewCountersWithSingleLabel

func NewCountersWithSingleLabel(name, help, label string, tags ...string) *CountersWithSingleLabel

NewCountersWithSingleLabel create a new Counters instance. If name is set, the variable gets published. The function also accepts an optional list of tags that pre-creates them initialized to 0. label is a category name used to organize the tags. It is currently only used by Prometheus, but not by the expvar package.

func (*CountersWithSingleLabel) Add

func (c *CountersWithSingleLabel) Add(name string, value int64)

Add adds a value to a named counter.

func (*CountersWithSingleLabel) Counts

func (c *CountersWithSingleLabel) Counts() map[string]int64

Counts returns a copy of the Counters' map.

func (*CountersWithSingleLabel) Help

func (c *CountersWithSingleLabel) Help() string

Help returns the help string.

func (*CountersWithSingleLabel) Label

func (c *CountersWithSingleLabel) Label() string

Label returns the label name.

func (*CountersWithSingleLabel) Reset

func (c *CountersWithSingleLabel) Reset(name string)

Reset resets the value for the name.

func (*CountersWithSingleLabel) ResetAll

func (c *CountersWithSingleLabel) ResetAll()

ResetAll clears the counters

func (*CountersWithSingleLabel) String

func (c *CountersWithSingleLabel) String() string

func (*CountersWithSingleLabel) ZeroAll

func (c *CountersWithSingleLabel) ZeroAll()

ZeroAll zeroes out all values

type FloatFunc

type FloatFunc func() float64

FloatFunc converts a function that returns a float64 as an expvar.

func (FloatFunc) Help

func (f FloatFunc) Help() string

Help returns the help string (undefined currently)

func (FloatFunc) String

func (f FloatFunc) String() string

String is the implementation of expvar.var

type Gauge

type Gauge struct {
	Counter
}

Gauge tracks the current value of an integer metric. The emphasis here is on *current* i.e. this is not a cumulative counter. For a one-dimensional or multi-dimensional gauge, please use GaugeWithSingleLabel or GaugesWithMultiLabels instead.

func NewGauge

func NewGauge(name string, help string) *Gauge

NewGauge creates a new Gauge and publishes it if name is set.

func (*Gauge) Add

func (v *Gauge) Add(delta int64)

Add adds the provided value to the Gauge.

func (*Gauge) Set

func (v *Gauge) Set(value int64)

Set overwrites the current value.

type GaugeDuration

type GaugeDuration struct {
	CounterDuration
}

GaugeDuration exports a time.Duration as gauge. In addition to CounterDuration, it also has Set() which allows overriding the current value.

func NewGaugeDuration

func NewGaugeDuration(name, help string) *GaugeDuration

NewGaugeDuration returns a new GaugeDuration.

func (*GaugeDuration) Set

func (gd *GaugeDuration) Set(value time.Duration)

Set sets the value.

type GaugeDurationFunc

type GaugeDurationFunc struct {
	CounterDurationFunc
}

GaugeDurationFunc allows to provide the value via a custom function.

func NewGaugeDurationFunc

func NewGaugeDurationFunc(name string, help string, f func() time.Duration) *GaugeDurationFunc

NewGaugeDurationFunc creates a new GaugeDurationFunc instance and publishes it if name is set.

type GaugeFunc

type GaugeFunc struct {
	CounterFunc
}

GaugeFunc is the same as CounterFunc but meant for gauges. It's a wrapper around CounterFunc for values that go up/down for implementations (like Prometheus) that need to differ between Counters and Gauges.

func NewGaugeFunc

func NewGaugeFunc(name string, help string, f func() int64) *GaugeFunc

NewGaugeFunc creates a new GaugeFunc instance and publishes it if name is set.

type GaugesFuncWithMultiLabels

type GaugesFuncWithMultiLabels struct {
	CountersFuncWithMultiLabels
}

GaugesFuncWithMultiLabels is a wrapper around CountersFuncWithMultiLabels for values that go up/down for implementations (like Prometheus) that need to differ between Counters and Gauges.

func NewGaugesFuncWithMultiLabels

func NewGaugesFuncWithMultiLabels(name, help string, labels []string, f func() map[string]int64) *GaugesFuncWithMultiLabels

NewGaugesFuncWithMultiLabels creates a new GaugesFuncWithMultiLabels mapping to the provided function.

type GaugesWithMultiLabels

type GaugesWithMultiLabels struct {
	CountersWithMultiLabels
}

GaugesWithMultiLabels is a CountersWithMultiLabels implementation where the values can go up and down.

func NewGaugesWithMultiLabels

func NewGaugesWithMultiLabels(name, help string, labels []string) *GaugesWithMultiLabels

NewGaugesWithMultiLabels creates a new GaugesWithMultiLabels instance, and publishes it if name is set.

func (*GaugesWithMultiLabels) Help

func (c *GaugesWithMultiLabels) Help() string

Help returns the help string.

func (*GaugesWithMultiLabels) Set

func (mg *GaugesWithMultiLabels) Set(names []string, value int64)

Set sets the value of a named counter. len(names) must be equal to len(Labels).

func (*GaugesWithMultiLabels) String

func (c *GaugesWithMultiLabels) String() string

func (*GaugesWithMultiLabels) ZeroAll

func (c *GaugesWithMultiLabels) ZeroAll()

ZeroAll zeroes out all values

type GaugesWithSingleLabel

type GaugesWithSingleLabel struct {
	CountersWithSingleLabel
}

GaugesWithSingleLabel is similar to CountersWithSingleLabel, except its meant to track the current value and not a cumulative count.

func NewGaugesWithSingleLabel

func NewGaugesWithSingleLabel(name, help, label string, tags ...string) *GaugesWithSingleLabel

NewGaugesWithSingleLabel creates a new GaugesWithSingleLabel and publishes it if the name is set.

func (*GaugesWithSingleLabel) Counts

func (c *GaugesWithSingleLabel) Counts() map[string]int64

Counts returns a copy of the Counters' map.

func (*GaugesWithSingleLabel) Help

func (c *GaugesWithSingleLabel) Help() string

Help returns the help string.

func (*GaugesWithSingleLabel) Set

func (g *GaugesWithSingleLabel) Set(name string, value int64)

Set sets the value of a named gauge.

func (*GaugesWithSingleLabel) String

func (c *GaugesWithSingleLabel) String() string

func (*GaugesWithSingleLabel) ZeroAll

func (c *GaugesWithSingleLabel) ZeroAll()

ZeroAll zeroes out all values

type Histogram

type Histogram struct {
	// contains filtered or unexported fields
}

Histogram tracks counts and totals while splitting the counts under different buckets using specified cutoffs.

func NewGenericHistogram

func NewGenericHistogram(name, help string, cutoffs []int64, labels []string, countLabel, totalLabel string) *Histogram

NewGenericHistogram creates a histogram where all the labels are supplied by the caller. The number of labels has to be one more than the number of cutoffs because the last label captures everything that exceeds the highest cutoff.

func NewHistogram

func NewHistogram(name, help string, cutoffs []int64) *Histogram

NewHistogram creates a histogram with auto-generated labels based on the cutoffs. The buckets are categorized using the following criterion: cutoff[i-1] < value <= cutoff[i]. Anything higher than the highest cutoff is labeled as "inf".

func (*Histogram) Add

func (h *Histogram) Add(value int64)

Add adds a new measurement to the Histogram.

func (*Histogram) Buckets

func (h *Histogram) Buckets() []int64

Buckets returns a snapshot of the current values in all buckets.

func (*Histogram) Count

func (h *Histogram) Count() (count int64)

Count returns the number of times Add has been called.

func (*Histogram) CountLabel

func (h *Histogram) CountLabel() string

CountLabel returns the count label that was set when this Histogram was created.

func (*Histogram) Counts

func (h *Histogram) Counts() map[string]int64

Counts returns a map from labels to the current count in the Histogram for that label.

func (*Histogram) Cutoffs

func (h *Histogram) Cutoffs() []int64

Cutoffs returns the cutoffs that were set when this Histogram was created.

func (*Histogram) Help

func (h *Histogram) Help() string

Help returns the help string.

func (*Histogram) Labels

func (h *Histogram) Labels() []string

Labels returns the labels that were set when this Histogram was created.

func (*Histogram) MarshalJSON

func (h *Histogram) MarshalJSON() ([]byte, error)

MarshalJSON returns a JSON representation of the Histogram. Note that sum of all buckets may not be equal to the total temporarily, because Add() increments bucket and total with two atomic operations.

func (*Histogram) String

func (h *Histogram) String() string

String returns a string representation of the Histogram. Note that sum of all buckets may not be equal to the total temporarily, because Add() increments bucket and total with two atomic operations.

func (*Histogram) Total

func (h *Histogram) Total() (total int64)

Total returns the sum of all values that have been added to this Histogram.

func (*Histogram) TotalLabel

func (h *Histogram) TotalLabel() string

TotalLabel returns the total label that was set when this Histogram was created.

type JSONFunc

type JSONFunc func() string

JSONFunc is the public type for a single function that returns json directly.

func (JSONFunc) String

func (f JSONFunc) String() string

String is the implementation of expvar.var

type MultiTimings

type MultiTimings struct {
	Timings
	// contains filtered or unexported fields
}

MultiTimings is meant to tracks timing data by categories as well as histograms. The names of the categories are compound names made with joining multiple strings with '.'.

func NewMultiTimings

func NewMultiTimings(name string, help string, labels []string) *MultiTimings

NewMultiTimings creates a new MultiTimings object.

func (*MultiTimings) Add

func (mt *MultiTimings) Add(names []string, elapsed time.Duration)

Add will add a new value to the named histogram.

func (*MultiTimings) Cutoffs

func (mt *MultiTimings) Cutoffs() []int64

Cutoffs returns the cutoffs used in the component histograms. Do not change the returned slice.

func (*MultiTimings) Labels

func (mt *MultiTimings) Labels() []string

Labels returns descriptions of the parts of each compound category name.

func (*MultiTimings) Record

func (mt *MultiTimings) Record(names []string, startTime time.Time)

Record is a convenience function that records completion timing data based on the provided start time of an event.

type MultiTracker

type MultiTracker interface {
	CountTracker
	Labels() []string
}

MultiTracker is a CountTracker that tracks counts grouping them by more than one dimension.

type NewVarHook

type NewVarHook func(name string, v expvar.Var)

NewVarHook is the type of a hook to export variables in a different way

type PushBackend

type PushBackend interface {
	// PushAll pushes all stats from expvar to the backend
	PushAll() error
}

PushBackend is an interface for any stats/metrics backend that requires data to be pushed to it. It's used to support push-based metrics backends, as expvar by default only supports pull-based ones.

type Rates

type Rates struct {
	// contains filtered or unexported fields
}

Rates is capable of reporting the rate (typically QPS) for any variable that satisfies the CountTracker interface.

func NewRates

func NewRates(name string, countTracker CountTracker, samples int, interval time.Duration) *Rates

NewRates reports rolling rate information for countTracker. samples specifies the number of samples to report, and interval specifies the time interval between samples. The minimum interval is 1 second. If passing the special value of -1s as interval, we don't snapshot. (use this for tests).

func (*Rates) Get

func (rt *Rates) Get() (rateMap map[string][]float64)

Get returns for each category (string) its latest rates (up to X values where X is the configured number of samples of the Rates struct). Rates are ordered from least recent (index 0) to most recent (end of slice).

func (*Rates) String

func (rt *Rates) String() string

func (*Rates) TotalRate

func (rt *Rates) TotalRate() float64

TotalRate returns the current total rate (counted across categories).

type RatesFunc

type RatesFunc struct {
	F func() map[string][]float64
	// contains filtered or unexported fields
}

func NewRateFunc

func NewRateFunc(name string, help string, f func() map[string][]float64) *RatesFunc

func (*RatesFunc) Help

func (rf *RatesFunc) Help() string

func (*RatesFunc) String

func (rf *RatesFunc) String() string

type RingInt64

type RingInt64 struct {
	// contains filtered or unexported fields
}

Ring of int64 values Not thread safe

func NewRingInt64

func NewRingInt64(capacity int) *RingInt64

func (*RingInt64) Add

func (ri *RingInt64) Add(val int64)

func (*RingInt64) Values

func (ri *RingInt64) Values() (values []int64)

type String

type String struct {
	// contains filtered or unexported fields
}

String is expvar.String+Get+hook

func NewString

func NewString(name string) *String

NewString returns a new String

func (*String) Get

func (v *String) Get() string

Get returns the value

func (*String) Set

func (v *String) Set(value string)

Set sets the value

func (*String) String

func (v *String) String() string

String is the implementation of expvar.var

type StringFunc

type StringFunc func() string

StringFunc converts a function that returns an string as an expvar.

func (StringFunc) String

func (f StringFunc) String() string

String is the implementation of expvar.var

type StringMapFunc

type StringMapFunc func() map[string]string

StringMapFunc is the function equivalent of StringMap

func (StringMapFunc) String

func (f StringMapFunc) String() string

String is used by expvar.

type Timings

type Timings struct {
	// contains filtered or unexported fields
}

Timings is meant to tracks timing data by named categories as well as histograms.

func NewTimings

func NewTimings(name, help, label string, categories ...string) *Timings

NewTimings creates a new Timings object, and publishes it if name is set. categories is an optional list of categories to initialize to 0. Categories that aren't initialized will be missing from the map until the first time they are updated.

func (*Timings) Add

func (t *Timings) Add(name string, elapsed time.Duration)

Add will add a new value to the named histogram.

func (*Timings) Count

func (t *Timings) Count() int64

Count returns the total count for all values.

func (*Timings) Counts

func (t *Timings) Counts() map[string]int64

Counts returns the total count for each value.

func (*Timings) Cutoffs

func (t *Timings) Cutoffs() []int64

Cutoffs returns the cutoffs used in the component histograms. Do not change the returned slice.

func (*Timings) Help

func (t *Timings) Help() string

Help returns the help string.

func (*Timings) Histograms

func (t *Timings) Histograms() (h map[string]*Histogram)

Histograms returns a map pointing at the histograms.

func (*Timings) Label

func (t *Timings) Label() string

Label returns the label name.

func (*Timings) Record

func (t *Timings) Record(name string, startTime time.Time)

Record is a convenience function that records completion timing data based on the provided start time of an event.

func (*Timings) Reset added in v0.8.0

func (t *Timings) Reset()

Reset will clear histograms: used during testing

func (*Timings) String

func (t *Timings) String() string

String is for expvar.

func (*Timings) Time

func (t *Timings) Time() int64

Time returns the total time elapsed for all values.

type Variable

type Variable interface {
	// Help returns the description of the variable.
	Help() string

	// String must implement String() from the expvar.Var interface.
	String() string
}

Variable is the minimal interface which each type in this "stats" package must implement. When integrating the Vitess stats types ("variables") with the different monitoring systems, you can rely on this interface.

Directories

Path Synopsis
Package opentsdb adds support for pushing stats to opentsdb.
Package opentsdb adds support for pushing stats to opentsdb.

Jump to

Keyboard shortcuts

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