DataDog and StatsD Metrics Support
* Added support for DataDog and StatsD monitoring * Added documentation
This commit is contained in:
parent
cd28e7b24f
commit
69c628b626
39 changed files with 3921 additions and 13 deletions
306
vendor/github.com/go-kit/kit/metrics/dogstatsd/dogstatsd.go
generated
vendored
Normal file
306
vendor/github.com/go-kit/kit/metrics/dogstatsd/dogstatsd.go
generated
vendored
Normal file
|
@ -0,0 +1,306 @@
|
|||
// Package dogstatsd provides a DogStatsD backend for package metrics. It's very
|
||||
// similar to StatsD, but supports arbitrary tags per-metric, which map to Go
|
||||
// kit's label values. So, while label values are no-ops in StatsD, they are
|
||||
// supported here. For more details, see the documentation at
|
||||
// http://docs.datadoghq.com/guides/dogstatsd/.
|
||||
//
|
||||
// This package batches observations and emits them on some schedule to the
|
||||
// remote server. This is useful even if you connect to your DogStatsD server
|
||||
// over UDP. Emitting one network packet per observation can quickly overwhelm
|
||||
// even the fastest internal network.
|
||||
package dogstatsd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/go-kit/kit/metrics"
|
||||
"github.com/go-kit/kit/metrics/internal/lv"
|
||||
"github.com/go-kit/kit/metrics/internal/ratemap"
|
||||
"github.com/go-kit/kit/util/conn"
|
||||
)
|
||||
|
||||
// Dogstatsd receives metrics observations and forwards them to a DogStatsD
|
||||
// server. Create a Dogstatsd object, use it to create metrics, and pass those
|
||||
// metrics as dependencies to the components that will use them.
|
||||
//
|
||||
// All metrics are buffered until WriteTo is called. Counters and gauges are
|
||||
// aggregated into a single observation per timeseries per write. Timings and
|
||||
// histograms are buffered but not aggregated.
|
||||
//
|
||||
// To regularly report metrics to an io.Writer, use the WriteLoop helper method.
|
||||
// To send to a DogStatsD server, use the SendLoop helper method.
|
||||
type Dogstatsd struct {
|
||||
prefix string
|
||||
rates *ratemap.RateMap
|
||||
counters *lv.Space
|
||||
gauges *lv.Space
|
||||
timings *lv.Space
|
||||
histograms *lv.Space
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
// New returns a Dogstatsd object that may be used to create metrics. Prefix is
|
||||
// applied to all created metrics. Callers must ensure that regular calls to
|
||||
// WriteTo are performed, either manually or with one of the helper methods.
|
||||
func New(prefix string, logger log.Logger) *Dogstatsd {
|
||||
return &Dogstatsd{
|
||||
prefix: prefix,
|
||||
rates: ratemap.New(),
|
||||
counters: lv.NewSpace(),
|
||||
gauges: lv.NewSpace(),
|
||||
timings: lv.NewSpace(),
|
||||
histograms: lv.NewSpace(),
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// NewCounter returns a counter, sending observations to this Dogstatsd object.
|
||||
func (d *Dogstatsd) NewCounter(name string, sampleRate float64) *Counter {
|
||||
d.rates.Set(d.prefix+name, sampleRate)
|
||||
return &Counter{
|
||||
name: d.prefix + name,
|
||||
obs: d.counters.Observe,
|
||||
}
|
||||
}
|
||||
|
||||
// NewGauge returns a gauge, sending observations to this Dogstatsd object.
|
||||
func (d *Dogstatsd) NewGauge(name string) *Gauge {
|
||||
return &Gauge{
|
||||
name: d.prefix + name,
|
||||
obs: d.gauges.Observe,
|
||||
}
|
||||
}
|
||||
|
||||
// NewTiming returns a histogram whose observations are interpreted as
|
||||
// millisecond durations, and are forwarded to this Dogstatsd object.
|
||||
func (d *Dogstatsd) NewTiming(name string, sampleRate float64) *Timing {
|
||||
d.rates.Set(d.prefix+name, sampleRate)
|
||||
return &Timing{
|
||||
name: d.prefix + name,
|
||||
obs: d.timings.Observe,
|
||||
}
|
||||
}
|
||||
|
||||
// NewHistogram returns a histogram whose observations are of an unspecified
|
||||
// unit, and are forwarded to this Dogstatsd object.
|
||||
func (d *Dogstatsd) NewHistogram(name string, sampleRate float64) *Histogram {
|
||||
d.rates.Set(d.prefix+name, sampleRate)
|
||||
return &Histogram{
|
||||
name: d.prefix + name,
|
||||
obs: d.histograms.Observe,
|
||||
}
|
||||
}
|
||||
|
||||
// WriteLoop is a helper method that invokes WriteTo to the passed writer every
|
||||
// time the passed channel fires. This method blocks until the channel is
|
||||
// closed, so clients probably want to run it in its own goroutine. For typical
|
||||
// usage, create a time.Ticker and pass its C channel to this method.
|
||||
func (d *Dogstatsd) WriteLoop(c <-chan time.Time, w io.Writer) {
|
||||
for range c {
|
||||
if _, err := d.WriteTo(w); err != nil {
|
||||
d.logger.Log("during", "WriteTo", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SendLoop is a helper method that wraps WriteLoop, passing a managed
|
||||
// connection to the network and address. Like WriteLoop, this method blocks
|
||||
// until the channel is closed, so clients probably want to start it in its own
|
||||
// goroutine. For typical usage, create a time.Ticker and pass its C channel to
|
||||
// this method.
|
||||
func (d *Dogstatsd) SendLoop(c <-chan time.Time, network, address string) {
|
||||
d.WriteLoop(c, conn.NewDefaultManager(network, address, d.logger))
|
||||
}
|
||||
|
||||
// WriteTo flushes the buffered content of the metrics to the writer, in
|
||||
// DogStatsD format. WriteTo abides best-effort semantics, so observations are
|
||||
// lost if there is a problem with the write. Clients should be sure to call
|
||||
// WriteTo regularly, ideally through the WriteLoop or SendLoop helper methods.
|
||||
func (d *Dogstatsd) WriteTo(w io.Writer) (count int64, err error) {
|
||||
var n int
|
||||
|
||||
d.counters.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
|
||||
n, err = fmt.Fprintf(w, "%s:%f|c%s%s\n", name, sum(values), sampling(d.rates.Get(name)), tagValues(lvs))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
count += int64(n)
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
d.gauges.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
|
||||
n, err = fmt.Fprintf(w, "%s:%f|g%s\n", name, last(values), tagValues(lvs))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
count += int64(n)
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
d.timings.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
|
||||
sampleRate := d.rates.Get(name)
|
||||
for _, value := range values {
|
||||
n, err = fmt.Fprintf(w, "%s:%f|ms%s%s\n", name, value, sampling(sampleRate), tagValues(lvs))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
count += int64(n)
|
||||
}
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
d.histograms.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
|
||||
sampleRate := d.rates.Get(name)
|
||||
for _, value := range values {
|
||||
n, err = fmt.Fprintf(w, "%s:%f|h%s%s\n", name, value, sampling(sampleRate), tagValues(lvs))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
count += int64(n)
|
||||
}
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
return count, err
|
||||
}
|
||||
|
||||
func sum(a []float64) float64 {
|
||||
var v float64
|
||||
for _, f := range a {
|
||||
v += f
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func last(a []float64) float64 {
|
||||
return a[len(a)-1]
|
||||
}
|
||||
|
||||
func sampling(r float64) string {
|
||||
var sv string
|
||||
if r < 1.0 {
|
||||
sv = fmt.Sprintf("|@%f", r)
|
||||
}
|
||||
return sv
|
||||
}
|
||||
|
||||
func tagValues(labelValues []string) string {
|
||||
if len(labelValues) == 0 {
|
||||
return ""
|
||||
}
|
||||
if len(labelValues)%2 != 0 {
|
||||
panic("tagValues received a labelValues with an odd number of strings")
|
||||
}
|
||||
pairs := make([]string, 0, len(labelValues)/2)
|
||||
for i := 0; i < len(labelValues); i += 2 {
|
||||
pairs = append(pairs, labelValues[i]+":"+labelValues[i+1])
|
||||
}
|
||||
return "|#" + strings.Join(pairs, ",")
|
||||
}
|
||||
|
||||
type observeFunc func(name string, lvs lv.LabelValues, value float64)
|
||||
|
||||
// Counter is a DogStatsD counter. Observations are forwarded to a Dogstatsd
|
||||
// object, and aggregated (summed) per timeseries.
|
||||
type Counter struct {
|
||||
name string
|
||||
lvs lv.LabelValues
|
||||
obs observeFunc
|
||||
}
|
||||
|
||||
// With implements metrics.Counter.
|
||||
func (c *Counter) With(labelValues ...string) metrics.Counter {
|
||||
return &Counter{
|
||||
name: c.name,
|
||||
lvs: c.lvs.With(labelValues...),
|
||||
obs: c.obs,
|
||||
}
|
||||
}
|
||||
|
||||
// Add implements metrics.Counter.
|
||||
func (c *Counter) Add(delta float64) {
|
||||
c.obs(c.name, c.lvs, delta)
|
||||
}
|
||||
|
||||
// Gauge is a DogStatsD gauge. Observations are forwarded to a Dogstatsd
|
||||
// object, and aggregated (the last observation selected) per timeseries.
|
||||
type Gauge struct {
|
||||
name string
|
||||
lvs lv.LabelValues
|
||||
obs observeFunc
|
||||
}
|
||||
|
||||
// With implements metrics.Gauge.
|
||||
func (g *Gauge) With(labelValues ...string) metrics.Gauge {
|
||||
return &Gauge{
|
||||
name: g.name,
|
||||
lvs: g.lvs.With(labelValues...),
|
||||
obs: g.obs,
|
||||
}
|
||||
}
|
||||
|
||||
// Set implements metrics.Gauge.
|
||||
func (g *Gauge) Set(value float64) {
|
||||
g.obs(g.name, g.lvs, value)
|
||||
}
|
||||
|
||||
// Timing is a DogStatsD timing, or metrics.Histogram. Observations are
|
||||
// forwarded to a Dogstatsd object, and collected (but not aggregated) per
|
||||
// timeseries.
|
||||
type Timing struct {
|
||||
name string
|
||||
lvs lv.LabelValues
|
||||
obs observeFunc
|
||||
}
|
||||
|
||||
// With implements metrics.Timing.
|
||||
func (t *Timing) With(labelValues ...string) metrics.Histogram {
|
||||
return &Timing{
|
||||
name: t.name,
|
||||
lvs: t.lvs.With(labelValues...),
|
||||
obs: t.obs,
|
||||
}
|
||||
}
|
||||
|
||||
// Observe implements metrics.Histogram. Value is interpreted as milliseconds.
|
||||
func (t *Timing) Observe(value float64) {
|
||||
t.obs(t.name, t.lvs, value)
|
||||
}
|
||||
|
||||
// Histogram is a DogStatsD histrogram. Observations are forwarded to a
|
||||
// Dogstatsd object, and collected (but not aggregated) per timeseries.
|
||||
type Histogram struct {
|
||||
name string
|
||||
lvs lv.LabelValues
|
||||
obs observeFunc
|
||||
}
|
||||
|
||||
// With implements metrics.Histogram.
|
||||
func (h *Histogram) With(labelValues ...string) metrics.Histogram {
|
||||
return &Histogram{
|
||||
name: h.name,
|
||||
lvs: h.lvs.With(labelValues...),
|
||||
obs: h.obs,
|
||||
}
|
||||
}
|
||||
|
||||
// Observe implements metrics.Histogram.
|
||||
func (h *Histogram) Observe(value float64) {
|
||||
h.obs(h.name, h.lvs, value)
|
||||
}
|
40
vendor/github.com/go-kit/kit/metrics/internal/ratemap/ratemap.go
generated
vendored
Normal file
40
vendor/github.com/go-kit/kit/metrics/internal/ratemap/ratemap.go
generated
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Package ratemap implements a goroutine-safe map of string to float64. It can
|
||||
// be embedded in implementations whose metrics support fixed sample rates, so
|
||||
// that an additional parameter doesn't have to be tracked through the e.g.
|
||||
// lv.Space object.
|
||||
package ratemap
|
||||
|
||||
import "sync"
|
||||
|
||||
// RateMap is a simple goroutine-safe map of string to float64.
|
||||
type RateMap struct {
|
||||
mtx sync.RWMutex
|
||||
m map[string]float64
|
||||
}
|
||||
|
||||
// New returns a new RateMap.
|
||||
func New() *RateMap {
|
||||
return &RateMap{
|
||||
m: map[string]float64{},
|
||||
}
|
||||
}
|
||||
|
||||
// Set writes the given name/rate pair to the map.
|
||||
// Set is safe for concurrent access by multiple goroutines.
|
||||
func (m *RateMap) Set(name string, rate float64) {
|
||||
m.mtx.Lock()
|
||||
defer m.mtx.Unlock()
|
||||
m.m[name] = rate
|
||||
}
|
||||
|
||||
// Get retrieves the rate for the given name, or 1.0 if none is set.
|
||||
// Get is safe for concurrent access by multiple goroutines.
|
||||
func (m *RateMap) Get(name string) float64 {
|
||||
m.mtx.RLock()
|
||||
defer m.mtx.RUnlock()
|
||||
f, ok := m.m[name]
|
||||
if !ok {
|
||||
f = 1.0
|
||||
}
|
||||
return f
|
||||
}
|
79
vendor/github.com/go-kit/kit/metrics/multi/multi.go
generated
vendored
Normal file
79
vendor/github.com/go-kit/kit/metrics/multi/multi.go
generated
vendored
Normal file
|
@ -0,0 +1,79 @@
|
|||
// Package multi provides adapters that send observations to multiple metrics
|
||||
// simultaneously. This is useful if your service needs to emit to multiple
|
||||
// instrumentation systems at the same time, for example if your organization is
|
||||
// transitioning from one system to another.
|
||||
package multi
|
||||
|
||||
import "github.com/go-kit/kit/metrics"
|
||||
|
||||
// Counter collects multiple individual counters and treats them as a unit.
|
||||
type Counter []metrics.Counter
|
||||
|
||||
// NewCounter returns a multi-counter, wrapping the passed counters.
|
||||
func NewCounter(c ...metrics.Counter) Counter {
|
||||
return Counter(c)
|
||||
}
|
||||
|
||||
// Add implements counter.
|
||||
func (c Counter) Add(delta float64) {
|
||||
for _, counter := range c {
|
||||
counter.Add(delta)
|
||||
}
|
||||
}
|
||||
|
||||
// With implements counter.
|
||||
func (c Counter) With(labelValues ...string) metrics.Counter {
|
||||
next := make(Counter, len(c))
|
||||
for i := range c {
|
||||
next[i] = c[i].With(labelValues...)
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
// Gauge collects multiple individual gauges and treats them as a unit.
|
||||
type Gauge []metrics.Gauge
|
||||
|
||||
// NewGauge returns a multi-gauge, wrapping the passed gauges.
|
||||
func NewGauge(g ...metrics.Gauge) Gauge {
|
||||
return Gauge(g)
|
||||
}
|
||||
|
||||
// Set implements Gauge.
|
||||
func (g Gauge) Set(value float64) {
|
||||
for _, gauge := range g {
|
||||
gauge.Set(value)
|
||||
}
|
||||
}
|
||||
|
||||
// With implements gauge.
|
||||
func (g Gauge) With(labelValues ...string) metrics.Gauge {
|
||||
next := make(Gauge, len(g))
|
||||
for i := range g {
|
||||
next[i] = g[i].With(labelValues...)
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
// Histogram collects multiple individual histograms and treats them as a unit.
|
||||
type Histogram []metrics.Histogram
|
||||
|
||||
// NewHistogram returns a multi-histogram, wrapping the passed histograms.
|
||||
func NewHistogram(h ...metrics.Histogram) Histogram {
|
||||
return Histogram(h)
|
||||
}
|
||||
|
||||
// Observe implements Histogram.
|
||||
func (h Histogram) Observe(value float64) {
|
||||
for _, histogram := range h {
|
||||
histogram.Observe(value)
|
||||
}
|
||||
}
|
||||
|
||||
// With implements histogram.
|
||||
func (h Histogram) With(labelValues ...string) metrics.Histogram {
|
||||
next := make(Histogram, len(h))
|
||||
for i := range h {
|
||||
next[i] = h[i].With(labelValues...)
|
||||
}
|
||||
return next
|
||||
}
|
232
vendor/github.com/go-kit/kit/metrics/statsd/statsd.go
generated
vendored
Normal file
232
vendor/github.com/go-kit/kit/metrics/statsd/statsd.go
generated
vendored
Normal file
|
@ -0,0 +1,232 @@
|
|||
// Package statsd provides a StatsD backend for package metrics. StatsD has no
|
||||
// concept of arbitrary key-value tagging, so label values are not supported,
|
||||
// and With is a no-op on all metrics.
|
||||
//
|
||||
// This package batches observations and emits them on some schedule to the
|
||||
// remote server. This is useful even if you connect to your StatsD server over
|
||||
// UDP. Emitting one network packet per observation can quickly overwhelm even
|
||||
// the fastest internal network.
|
||||
package statsd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/go-kit/kit/metrics"
|
||||
"github.com/go-kit/kit/metrics/internal/lv"
|
||||
"github.com/go-kit/kit/metrics/internal/ratemap"
|
||||
"github.com/go-kit/kit/util/conn"
|
||||
)
|
||||
|
||||
// Statsd receives metrics observations and forwards them to a StatsD server.
|
||||
// Create a Statsd object, use it to create metrics, and pass those metrics as
|
||||
// dependencies to the components that will use them.
|
||||
//
|
||||
// All metrics are buffered until WriteTo is called. Counters and gauges are
|
||||
// aggregated into a single observation per timeseries per write. Timings are
|
||||
// buffered but not aggregated.
|
||||
//
|
||||
// To regularly report metrics to an io.Writer, use the WriteLoop helper method.
|
||||
// To send to a StatsD server, use the SendLoop helper method.
|
||||
type Statsd struct {
|
||||
prefix string
|
||||
rates *ratemap.RateMap
|
||||
|
||||
// The observations are collected in an N-dimensional vector space, even
|
||||
// though they only take advantage of a single dimension (name). This is an
|
||||
// implementation detail born purely from convenience. It would be more
|
||||
// accurate to collect them in a map[string][]float64, but we already have
|
||||
// this nice data structure and helper methods.
|
||||
counters *lv.Space
|
||||
gauges *lv.Space
|
||||
timings *lv.Space
|
||||
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
// New returns a Statsd object that may be used to create metrics. Prefix is
|
||||
// applied to all created metrics. Callers must ensure that regular calls to
|
||||
// WriteTo are performed, either manually or with one of the helper methods.
|
||||
func New(prefix string, logger log.Logger) *Statsd {
|
||||
return &Statsd{
|
||||
prefix: prefix,
|
||||
rates: ratemap.New(),
|
||||
counters: lv.NewSpace(),
|
||||
gauges: lv.NewSpace(),
|
||||
timings: lv.NewSpace(),
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// NewCounter returns a counter, sending observations to this Statsd object.
|
||||
func (s *Statsd) NewCounter(name string, sampleRate float64) *Counter {
|
||||
s.rates.Set(s.prefix+name, sampleRate)
|
||||
return &Counter{
|
||||
name: s.prefix + name,
|
||||
obs: s.counters.Observe,
|
||||
}
|
||||
}
|
||||
|
||||
// NewGauge returns a gauge, sending observations to this Statsd object.
|
||||
func (s *Statsd) NewGauge(name string) *Gauge {
|
||||
return &Gauge{
|
||||
name: s.prefix + name,
|
||||
obs: s.gauges.Observe,
|
||||
}
|
||||
}
|
||||
|
||||
// NewTiming returns a histogram whose observations are interpreted as
|
||||
// millisecond durations, and are forwarded to this Statsd object.
|
||||
func (s *Statsd) NewTiming(name string, sampleRate float64) *Timing {
|
||||
s.rates.Set(s.prefix+name, sampleRate)
|
||||
return &Timing{
|
||||
name: s.prefix + name,
|
||||
obs: s.timings.Observe,
|
||||
}
|
||||
}
|
||||
|
||||
// WriteLoop is a helper method that invokes WriteTo to the passed writer every
|
||||
// time the passed channel fires. This method blocks until the channel is
|
||||
// closed, so clients probably want to run it in its own goroutine. For typical
|
||||
// usage, create a time.Ticker and pass its C channel to this method.
|
||||
func (s *Statsd) WriteLoop(c <-chan time.Time, w io.Writer) {
|
||||
for range c {
|
||||
if _, err := s.WriteTo(w); err != nil {
|
||||
s.logger.Log("during", "WriteTo", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SendLoop is a helper method that wraps WriteLoop, passing a managed
|
||||
// connection to the network and address. Like WriteLoop, this method blocks
|
||||
// until the channel is closed, so clients probably want to start it in its own
|
||||
// goroutine. For typical usage, create a time.Ticker and pass its C channel to
|
||||
// this method.
|
||||
func (s *Statsd) SendLoop(c <-chan time.Time, network, address string) {
|
||||
s.WriteLoop(c, conn.NewDefaultManager(network, address, s.logger))
|
||||
}
|
||||
|
||||
// WriteTo flushes the buffered content of the metrics to the writer, in
|
||||
// StatsD format. WriteTo abides best-effort semantics, so observations are
|
||||
// lost if there is a problem with the write. Clients should be sure to call
|
||||
// WriteTo regularly, ideally through the WriteLoop or SendLoop helper methods.
|
||||
func (s *Statsd) WriteTo(w io.Writer) (count int64, err error) {
|
||||
var n int
|
||||
|
||||
s.counters.Reset().Walk(func(name string, _ lv.LabelValues, values []float64) bool {
|
||||
n, err = fmt.Fprintf(w, "%s:%f|c%s\n", name, sum(values), sampling(s.rates.Get(name)))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
count += int64(n)
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
s.gauges.Reset().Walk(func(name string, _ lv.LabelValues, values []float64) bool {
|
||||
n, err = fmt.Fprintf(w, "%s:%f|g\n", name, last(values))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
count += int64(n)
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
s.timings.Reset().Walk(func(name string, _ lv.LabelValues, values []float64) bool {
|
||||
sampleRate := s.rates.Get(name)
|
||||
for _, value := range values {
|
||||
n, err = fmt.Fprintf(w, "%s:%f|ms%s\n", name, value, sampling(sampleRate))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
count += int64(n)
|
||||
}
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
return count, err
|
||||
}
|
||||
|
||||
func sum(a []float64) float64 {
|
||||
var v float64
|
||||
for _, f := range a {
|
||||
v += f
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func last(a []float64) float64 {
|
||||
return a[len(a)-1]
|
||||
}
|
||||
|
||||
func sampling(r float64) string {
|
||||
var sv string
|
||||
if r < 1.0 {
|
||||
sv = fmt.Sprintf("|@%f", r)
|
||||
}
|
||||
return sv
|
||||
}
|
||||
|
||||
type observeFunc func(name string, lvs lv.LabelValues, value float64)
|
||||
|
||||
// Counter is a StatsD counter. Observations are forwarded to a Statsd object,
|
||||
// and aggregated (summed) per timeseries.
|
||||
type Counter struct {
|
||||
name string
|
||||
obs observeFunc
|
||||
}
|
||||
|
||||
// With is a no-op.
|
||||
func (c *Counter) With(...string) metrics.Counter {
|
||||
return c
|
||||
}
|
||||
|
||||
// Add implements metrics.Counter.
|
||||
func (c *Counter) Add(delta float64) {
|
||||
c.obs(c.name, lv.LabelValues{}, delta)
|
||||
}
|
||||
|
||||
// Gauge is a StatsD gauge. Observations are forwarded to a Statsd object, and
|
||||
// aggregated (the last observation selected) per timeseries.
|
||||
type Gauge struct {
|
||||
name string
|
||||
obs observeFunc
|
||||
}
|
||||
|
||||
// With is a no-op.
|
||||
func (g *Gauge) With(...string) metrics.Gauge {
|
||||
return g
|
||||
}
|
||||
|
||||
// Set implements metrics.Gauge.
|
||||
func (g *Gauge) Set(value float64) {
|
||||
g.obs(g.name, lv.LabelValues{}, value)
|
||||
}
|
||||
|
||||
// Timing is a StatsD timing, or metrics.Histogram. Observations are
|
||||
// forwarded to a Statsd object, and collected (but not aggregated) per
|
||||
// timeseries.
|
||||
type Timing struct {
|
||||
name string
|
||||
obs observeFunc
|
||||
}
|
||||
|
||||
// With is a no-op.
|
||||
func (t *Timing) With(...string) metrics.Histogram {
|
||||
return t
|
||||
}
|
||||
|
||||
// Observe implements metrics.Histogram. Value is interpreted as milliseconds.
|
||||
func (t *Timing) Observe(value float64) {
|
||||
t.obs(t.name, lv.LabelValues{}, value)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue