# README
metric
import "github.com/ccheers/xpkg/stat/metric"
Index
- func Avg(iterator Iterator) float64
- func Count(iterator Iterator) float64
- func Max(iterator Iterator) float64
- func Min(iterator Iterator) float64
- func Sum(iterator Iterator) float64
- type Aggregation
- type Bucket
- type Counter
- type CounterOpts
- type CounterVec
- type CounterVecOpts
- type Gauge
- type GaugeOpts
- type GaugeVec
- type GaugeVecOpts
- type HistogramVec
- type HistogramVecOpts
- type Iterator
- type Metric
- type Opts
- type PointGauge
- type PointGaugeOpts
- type PointPolicy
- type RollingCounter
- type RollingCounterOpts
- type RollingGauge
- type RollingGaugeOpts
- type RollingPolicy
- type RollingPolicyOpts
- type VectorOpts
- type Window
- func NewWindow(opts WindowOpts) *Window
- func (w *Window) Add(offset int, val float64)
- func (w *Window) Append(offset int, val float64)
- func (w *Window) Bucket(offset int) Bucket
- func (w *Window) Iterator(offset int, count int) Iterator
- func (w *Window) ResetBucket(offset int)
- func (w *Window) ResetBuckets(offsets []int)
- func (w *Window) ResetWindow()
- func (w *Window) Size() int
- type WindowOpts
func Avg
func Avg(iterator Iterator) float64
Avg the values within the window.
func Count
func Count(iterator Iterator) float64
Count sums the count value within the window.
func Max
func Max(iterator Iterator) float64
Max the values within the window.
func Min
func Min(iterator Iterator) float64
Min the values within the window.
func Sum
func Sum(iterator Iterator) float64
Sum the values within the window.
type Aggregation
Aggregation contains some common pipeline function. Each pipeline can compute summary statistics of window.
type Aggregation interface {
// Min finds the min value within the window.
Min() float64
// Max finds the max value within the window.
Max() float64
// Avg computes average value within the window.
Avg() float64
// Sum computes sum value within the window.
Sum() float64
}
type Bucket
Bucket contains multiple float64 points.
type Bucket struct {
Points []float64
Count int64
// contains filtered or unexported fields
}
func (*Bucket) Add
func (b *Bucket) Add(offset int, val float64)
Add adds the given value to the point.
func (*Bucket) Append
func (b *Bucket) Append(val float64)
Append appends the given value to the bucket.
func (*Bucket) Next
func (b *Bucket) Next() *Bucket
Next returns the next bucket.
func (*Bucket) Reset
func (b *Bucket) Reset()
Reset empties the bucket.
type Counter
Counter stores a numerical value that only ever goes up.
type Counter interface {
Metric
}
func NewCounter
func NewCounter(opts CounterOpts) Counter
NewCounter creates a new Counter based on the CounterOpts.
type CounterOpts
CounterOpts is an alias of Opts.
type CounterOpts Opts
type CounterVec
CounterVec counter vec.
type CounterVec interface {
// Inc increments the counter by 1. Use Add to increment it by arbitrary
// non-negative values.
Inc(labels ...string)
// Add adds the given value to the counter. It panics if the value is <
// 0.
Add(v float64, labels ...string)
}
func NewBusinessMetricCount
func NewBusinessMetricCount(name string, labels ...string) CounterVec
NewBusinessMetricCount business Metric count vec. name or labels should not be empty.
func NewCounterVec
func NewCounterVec(cfg *CounterVecOpts) CounterVec
NewCounterVec .
type CounterVecOpts
CounterVecOpts is an alias of VectorOpts.
type CounterVecOpts VectorOpts
type Gauge
Gauge stores a numerical value that can be add arbitrarily.
type Gauge interface {
Metric
// Sets sets the value to the given number.
Set(int64)
}
func NewGauge
func NewGauge(opts GaugeOpts) Gauge
NewGauge creates a new Gauge based on the GaugeOpts.
type GaugeOpts
GaugeOpts is an alias of Opts.
type GaugeOpts Opts
type GaugeVec
GaugeVec gauge vec.
type GaugeVec interface {
// Set sets the Gauge to an arbitrary value.
Set(v float64, labels ...string)
// Inc increments the Gauge by 1. Use Add to increment it by arbitrary
// values.
Inc(labels ...string)
// Add adds the given value to the Gauge. (The value can be negative,
// resulting in a decrease of the Gauge.)
Add(v float64, labels ...string)
}
func NewBusinessMetricGauge
func NewBusinessMetricGauge(name string, labels ...string) GaugeVec
NewBusinessMetricGauge business Metric gauge vec. name or labels should not be empty.
func NewGaugeVec
func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec
NewGaugeVec .
type GaugeVecOpts
GaugeVecOpts is an alias of VectorOpts.
type GaugeVecOpts VectorOpts
type HistogramVec
HistogramVec gauge vec.
type HistogramVec interface {
// Observe adds a single observation to the histogram.
Observe(v int64, labels ...string)
}
func NewBusinessMetricHistogram
func NewBusinessMetricHistogram(name string, buckets []float64, labels ...string) HistogramVec
NewBusinessMetricHistogram business Metric histogram vec. name or labels should not be empty.
func NewHistogramVec
func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec
NewHistogramVec new a histogram vec.
type HistogramVecOpts
HistogramVecOpts is histogram vector opts.
type HistogramVecOpts struct {
Namespace string
Subsystem string
Name string
Help string
Labels []string
Buckets []float64
}
type Iterator
Iterator iterates the buckets within the window.
type Iterator struct {
// contains filtered or unexported fields
}
func (*Iterator) Bucket
func (i *Iterator) Bucket() Bucket
Bucket gets current bucket.
func (*Iterator) Next
func (i *Iterator) Next() bool
Next returns true util all of the buckets has been iterated.
type Metric
Metric is a sample interface. Implementations of Metrics in metric package are Counter, Gauge, PointGauge, RollingCounter and RollingGauge.
type Metric interface {
// Add adds the given value to the counter.
Add(int64)
// Value gets the current value.
// If the metric's type is PointGauge, RollingCounter, RollingGauge,
// it returns the sum value within the window.
Value() int64
}
type Opts
Opts contains the common arguments for creating Metric.
type Opts struct{}
type PointGauge
PointGauge represents a ring window. Every buckets within the window contains one point. When the window is full, the earliest point will be overwrite.
type PointGauge interface {
Aggregation
Metric
// Reduce applies the reduction function to all buckets within the window.
Reduce(func(Iterator) float64) float64
}
func NewPointGauge
func NewPointGauge(opts PointGaugeOpts) PointGauge
NewPointGauge creates a new PointGauge based on PointGaugeOpts.
type PointGaugeOpts
PointGaugeOpts contains the arguments for creating PointGauge.
type PointGaugeOpts struct {
// Size represents the bucket size within the window.
Size int
}
type PointPolicy
PointPolicy is a policy of points within the window. PointPolicy wraps the window and make it seem like ring-buf. When using PointPolicy, every buckets within the windows contains at more one point. e.g. [[1], [2], [3]]
type PointPolicy struct {
// contains filtered or unexported fields
}
func NewPointPolicy
func NewPointPolicy(window *Window) *PointPolicy
NewPointPolicy creates a new PointPolicy.
func (*PointPolicy) Append
func (p *PointPolicy) Append(val float64)
Append appends the given points to the window.
func (*PointPolicy) Reduce
func (p *PointPolicy) Reduce(f func(Iterator) float64) float64
Reduce applies the reduction function to all buckets within the window.
type RollingCounter
RollingCounter represents a ring window based on time duration. e.g. [[1], [3], [5]]
type RollingCounter interface {
Metric
Aggregation
Timespan() int
// Reduce applies the reduction function to all buckets within the window.
Reduce(func(Iterator) float64) float64
}
func NewRollingCounter
func NewRollingCounter(opts RollingCounterOpts) RollingCounter
NewRollingCounter creates a new RollingCounter bases on RollingCounterOpts.
type RollingCounterOpts
RollingCounterOpts contains the arguments for creating RollingCounter.
type RollingCounterOpts struct {
Size int
BucketDuration time.Duration
}
type RollingGauge
RollingGauge represents a ring window based on time duration. e.g. [[1, 2], [1, 2, 3], [1,2, 3, 4]]
type RollingGauge interface {
Metric
Aggregation
// Reduce applies the reduction function to all buckets within the window.
Reduce(func(Iterator) float64) float64
}
func NewRollingGauge
func NewRollingGauge(opts RollingGaugeOpts) RollingGauge
NewRollingGauge creates a new RollingGauge baseed on RollingGaugeOpts.
type RollingGaugeOpts
RollingGaugeOpts contains the arguments for creating RollingGauge.
type RollingGaugeOpts struct {
Size int
BucketDuration time.Duration
}
type RollingPolicy
RollingPolicy is a policy for ring window based on time duration. RollingPolicy moves bucket offset with time duration. e.g. If the last point is appended one bucket duration ago, RollingPolicy will increment current offset.
type RollingPolicy struct {
// contains filtered or unexported fields
}
func NewRollingPolicy
func NewRollingPolicy(window *Window, opts RollingPolicyOpts) *RollingPolicy
NewRollingPolicy creates a new RollingPolicy based on the given window and RollingPolicyOpts.
func (*RollingPolicy) Add
func (r *RollingPolicy) Add(val float64)
Add adds the given value to the latest point within bucket.
func (*RollingPolicy) Append
func (r *RollingPolicy) Append(val float64)
Append appends the given points to the window.
func (*RollingPolicy) Reduce
func (r *RollingPolicy) Reduce(f func(Iterator) float64) (val float64)
Reduce applies the reduction function to all buckets within the window.
type RollingPolicyOpts
RollingPolicyOpts contains the arguments for creating RollingPolicy.
type RollingPolicyOpts struct {
BucketDuration time.Duration
}
type VectorOpts
VectorOpts contains the common arguments for creating vec Metric..
type VectorOpts struct {
Namespace string
Subsystem string
Name string
Help string
Labels []string
}
type Window
Window contains multiple buckets.
type Window struct {
// contains filtered or unexported fields
}
func NewWindow
func NewWindow(opts WindowOpts) *Window
NewWindow creates a new Window based on WindowOpts.
func (*Window) Add
func (w *Window) Add(offset int, val float64)
Add adds the given value to the latest point within bucket where index equals the given offset.
func (*Window) Append
func (w *Window) Append(offset int, val float64)
Append appends the given value to the bucket where index equals the given offset.
func (*Window) Bucket
func (w *Window) Bucket(offset int) Bucket
Bucket returns the bucket where index equals the given offset.
func (*Window) Iterator
func (w *Window) Iterator(offset int, count int) Iterator
Iterator returns the bucket iterator.
func (*Window) ResetBucket
func (w *Window) ResetBucket(offset int)
ResetBucket empties the bucket based on the given offset.
func (*Window) ResetBuckets
func (w *Window) ResetBuckets(offsets []int)
ResetBuckets empties the buckets based on the given offsets.
func (*Window) ResetWindow
func (w *Window) ResetWindow()
ResetWindow empties all buckets within the window.
func (*Window) Size
func (w *Window) Size() int
Size returns the size of the window.
type WindowOpts
WindowOpts contains the arguments for creating Window.
type WindowOpts struct {
Size int
}
Generated by gomarkdoc