Documentation ¶
Overview ¶
Fork of rcrowley's Metrics library
<https://github.com/rcrowley/go-metrics>
Metrics packages have been simplified to be easier to use. Additional behaviour has been added to metrics types, as well as the inclusion of a Text type.
Example ¶
// nolint package main import ( "encoding/json" "fmt" "time" ) type NestedData struct { ValueOne int ValueTwo int } type ExistingJson struct { Sample NestedData Data NestedData Value int } func main() { registry := NewRegistry() c := NewRegisteredCounter("foo", registry) c.Inc(3) c.Dec(1) c.Inc(6) c2 := NewRegisteredCounter("bar", registry) c2.Inc(31) c2.Dec(14) c2.Inc(66) cx := registry.Get("foo").(Counter) cx.Inc(1) t1 := NewRegisteredText("hello", registry) t1.Set("Error: ") t1.Append("did not hello world") m1 := NewRegisteredMeter("world", registry) m1.Mark(50) m1.Mark(100) q1 := NewRegisteredTimer("golang", registry) q1.Start() time.Sleep(time.Second * 1) q1.Stop() q1.Time(func() { time.Sleep(time.Second * 4) }) nestedRegistry := NewRegistry() NewRegisteredText("msg", nestedRegistry).Set("This is a nested registry") NewRegisteredCounter("count", nestedRegistry).Inc(1996) registry.Register("registry2", nestedRegistry) s := NewSlice() for i := 0; i < 5; i++ { p := NewRegistry() NewRegisteredText("msg", p).Set("This is a slice entry") NewRegisteredCounter("count", p).Inc(int64(i)) s.Append(p) } registry.Register("sliceReg", s) data := ExistingJson{ Sample: NestedData{ ValueOne: 1, ValueTwo: 2, }, Data: NestedData{ ValueOne: 3, ValueTwo: 4, }, Value: 5, } raw, _ := json.Marshal(&data) NewRegisteredJson("ExistingJson", registry).Set(raw) js, err := registry.GetAllJson() if err != nil { panic(err) } fmt.Println(string(js)) }
Output: {"ExistingJson":{"Sample":{"ValueOne":1,"ValueTwo":2},"Data":{"ValueOne":3,"ValueTwo":4},"Value":5},"bar":83,"foo":9,"golang":{"count":2,"executions":[1,4],"lastValue":4,"max":4,"mean":2.5,"min":1},"hello":"Error: did not hello world","registry2":{"count":1996,"msg":"This is a nested registry"},"sliceReg":[{"count":0,"msg":"This is a slice entry"},{"count":1,"msg":"This is a slice entry"},{"count":2,"msg":"This is a slice entry"},{"count":3,"msg":"This is a slice entry"},{"count":4,"msg":"This is a slice entry"}],"world":{"count":2,"lastValue":100,"mean":75}}
Index ¶
- func SampleMax(values []int64) int64
- func SampleMean(values []int64) float64
- func SampleMin(values []int64) int64
- func SamplePercentile(values int64Slice, p float64) float64
- func SamplePercentiles(values int64Slice, ps []float64) []float64
- func SampleStdDev(values []int64) float64
- func SampleSum(values []int64) int64
- func SampleVariance(values []int64) float64
- type Counter
- type DuplicateMetric
- type ExpDecaySample
- func (s *ExpDecaySample) Clear()
- func (s *ExpDecaySample) Count() int64
- func (s *ExpDecaySample) Max() int64
- func (s *ExpDecaySample) Mean() float64
- func (s *ExpDecaySample) Min() int64
- func (s *ExpDecaySample) Percentile(p float64) float64
- func (s *ExpDecaySample) Percentiles(ps []float64) []float64
- func (s *ExpDecaySample) Size() int
- func (s *ExpDecaySample) StdDev() float64
- func (s *ExpDecaySample) Sum() int64
- func (s *ExpDecaySample) Update(v int64)
- func (s *ExpDecaySample) Values() []int64
- func (s *ExpDecaySample) Variance() float64
- type Histogram
- type Json
- type Meter
- type MeterSnapshot
- type Registry
- type Sample
- type Slice
- type StandardCounter
- type StandardHistogram
- func (h *StandardHistogram) Clear()
- func (h *StandardHistogram) Count() int64
- func (h *StandardHistogram) Max() int64
- func (h *StandardHistogram) Mean() float64
- func (h *StandardHistogram) Min() int64
- func (h *StandardHistogram) Percentile(p float64) float64
- func (h *StandardHistogram) Percentiles(ps []float64) []float64
- func (h *StandardHistogram) Sample() Sample
- func (h *StandardHistogram) StdDev() float64
- func (h *StandardHistogram) Sum() int64
- func (h *StandardHistogram) Update(v int64)
- func (h *StandardHistogram) Variance() float64
- type StandardJson
- type StandardMeter
- type StandardRegistry
- func (r *StandardRegistry) Each(f func(string, interface{}))
- func (r *StandardRegistry) Get(name string) interface{}
- func (r *StandardRegistry) GetAllJson() ([]byte, error)
- func (r *StandardRegistry) MetricCount() int
- func (r *StandardRegistry) Register(name string, i interface{}) error
- func (r *StandardRegistry) Unregister(name string)
- type StandardSlice
- type StandardText
- type StandardTimer
- func (t *StandardTimer) AllExecutions() []float64
- func (t *StandardTimer) Count() int64
- func (t *StandardTimer) LastValue() float64
- func (t *StandardTimer) Max() int64
- func (t *StandardTimer) Mean() float64
- func (t *StandardTimer) Min() int64
- func (t *StandardTimer) Start()
- func (t *StandardTimer) Stop()
- func (t *StandardTimer) Time(f func())
- type Text
- type Timer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SampleMean ¶
SampleMean returns the mean value of the slice of int64.
func SamplePercentile ¶
SamplePercentiles returns an arbitrary percentile of the slice of int64.
func SamplePercentiles ¶
SamplePercentiles returns a slice of arbitrary percentiles of the slice of int64.
func SampleStdDev ¶
SampleStdDev returns the standard deviation of the slice of int64.
func SampleVariance ¶
SampleVariance returns the variance of the slice of int64.
Types ¶
type Counter ¶
Counters hold an int64 value that can be incremented and decremented.
func GetCounter ¶
GetCounter returns an existing Counter
func NewRegisteredCounter ¶
NewRegisteredCounter constructs and registers a new StandardCounter.
type DuplicateMetric ¶
type DuplicateMetric string
DuplicateMetric is the error returned by Registry.Register when a metric already exists. If you mean to Register that metric you must first Unregister the existing metric.
func (DuplicateMetric) Error ¶
func (err DuplicateMetric) Error() string
type ExpDecaySample ¶
type ExpDecaySample struct {
// contains filtered or unexported fields
}
ExpDecaySample is an exponentially-decaying sample using a forward-decaying priority reservoir. See Cormode et al's "Forward Decay: A Practical Time Decay Model for Streaming Systems".
<http://dimacs.rutgers.edu/~graham/pubs/papers/fwddecay.pdf>
func (*ExpDecaySample) Count ¶
func (s *ExpDecaySample) Count() int64
Count returns the number of samples recorded, which may exceed the reservoir size.
func (*ExpDecaySample) Max ¶
func (s *ExpDecaySample) Max() int64
Max returns the maximum value in the sample, which may not be the maximum value ever to be part of the sample.
func (*ExpDecaySample) Mean ¶
func (s *ExpDecaySample) Mean() float64
Mean returns the mean of the values in the sample.
func (*ExpDecaySample) Min ¶
func (s *ExpDecaySample) Min() int64
Min returns the minimum value in the sample, which may not be the minimum value ever to be part of the sample.
func (*ExpDecaySample) Percentile ¶
func (s *ExpDecaySample) Percentile(p float64) float64
Percentile returns an arbitrary percentile of values in the sample.
func (*ExpDecaySample) Percentiles ¶
func (s *ExpDecaySample) Percentiles(ps []float64) []float64
Percentiles returns a slice of arbitrary percentiles of values in the sample.
func (*ExpDecaySample) Size ¶
func (s *ExpDecaySample) Size() int
Size returns the size of the sample, which is at most the reservoir size.
func (*ExpDecaySample) StdDev ¶
func (s *ExpDecaySample) StdDev() float64
StdDev returns the standard deviation of the values in the sample.
func (*ExpDecaySample) Sum ¶
func (s *ExpDecaySample) Sum() int64
Sum returns the sum of the values in the sample.
func (*ExpDecaySample) Update ¶
func (s *ExpDecaySample) Update(v int64)
Update samples a new value.
func (*ExpDecaySample) Values ¶
func (s *ExpDecaySample) Values() []int64
Values returns a copy of the values in the sample.
func (*ExpDecaySample) Variance ¶
func (s *ExpDecaySample) Variance() float64
Variance returns the variance of the values in the sample.
type Histogram ¶
type Histogram interface { Clear() Count() int64 Max() int64 Mean() float64 Min() int64 Percentile(float64) float64 Percentiles([]float64) []float64 Sample() Sample StdDev() float64 Sum() int64 Update(int64) Variance() float64 }
Histograms calculate distribution statistics from a series of int64 values.
func GetHistogram ¶
GetHistogram returns an existing Counter
func NewHistogram ¶
NewHistogram constructs a new StandardHistogram from a Sample.
type Json ¶
type Json interface { Clear() Json() json.RawMessage // Get the current msg value Set(json.RawMessage) // Set a new msg value }
Json is a basic string message that can be set and appended to
func NewRegisteredJson ¶
NewRegisteredCounter constructs and registers a new StandardJson.
type Meter ¶
type Meter interface { Count() int64 // Number of times the meter has saved a value Mark(int64) // Set a value in the meter RateMean() float64 // Get the mean value from the meter LastValue() int64 // Get the most recently saved value Snapshot() Meter // Save a snapshot of the current state }
Meters track set values and how the value changes over time each time it is recorded
func NewMeter ¶
func NewMeter() Meter
NewMeter constructs a new StandardMeter and launches a goroutine. Be sure to call Stop() once the meter is of no use to allow for garbage collection.
func NewRegisteredMeter ¶
NewMeter constructs and registers a new StandardMeter and launches a goroutine. Be sure to unregister the meter from the registry once it is of no use to allow for garbage collection.
type MeterSnapshot ¶
type MeterSnapshot struct {
// contains filtered or unexported fields
}
MeterSnapshot is a read-only copy of another Meter.
func (*MeterSnapshot) Count ¶
func (m *MeterSnapshot) Count() int64
Count returns the count of events at the time the snapshot was taken.
func (*MeterSnapshot) LastValue ¶
func (m *MeterSnapshot) LastValue() int64
LastValue returns the last recorded value on the meter
func (*MeterSnapshot) RateMean ¶
func (m *MeterSnapshot) RateMean() float64
RateMean returns the meter's mean rate of events per second at the time the snapshot was taken.
func (*MeterSnapshot) Snapshot ¶
func (m *MeterSnapshot) Snapshot() Meter
Snapshot returns the snapshot.
type Registry ¶
type Registry interface { // Call the given function for each registered metric. Each(func(string, interface{})) // Get the metric by the given name or nil if none is registered. Get(string) interface{} // Output the value of all metrics in JSON GetAllJson() ([]byte, error) // Register the given metric under the given name. Register(string, interface{}) error // Unregister the metric with the given name. Unregister(string) // Get the number of tracked metrics MetricCount() int }
A Registry holds references to a set of metrics by name and can iterate over them, calling callback functions provided by the user.
This is an interface so as to encourage other structs to implement the Registry API as appropriate.
var DefaultRegistry Registry = NewRegistry()
Default registry is none is specified
type Sample ¶
type Sample interface { Clear() Count() int64 Max() int64 Mean() float64 Min() int64 Percentile(float64) float64 Percentiles([]float64) []float64 Size() int StdDev() float64 Sum() int64 Update(int64) Values() []int64 Variance() float64 }
Samples maintain a statistically-significant selection of values from a stream.
func NewExpDecaySample ¶
NewExpDecaySample constructs a new exponentially-decaying sample with the given reservoir size and alpha.
type Slice ¶
Counters hold an int64 value that can be incremented and decremented.
func NewRegisteredSlice ¶
NewRegisteredSlice constructs and registers a new StandardCounter.
type StandardCounter ¶
type StandardCounter struct {
// contains filtered or unexported fields
}
StandardCounter is the standard implementation of a Counter and uses the sync/atomic package to manage a single int64 value.
func (*StandardCounter) Count ¶
func (c *StandardCounter) Count() int64
Count returns the current count.
func (*StandardCounter) Dec ¶
func (c *StandardCounter) Dec(i int64)
Dec decrements the counter by the given amount.
func (*StandardCounter) Inc ¶
func (c *StandardCounter) Inc(i int64)
Inc increments the counter by the given amount.
func (*StandardCounter) Set ¶
func (c *StandardCounter) Set(i int64)
Set changes the counter to the given value.
type StandardHistogram ¶
type StandardHistogram struct {
// contains filtered or unexported fields
}
StandardHistogram is the standard implementation of a Histogram and uses a Sample to bound its memory use.
func (*StandardHistogram) Clear ¶
func (h *StandardHistogram) Clear()
Clear clears the histogram and its sample.
func (*StandardHistogram) Count ¶
func (h *StandardHistogram) Count() int64
Count returns the number of samples recorded since the histogram was last cleared.
func (*StandardHistogram) Max ¶
func (h *StandardHistogram) Max() int64
Max returns the maximum value in the sample.
func (*StandardHistogram) Mean ¶
func (h *StandardHistogram) Mean() float64
Mean returns the mean of the values in the sample.
func (*StandardHistogram) Min ¶
func (h *StandardHistogram) Min() int64
Min returns the minimum value in the sample.
func (*StandardHistogram) Percentile ¶
func (h *StandardHistogram) Percentile(p float64) float64
Percentile returns an arbitrary percentile of the values in the sample.
func (*StandardHistogram) Percentiles ¶
func (h *StandardHistogram) Percentiles(ps []float64) []float64
Percentiles returns a slice of arbitrary percentiles of the values in the sample.
func (*StandardHistogram) Sample ¶
func (h *StandardHistogram) Sample() Sample
Sample returns the Sample underlying the histogram.
func (*StandardHistogram) StdDev ¶
func (h *StandardHistogram) StdDev() float64
StdDev returns the standard deviation of the values in the sample.
func (*StandardHistogram) Sum ¶
func (h *StandardHistogram) Sum() int64
Sum returns the sum in the sample.
func (*StandardHistogram) Update ¶
func (h *StandardHistogram) Update(v int64)
Update samples a new value.
func (*StandardHistogram) Variance ¶
func (h *StandardHistogram) Variance() float64
Variance returns the variance of the values in the sample.
type StandardJson ¶
type StandardJson struct {
// contains filtered or unexported fields
}
StandardJson is the standard implementation of a Json value
func (*StandardJson) Json ¶
func (t *StandardJson) Json() json.RawMessage
Json returns the msg value
func (*StandardJson) Set ¶
func (t *StandardJson) Set(j json.RawMessage)
Set changes the msg value to the indicated string
type StandardMeter ¶
type StandardMeter struct {
// contains filtered or unexported fields
}
StandardMeter is the standard implementation of a Meter.
func (*StandardMeter) Count ¶
func (m *StandardMeter) Count() int64
Count returns the number of events recorded.
func (*StandardMeter) LastValue ¶
func (m *StandardMeter) LastValue() int64
LastValue returns the last recorded value on the meter
func (*StandardMeter) Mark ¶
func (m *StandardMeter) Mark(n int64)
Mark records the occurance of n events.
func (*StandardMeter) RateMean ¶
func (m *StandardMeter) RateMean() float64
RateMean returns the meter's mean rate of events per second.
func (*StandardMeter) Snapshot ¶
func (m *StandardMeter) Snapshot() Meter
Snapshot returns a read-only copy of the meter.
type StandardRegistry ¶
type StandardRegistry struct {
// contains filtered or unexported fields
}
The standard implementation of a Registry is a mutex-protected map of names to metrics.
func (*StandardRegistry) Each ¶
func (r *StandardRegistry) Each(f func(string, interface{}))
Call the given function for each registered metric.
func (*StandardRegistry) Get ¶
func (r *StandardRegistry) Get(name string) interface{}
Get the metric by the given name or nil if none is registered.
func (*StandardRegistry) GetAllJson ¶
func (r *StandardRegistry) GetAllJson() ([]byte, error)
Output the value of all registered metrics in JSON format
func (*StandardRegistry) MetricCount ¶
func (r *StandardRegistry) MetricCount() int
Get the number of tracked metrics
func (*StandardRegistry) Register ¶
func (r *StandardRegistry) Register(name string, i interface{}) error
Register the given metric under the given name. Returns a DuplicateMetric if a metric by the given name is already registered.
func (*StandardRegistry) Unregister ¶
func (r *StandardRegistry) Unregister(name string)
Unregister the metric with the given name.
type StandardSlice ¶
type StandardSlice struct {
// contains filtered or unexported fields
}
StandardSlice is the standard implementation of a Slice metrics set
func (*StandardSlice) Append ¶
func (s *StandardSlice) Append(r Registry)
Append a registry onto the slice
func (*StandardSlice) GetAll ¶
func (s *StandardSlice) GetAll() []Registry
Return all included registries
type StandardText ¶
type StandardText struct {
// contains filtered or unexported fields
}
StandardText is the standard implementation of a text value
func (*StandardText) Append ¶
func (t *StandardText) Append(str string)
Append adds the indicated string to the end of the msg value
func (*StandardText) Set ¶
func (t *StandardText) Set(str string)
Set changes the msg value to the indicated string
type StandardTimer ¶
type StandardTimer struct {
// contains filtered or unexported fields
}
StandardTimer is the standard implementation of a Timer and uses a Histogram and Meter.
func (*StandardTimer) AllExecutions ¶
func (t *StandardTimer) AllExecutions() []float64
AllExecutions returns all the past executions
func (*StandardTimer) Count ¶
func (t *StandardTimer) Count() int64
Count returns the number of events recorded.
func (*StandardTimer) LastValue ¶
func (t *StandardTimer) LastValue() float64
LastValue returns the value from the most recent execution.
func (*StandardTimer) Max ¶
func (t *StandardTimer) Max() int64
Max returns the maximum value in the sample.
func (*StandardTimer) Mean ¶
func (t *StandardTimer) Mean() float64
Mean returns the mean of the values in the sample.
func (*StandardTimer) Min ¶
func (t *StandardTimer) Min() int64
Min returns the minimum value in the sample.
func (*StandardTimer) Start ¶
func (t *StandardTimer) Start()
Record the current time to prepare for a Stop() call
func (*StandardTimer) Stop ¶
func (t *StandardTimer) Stop()
Record the duration of an event that started with a call to Start() and ends now.
func (*StandardTimer) Time ¶
func (t *StandardTimer) Time(f func())
Record the duration of the execution of the given function.
type Text ¶
type Text interface { Clear() Text() string // Get the current msg value Set(string) // Set a new msg value Append(string) // Append to the end of the msg value }
Text is a basic string message that can be set and appended to
func NewRegisteredText ¶
NewRegisteredCounter constructs and registers a new StandardText.
type Timer ¶
type Timer interface { Count() int64 // Number of timer executions Max() int64 // Highest recorded timer execution in seconds Mean() float64 // Mean recorded timer execution in seconds Min() int64 // Lowest recorded timer execution in seconds LastValue() float64 // The value from the most recent execution in seconds AllExecutions() []float64 // All past executions in seconds Start() // Record current time Stop() // Record duration since Start() call Time(func()) // Record duration to execute a function }
Timers measures the time to complete a task and tracks the history of past executions
func NewRegisteredTimer ¶
NewRegisteredTimer constructs and registers a new StandardTimer. Be sure to unregister the meter from the registry once it is of no use to allow for garbage collection.