Documentation ¶
Overview ¶
Package metrics defines some common metrics, such as Counter, IGauge, ITimer and IHistogram. The method MetricsSink is used to adapt to external monitor systems, such as monitors in our company or open source prometheus.
For convenience, we provide two sorts of methods:
counter - reqNumCounter := metrics.Counter("req.num") reqNumCounter.Incr() - metrics.IncrCounter("req.num", 1)
gauge - cpuAvgLoad := metrics.Gauge("cpu.avgload") cpuAvgLoad.Set(0.75) - metrics.SetGauge("cpu.avgload", 0.75)
timer - timeCostTimer := metrics.Timer("req.proc.timecost") timeCostTimer.Record() - timeCostDuration := time.Millisecond * 2000 metrics.RecordTimer("req.proc.timecost", timeCostDuration)
histogram - buckets := metrics.NewDurationBounds(time.Second, time.Second*2, time.Second*5), timeCostDist := metrics.Histogram("timecost.distribution", buckets) timeCostDist.AddSample(float64(time.Second*3)) - metrics.AddSample("timecost.distribution", buckets, float64(time.Second*3))
Index ¶
- Constants
- func AddSample(key string, buckets BucketBounds, value float64)
- func IncrCounter(key string, value float64)
- func RecordTimer(key string, duration time.Duration)
- func RegisterHistogram(name string, o HistogramOption)
- func RegisterMetricsSink(sink Sink)
- func Report(rec Record, opts ...Option) (err error)
- func ReportMultiDimensionMetricsX(name string, dimensions []*Dimension, metrics []*Metrics, opts ...Option) error
- func ReportSingleDimensionMetrics(name string, value float64, policy Policy, opts ...Option) error
- func SetGauge(key string, value float64)
- type BucketBounds
- type ConsoleSink
- type Dimension
- type HistogramOption
- type HistogramSink
- type ICounter
- type IGauge
- type IHistogram
- type ITimer
- type Metrics
- type NoopSink
- type Option
- type Options
- type Policy
- type Record
- type Sink
Constants ¶
const ( PolicyNONE = 0 // Undefined PolicySET = 1 // instantaneous value PolicySUM = 2 // summary PolicyAVG = 3 // average PolicyMAX = 4 // maximum PolicyMIN = 5 // minimum PolicyMID = 6 // median PolicyTimer = 7 // timer PolicyHistogram = 8 // histogram )
All available Policy(s).
Variables ¶
This section is empty.
Functions ¶
func AddSample ¶
func AddSample(key string, buckets BucketBounds, value float64)
AddSample adds one sample key with value.
func IncrCounter ¶
IncrCounter increases counter key by value. Counters should accumulate values.
func RecordTimer ¶
RecordTimer records timer named key with duration.
func RegisterHistogram ¶
func RegisterHistogram(name string, o HistogramOption)
RegisterHistogram registers all named Histogram configurations to all Sink(s) which implement HistogramSink.
func RegisterMetricsSink ¶
func RegisterMetricsSink(sink Sink)
RegisterMetricsSink registers a Sink.
func ReportMultiDimensionMetricsX ¶
func ReportMultiDimensionMetricsX( name string, dimensions []*Dimension, metrics []*Metrics, opts ...Option, ) error
ReportMultiDimensionMetricsX creates and reports a named Record with multiple dimensions and metrics.
func ReportSingleDimensionMetrics ¶
ReportSingleDimensionMetrics creates and reports a Record with no dimension and only one metric.
Types ¶
type BucketBounds ¶
type BucketBounds []float64
BucketBounds allows developers to customize Buckets of histogram.
func NewDurationBounds ¶
func NewDurationBounds(durations ...time.Duration) BucketBounds
NewDurationBounds creates duration bounds.
func NewValueBounds ¶
func NewValueBounds(bounds ...float64) BucketBounds
NewValueBounds creates a value bounds.
func (BucketBounds) Less ¶
func (v BucketBounds) Less(i, j int) bool
Less implements sort.Interface.
type ConsoleSink ¶
type ConsoleSink struct {
// contains filtered or unexported fields
}
ConsoleSink defines the console sink.
type HistogramOption ¶
type HistogramOption struct {
BucketBounds BucketBounds
}
HistogramOption defines configurations when register a histogram.
type HistogramSink ¶
type HistogramSink interface {
Register(name string, o HistogramOption)
}
HistogramSink extends Sink in a way that allows to load a named Histogram configuration. Those who do not implement HistogramSink must define their own default bucket configuration.
type ICounter ¶
type ICounter interface { // Incr increments the counter by one. Incr() // IncrBy increments the counter by delta. IncrBy(delta float64) }
ICounter is the interface that emits counter type metrics.
type IGauge ¶
type IGauge interface { // Set sets the gauges absolute value. Set(value float64) }
IGauge is the interface that emits gauge metrics.
type IHistogram ¶
type IHistogram interface { // AddSample records a sample into histogram. AddSample(value float64) // GetBuckets get histogram buckets. GetBuckets() []*bucket }
IHistogram is the interface that emits histogram metrics.
func GetHistogram ¶
func GetHistogram(key string) (v IHistogram, ok bool)
GetHistogram gets the histogram by key.
func Histogram ¶
func Histogram(name string, buckets BucketBounds) IHistogram
Histogram creates a named histogram with buckets.
type ITimer ¶
type ITimer interface { // Record records the duration since timer.start, and reset timer.start. Record() time.Duration // RecordDuration records duration into timer, and reset timer.start. RecordDuration(duration time.Duration) // Reset resets the timer.start. Reset() }
ITimer is the interface that emits timer metrics.
type Metrics ¶
type Metrics struct {
// contains filtered or unexported fields
}
Metrics defines the metric.
func NewMetrics ¶
NewMetrics creates a new Metrics.
type Options ¶
type Options struct { // Meta is used to adapt some complex scenes. For example, a monitor may map the metric name to // monitor id. Meta map[string]interface{} }
Options defines the report options.
type Record ¶
type Record struct { Name string // the name of the record // contains filtered or unexported fields }
Record is the single record.
terminologies:
- dimension name is an attribute of a data, often used to filter data, such as a photo album business module includes region and server room.
- dimension value refines the dimension. For example, the regions of the album business module include Shenzhen, Shanghai, etc., the region is the dimension, and Shenzhen and Shanghai are the dimension values.
- metric is a measurement, used to aggregate and calculate. For example, request count of album business module in ShenZhen Telecom is a metric.
func NewMultiDimensionMetricsX ¶
NewMultiDimensionMetricsX creates a named Record with multiple dimensions and metrics.
func NewSingleDimensionMetrics ¶
NewSingleDimensionMetrics creates a Record with no dimension and only one metric.
func (*Record) GetDimensions ¶
GetDimensions returns dimensions.
type Sink ¶
type Sink interface { // Name returns the name of the monitor system. Name() string // Report reports a record to monitor system. Report(rec Record, opts ...Option) error }
Sink defines the interface an external monitor system should provide.
func GetMetricsSink ¶
GetMetricsSink gets a Sink by name