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
- func GetSnakeName(name string) string
- func IsDimensionCombined(name string) bool
- func Publish(name string, v expvar.Var)
- func PublishJSONFunc(name string, f func() string)
- func Register(nvh NewVarHook)
- func RegisterHistogramHook(hook func(string, int64))
- func RegisterPushBackend(name string, backend PushBackend)
- func RegisterTimerHook(hook func(string, string, int64, *Timings))
- type CountTracker
- type Counter
- type CounterDuration
- type CounterDurationFunc
- type CounterFunc
- type CountersFuncWithMultiLabels
- type CountersWithMultiLabels
- func (mc *CountersWithMultiLabels) Add(names []string, value int64)
- func (mc *CountersWithMultiLabels) Counts() map[string]int64
- func (c *CountersWithMultiLabels) Help() string
- func (mc *CountersWithMultiLabels) Labels() []string
- func (mc *CountersWithMultiLabels) Reset(names []string)
- func (mc *CountersWithMultiLabels) ResetAll()
- func (c *CountersWithMultiLabels) String() string
- func (c *CountersWithMultiLabels) ZeroAll()
- type CountersWithSingleLabel
- func (c *CountersWithSingleLabel) Add(name string, value int64)
- func (c *CountersWithSingleLabel) Counts() map[string]int64
- func (c *CountersWithSingleLabel) Help() string
- func (c *CountersWithSingleLabel) Label() string
- func (c *CountersWithSingleLabel) Reset(name string)
- func (c *CountersWithSingleLabel) ResetAll()
- func (c *CountersWithSingleLabel) String() string
- func (c *CountersWithSingleLabel) ZeroAll()
- type FloatFunc
- type Gauge
- type GaugeDuration
- type GaugeDurationFunc
- type GaugeFunc
- type GaugesFuncWithMultiLabels
- type GaugesWithMultiLabels
- type GaugesWithSingleLabel
- type Histogram
- func (h *Histogram) Add(value int64)
- func (h *Histogram) Buckets() []int64
- func (h *Histogram) Count() (count int64)
- func (h *Histogram) CountLabel() string
- func (h *Histogram) Counts() map[string]int64
- func (h *Histogram) Cutoffs() []int64
- func (h *Histogram) Help() string
- func (h *Histogram) Labels() []string
- func (h *Histogram) MarshalJSON() ([]byte, error)
- func (h *Histogram) String() string
- func (h *Histogram) Total() (total int64)
- func (h *Histogram) TotalLabel() string
- type JSONFunc
- type MultiTimings
- type MultiTracker
- type NewVarHook
- type PushBackend
- type Rates
- type RatesFunc
- type RingInt64
- type String
- type StringFunc
- type StringMapFunc
- type Timings
- func (t *Timings) Add(name string, elapsed time.Duration)
- func (t *Timings) Count() int64
- func (t *Timings) Counts() map[string]int64
- func (t *Timings) Cutoffs() []int64
- func (t *Timings) Help() string
- func (t *Timings) Histograms() (h map[string]*Histogram)
- func (t *Timings) Label() string
- func (t *Timings) Record(name string, startTime time.Time)
- func (t *Timings) Reset()
- func (t *Timings) String() string
- func (t *Timings) Time() int64
- type Variable
Constants ¶
const StatsAllStr = "all"
StatsAllStr is the consolidated name if a dimension gets combined.
Variables ¶
This section is empty.
Functions ¶
func GetSnakeName ¶
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 ¶
IsDimensionCombined returns true if the specified dimension should be combined.
func PublishJSONFunc ¶
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
RegisterHistogramHook registers timer hook
func RegisterPushBackend ¶
func RegisterPushBackend(name string, backend PushBackend)
RegisterPushBackend allows modules to register PushBackend implementations. Should be called on init().
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 ¶
NewCounter returns a new Counter.
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) 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) 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.
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 ¶
func (c CountersFuncWithMultiLabels) Help() string
Help returns the help string.
func (CountersFuncWithMultiLabels) Labels ¶
func (c CountersFuncWithMultiLabels) Labels() []string
Labels returns the list of labels.
func (CountersFuncWithMultiLabels) String ¶
func (c CountersFuncWithMultiLabels) String() 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
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) 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
type FloatFunc ¶
type FloatFunc func() float64
FloatFunc converts a function that returns a float64 as an expvar.
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.
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.
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.
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).
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) 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.
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 ¶
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) CountLabel ¶
CountLabel returns the count label that was set when this Histogram was created.
func (*Histogram) Counts ¶
Counts returns a map from labels to the current count in the Histogram for that label.
func (*Histogram) Cutoffs ¶
Cutoffs returns the cutoffs that were set when this Histogram was created.
func (*Histogram) MarshalJSON ¶
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 ¶
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 ¶
Total returns the sum of all values that have been added to this Histogram.
func (*Histogram) TotalLabel ¶
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.
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.
type MultiTracker ¶
type MultiTracker interface { CountTracker Labels() []string }
MultiTracker is a CountTracker that tracks counts grouping them by more than one dimension.
type NewVarHook ¶
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 ¶
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).
type RatesFunc ¶
func NewRateFunc ¶
type RingInt64 ¶
type RingInt64 struct {
// contains filtered or unexported fields
}
Ring of int64 values Not thread safe
func NewRingInt64 ¶
type String ¶
type String struct {
// contains filtered or unexported fields
}
String is expvar.String+Get+hook
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 ¶
StringMapFunc is the function equivalent of StringMap
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 ¶
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) Cutoffs ¶
Cutoffs returns the cutoffs used in the component histograms. Do not change the returned slice.
func (*Timings) Histograms ¶
Histograms returns a map pointing at the histograms.
func (*Timings) Record ¶
Record is a convenience function that records completion timing data based on the provided start time of an event.
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.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package opentsdb adds support for pushing stats to opentsdb.
|
Package opentsdb adds support for pushing stats to opentsdb. |