metrics

package module
v0.6.49 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 16 Imported by: 5

README

Coverage Status

metrics

This is a fork from https://github.com/armon/go-metrics

This library provides a metrics package which can be used to instrument code, expose application metrics, and profile runtime performance in a flexible manner.

The main difference from the original code, that the Provider interface does not have xxxWithValues and has only 4 methods.

// Provider basics
type Provider interface {
	SetGauge(key string, val float64, tags ...Tag)
	IncrCounter(key string, val float64, tags ...Tag)
	AddSample(key string, val float64, tags ...Tag)
	MeasureSince(key string, start time.Time, tags ...Tag)
}

We removed EmitKey method since it's not supported by Prometheus, which is the most popular provider.

We also removed AllowedLabels config, as it's hard to configure in production, and impacts the performance. Developers should design better which labels are published, to keep the cardinality on acceptable level.

Sinks

The metrics package makes use of a Sink interface to support delivery to any type of backend.

type Sink interface {
	// SetGauge should retain the last value it is set to
	SetGauge(key string, val float64, tags []Tag)
	// IncrCounter should accumulate values
	IncrCounter(key string, val float64, tags []Tag)
	// AddSample is for timing information, where quantiles are used
	AddSample(key string, val float64, tags []Tag)
}

Currently the following sinks are provided:

  • statsd.Sink: Sinks to a StatsD / statsite instance (UDP)
  • prometheus.Sink: Sinks to a Prometheus metrics endpoint (exposed via HTTP for scrapes)
  • InmemSink : Provides in-memory aggregation, can be used to export stats
  • FanoutSink : Sinks to multiple sinks. Enables writing to multiple statsite instances for example.
  • BlackholeSink : Sinks to nowhere

In addition to the sinks, the InmemSignal can be used to catch a signal, and dump a formatted output of recent metrics. For example, when a process gets a SIGUSR1, it can dump to stderr recent performance metrics for debugging.

Tags

The metrics methods allow to push metrics with tags (or labels) and use some features of underlying Sinks (ex: translated into Prometheus labels).

Examples

Here is an example of using the package:

func SlowMethod() {
    // Profiling the runtime of a method
    defer metrics.MeasureSince([]string{"SlowMethod"}, time.Now(), metrics.Tag{Name: "method", Value: mathod})
}

// Configure a statsite sink as the global metrics sink
sink, _ := metrics.NewStatsiteSink("statsite:8125")
metrics.NewGlobal(metrics.DefaultConfig("service-name"), sink)

Here is an example of setting up a signal handler:

// Setup the inmem sink and signal handler
inm := metrics.NewInmemSink(10*time.Second, time.Minute)
sig := metrics.DefaultInmemSignal(inm)
metrics.NewGlobal(metrics.DefaultConfig("service-name"), inm)

// Run some code
inm.SetGauge([]string{"foo"}, 42)

inm.IncrCounter([]string{"baz"}, 42)
inm.IncrCounter([]string{"baz"}, 1)
inm.IncrCounter([]string{"baz"}, 80)

inm.AddSample([]string{"method", "wow"}, 42)
inm.AddSample([]string{"method", "wow"}, 100)
inm.AddSample([]string{"method", "wow"}, 22)

....

When a signal comes in, output like the following will be dumped to stderr:

[2014-01-28 14:57:33.04 -0800 PST][G] 'foo': 42.000
[2014-01-28 14:57:33.04 -0800 PST][P] 'bar': 30.000
[2014-01-28 14:57:33.04 -0800 PST][C] 'baz': Count: 3 Min: 1.000 Mean: 41.000 Max: 80.000 Stddev: 39.509
[2014-01-28 14:57:33.04 -0800 PST][S] 'method.wow': Count: 3 Min: 22.000 Mean: 54.667 Max: 100.000 Stddev: 40.513

Documentation

Index

Constants

View Source
const (
	TypeCounter = "counter"
	TypeSample  = "sample"
	TypeGauge   = "gauge"
)

Define metrics type const

View Source
const (
	// DefaultSignal is used with DefaultInmemSignal
	DefaultSignal = syscall.SIGUSR1
)

Variables

This section is empty.

Functions

func AddSample

func AddSample(key string, val float64, tags ...Tag)

AddSample is for timing information, where quantiles are used

func IncrCounter

func IncrCounter(key string, val float64, tags ...Tag)

IncrCounter should accumulate values

func MeasureSince

func MeasureSince(key string, start time.Time, tags ...Tag)

MeasureSince is for timing information

func SetGauge

func SetGauge(key string, val float64, tags ...Tag)

SetGauge should retain the last value it is set to

func StringStartsWithOneOf added in v0.2.0

func StringStartsWithOneOf(value string, items []string) bool

StringStartsWithOneOf returns true if one of items slice is a prefix of specified value.

func UpdateFilter

func UpdateFilter(allow, block []string)

UpdateFilter updates filters

Types

type AggregateSample

type AggregateSample struct {
	Count       int       // The count of emitted pairs
	Rate        float64   // The values rate per time unit (usually 1 second)
	Sum         float64   // The sum of values
	SumSq       float64   `json:"-"` // The sum of squared values
	Min         float64   // Minimum value
	Max         float64   // Maximum value
	LastUpdated time.Time `json:"-"` // When value was last updated
}

AggregateSample is used to hold aggregate metrics about a sample

func (*AggregateSample) Ingest

func (a *AggregateSample) Ingest(v float64, rateDenom float64)

Ingest is used to update a sample

func (*AggregateSample) Mean

func (a *AggregateSample) Mean() float64

Mean computes a mean of the values

func (*AggregateSample) Stddev

func (a *AggregateSample) Stddev() float64

Stddev computes a Stddev of the values

func (*AggregateSample) String

func (a *AggregateSample) String() string

type BlackholeSink

type BlackholeSink struct{}

BlackholeSink is used to just blackhole messages

func (*BlackholeSink) AddSample

func (*BlackholeSink) AddSample(_ string, _ float64, _ []Tag)

AddSample is for timing information, where quantiles are used

func (*BlackholeSink) IncrCounter

func (*BlackholeSink) IncrCounter(_ string, _ float64, _ []Tag)

IncrCounter should accumulate values

func (*BlackholeSink) SetGauge

func (*BlackholeSink) SetGauge(_ string, _ float64, _ []Tag)

SetGauge should retain the last value it is set to

type Config

type Config struct {
	ServiceName          string        // Prefixed with keys to separate services
	HostName             string        // Hostname to use. If not provided and EnableHostname, it will be os.Hostname
	EnableHostname       bool          // Enable prefixing gauge values with hostname
	EnableHostnameLabel  bool          // Enable adding hostname to labels
	EnableServiceLabel   bool          // Enable adding service to labels
	EnableRuntimeMetrics bool          // Enables profiling of runtime metrics (GC, Goroutines, Memory)
	EnableTypePrefix     bool          // Prefixes key with a type ("counter", "gauge", "sample")
	TimerGranularity     time.Duration // Granularity of timers.
	ProfileInterval      time.Duration // Interval to profile runtime metrics
	GlobalTags           []Tag         // Tags to add to every metric
	GlobalPrefix         string        // Prefix to add to every metric

	AllowedPrefixes []string // A list of the first metric prefixes to allow
	BlockedPrefixes []string // A list of the first metric prefixes to block
	FilterDefault   bool     // Whether to allow metrics by default
}

Config is used to configure metrics settings

func DefaultConfig

func DefaultConfig(serviceName string) *Config

DefaultConfig provides a sane default configuration

func (*Config) AllowMetric added in v0.4.0

func (m *Config) AllowMetric(key string) bool

AllowMetric returns whether the metric should be allowed based on configured prefix filters Also return the applicable tags

func (*Config) Help added in v0.4.0

func (m *Config) Help(providers ...[]*Describe) map[string]string

Help returns prepared help for described metrics

func (*Config) Prepare added in v0.4.0

func (m *Config) Prepare(typ string, key string, tags ...Tag) (bool, string, []Tag)

Prepare returns final metrics name and tags to emit

type Describe added in v0.4.0

type Describe struct {
	// Type of the metric: counter|gauge|summary
	Type string
	// Name is the metric name
	Name string
	// Help provides description
	Help string
	// RequiredTags is a list of metric tags
	RequiredTags []string
}

Describe provides metric description

func (*Describe) AddSample added in v0.4.0

func (d *Describe) AddSample(val float64, tags ...string)

AddSample is for timing information, where quantiles are used

func (*Describe) IncrCounter added in v0.4.0

func (d *Describe) IncrCounter(val float64, tags ...string)

IncrCounter should accumulate values

func (*Describe) MeasureSince added in v0.4.0

func (d *Describe) MeasureSince(start time.Time, tags ...string)

MeasureSince emits sample

func (*Describe) SetGauge added in v0.4.0

func (d *Describe) SetGauge(val float64, tags ...string)

SetGauge should retain the last value it is set to

func (*Describe) Tags added in v0.4.0

func (d *Describe) Tags(vals ...string) []Tag

Tags constructs tags. The size and order of the vals must much the ones in the description

type FanoutSink

type FanoutSink []Sink

FanoutSink is used to sink to fanout values to multiple sinks

func NewFanoutSink

func NewFanoutSink(sinks ...Sink) FanoutSink

NewFanoutSink creates fan-out sink

func (FanoutSink) AddSample

func (fh FanoutSink) AddSample(key string, val float64, tags []Tag)

AddSample is for timing information, where quantiles are used

func (FanoutSink) IncrCounter

func (fh FanoutSink) IncrCounter(key string, val float64, tags []Tag)

IncrCounter should accumulate values

func (FanoutSink) SetGauge

func (fh FanoutSink) SetGauge(key string, val float64, tags []Tag)

SetGauge should retain the last value it is set to

type GaugeValue

type GaugeValue struct {
	Name  string
	Hash  string `json:"-"`
	Value float64

	Labels        []Tag             `json:"-"`
	DisplayLabels map[string]string `json:"Labels"`
}

GaugeValue provides gauge value

type InmemSignal

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

InmemSignal is used to listen for a given signal, and when received, to dump the current metrics from the InmemSink to an io.Writer

func DefaultInmemSignal

func DefaultInmemSignal(inmem *InmemSink) *InmemSignal

DefaultInmemSignal returns a new InmemSignal that responds to SIGUSR1 and writes output to stderr. Windows uses SIGBREAK

func NewInmemSignal

func NewInmemSignal(inmem *InmemSink, sig syscall.Signal, w io.Writer) *InmemSignal

NewInmemSignal creates a new InmemSignal which listens for a given signal, and dumps the current metrics out to a writer

func (*InmemSignal) Stop

func (i *InmemSignal) Stop()

Stop is used to stop the InmemSignal from listening

type InmemSink

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

InmemSink provides a MetricSink that does in-memory aggregation without sending metrics over a network. It can be embedded within an application to provide profiling information.

func NewInmemSink

func NewInmemSink(interval, retain time.Duration) *InmemSink

NewInmemSink is used to construct a new in-memory sink. Uses an aggregation interval and maximum retention period.

func (*InmemSink) AddSample

func (i *InmemSink) AddSample(key string, val float64, tags []Tag)

AddSample is for timing information, where quantiles are used

func (*InmemSink) Data

func (i *InmemSink) Data() []*IntervalMetrics

Data is used to retrieve all the aggregated metrics Intervals may be in use, and a read lock should be acquired

func (*InmemSink) DisplayMetrics

func (i *InmemSink) DisplayMetrics() (*Summary, error)

DisplayMetrics returns a summary of the metrics from the most recent finished interval.

func (*InmemSink) IncrCounter

func (i *InmemSink) IncrCounter(key string, val float64, tags []Tag)

IncrCounter should accumulate values

func (*InmemSink) SetGauge

func (i *InmemSink) SetGauge(key string, val float64, tags []Tag)

SetGauge should retain the last value it is set to

type IntervalMetrics

type IntervalMetrics struct {
	sync.RWMutex

	// The start time of the interval
	Interval time.Time

	// Gauges maps the key to the last set value
	Gauges map[string]GaugeValue

	// Counters maps the string key to a sum of the counter
	// values
	Counters map[string]SampledValue

	// Samples maps the key to an AggregateSample,
	// which has the rolled up view of a sample
	Samples map[string]SampledValue
}

IntervalMetrics stores the aggregated metrics for a specific interval

func NewIntervalMetrics

func NewIntervalMetrics(intv time.Time) *IntervalMetrics

NewIntervalMetrics creates a new IntervalMetrics for a given interval

type Metrics

type Metrics struct {
	Config
	// contains filtered or unexported fields
}

Metrics represents an instance of a metrics sink that can be used to emit

func New

func New(conf *Config, sink Sink) (*Metrics, error)

New is used to create a new instance of Metrics

func NewGlobal

func NewGlobal(conf *Config, sink Sink) (*Metrics, error)

NewGlobal is the same as New, but it assigns the metrics object to be used globally as well as returning it.

func (*Metrics) AddSample

func (m *Metrics) AddSample(key string, val float64, tags ...Tag)

AddSample is for timing information, where quantiles are used

func (*Metrics) IncrCounter

func (m *Metrics) IncrCounter(key string, val float64, tags ...Tag)

IncrCounter should accumulate values

func (*Metrics) MeasureSince

func (m *Metrics) MeasureSince(key string, start time.Time, tags ...Tag)

MeasureSince is for timing information

func (*Metrics) SetGauge

func (m *Metrics) SetGauge(key string, val float64, tags ...Tag)

SetGauge should retain the last value it is set to

func (*Metrics) UpdateFilter

func (m *Metrics) UpdateFilter(allow, block []string)

UpdateFilter overwrites the existing filter with the given rules.

type PointValue

type PointValue struct {
	Name   string
	Points []float64
}

PointValue provides point value

type Provider

type Provider interface {
	SetGauge(key string, val float64, tags ...Tag)
	IncrCounter(key string, val float64, tags ...Tag)
	AddSample(key string, val float64, tags ...Tag)
	MeasureSince(key string, start time.Time, tags ...Tag)
}

Provider basics

type SampledValue

type SampledValue struct {
	Name string
	Hash string `json:"-"`
	*AggregateSample
	Mean   float64
	Stddev float64

	Labels        []Tag             `json:"-"`
	DisplayLabels map[string]string `json:"Labels"`
}

SampledValue provides sample value

type Sink

type Sink interface {
	// SetGauge should retain the last value it is set to
	SetGauge(key string, val float64, tags []Tag)
	// IncrCounter should accumulate values
	IncrCounter(key string, val float64, tags []Tag)
	// AddSample is for timing information, where quantiles are used
	AddSample(key string, val float64, tags []Tag)
}

The Sink interface is used to transmit metrics information to an external system

func NewInmemSinkFromURL

func NewInmemSinkFromURL(u *url.URL) (Sink, error)

NewInmemSinkFromURL creates an InmemSink from a URL. It is used (and tested) from NewMetricSinkFromURL.

type Summary

type Summary struct {
	Timestamp string
	Gauges    []GaugeValue
	//Points    []PointValue
	Counters []SampledValue
	Samples  []SampledValue
}

Summary holds a roll-up of metrics info for a given interval

type Tag

type Tag struct {
	Name  string
	Value string
}

Tag is used to add dimentions to metrics

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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