blob: 0b7d6e4be43fe7ce7211f0023e64449cbb1b748b [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001package metrics
2
Stephane Barbarie260a5632019-02-26 16:12:49 -05003import (
4 "fmt"
5 "net/url"
6)
7
khenaidooac637102019-01-14 15:44:34 -05008// The MetricSink interface is used to transmit metrics information
9// to an external system
10type MetricSink interface {
11 // A Gauge should retain the last value it is set to
12 SetGauge(key []string, val float32)
Stephane Barbarie260a5632019-02-26 16:12:49 -050013 SetGaugeWithLabels(key []string, val float32, labels []Label)
khenaidooac637102019-01-14 15:44:34 -050014
15 // Should emit a Key/Value pair for each call
16 EmitKey(key []string, val float32)
17
18 // Counters should accumulate values
19 IncrCounter(key []string, val float32)
Stephane Barbarie260a5632019-02-26 16:12:49 -050020 IncrCounterWithLabels(key []string, val float32, labels []Label)
khenaidooac637102019-01-14 15:44:34 -050021
22 // Samples are for timing information, where quantiles are used
23 AddSample(key []string, val float32)
Stephane Barbarie260a5632019-02-26 16:12:49 -050024 AddSampleWithLabels(key []string, val float32, labels []Label)
khenaidooac637102019-01-14 15:44:34 -050025}
26
27// BlackholeSink is used to just blackhole messages
28type BlackholeSink struct{}
29
Stephane Barbarie260a5632019-02-26 16:12:49 -050030func (*BlackholeSink) SetGauge(key []string, val float32) {}
31func (*BlackholeSink) SetGaugeWithLabels(key []string, val float32, labels []Label) {}
32func (*BlackholeSink) EmitKey(key []string, val float32) {}
33func (*BlackholeSink) IncrCounter(key []string, val float32) {}
34func (*BlackholeSink) IncrCounterWithLabels(key []string, val float32, labels []Label) {}
35func (*BlackholeSink) AddSample(key []string, val float32) {}
36func (*BlackholeSink) AddSampleWithLabels(key []string, val float32, labels []Label) {}
khenaidooac637102019-01-14 15:44:34 -050037
38// FanoutSink is used to sink to fanout values to multiple sinks
39type FanoutSink []MetricSink
40
41func (fh FanoutSink) SetGauge(key []string, val float32) {
Stephane Barbarie260a5632019-02-26 16:12:49 -050042 fh.SetGaugeWithLabels(key, val, nil)
43}
44
45func (fh FanoutSink) SetGaugeWithLabels(key []string, val float32, labels []Label) {
khenaidooac637102019-01-14 15:44:34 -050046 for _, s := range fh {
Stephane Barbarie260a5632019-02-26 16:12:49 -050047 s.SetGaugeWithLabels(key, val, labels)
khenaidooac637102019-01-14 15:44:34 -050048 }
49}
50
51func (fh FanoutSink) EmitKey(key []string, val float32) {
52 for _, s := range fh {
53 s.EmitKey(key, val)
54 }
55}
56
57func (fh FanoutSink) IncrCounter(key []string, val float32) {
Stephane Barbarie260a5632019-02-26 16:12:49 -050058 fh.IncrCounterWithLabels(key, val, nil)
59}
60
61func (fh FanoutSink) IncrCounterWithLabels(key []string, val float32, labels []Label) {
khenaidooac637102019-01-14 15:44:34 -050062 for _, s := range fh {
Stephane Barbarie260a5632019-02-26 16:12:49 -050063 s.IncrCounterWithLabels(key, val, labels)
khenaidooac637102019-01-14 15:44:34 -050064 }
65}
66
67func (fh FanoutSink) AddSample(key []string, val float32) {
Stephane Barbarie260a5632019-02-26 16:12:49 -050068 fh.AddSampleWithLabels(key, val, nil)
69}
70
71func (fh FanoutSink) AddSampleWithLabels(key []string, val float32, labels []Label) {
khenaidooac637102019-01-14 15:44:34 -050072 for _, s := range fh {
Stephane Barbarie260a5632019-02-26 16:12:49 -050073 s.AddSampleWithLabels(key, val, labels)
khenaidooac637102019-01-14 15:44:34 -050074 }
75}
Stephane Barbarie260a5632019-02-26 16:12:49 -050076
77// sinkURLFactoryFunc is an generic interface around the *SinkFromURL() function provided
78// by each sink type
79type sinkURLFactoryFunc func(*url.URL) (MetricSink, error)
80
81// sinkRegistry supports the generic NewMetricSink function by mapping URL
82// schemes to metric sink factory functions
83var sinkRegistry = map[string]sinkURLFactoryFunc{
84 "statsd": NewStatsdSinkFromURL,
85 "statsite": NewStatsiteSinkFromURL,
86 "inmem": NewInmemSinkFromURL,
87}
88
89// NewMetricSinkFromURL allows a generic URL input to configure any of the
90// supported sinks. The scheme of the URL identifies the type of the sink, the
91// and query parameters are used to set options.
92//
93// "statsd://" - Initializes a StatsdSink. The host and port are passed through
94// as the "addr" of the sink
95//
96// "statsite://" - Initializes a StatsiteSink. The host and port become the
97// "addr" of the sink
98//
99// "inmem://" - Initializes an InmemSink. The host and port are ignored. The
100// "interval" and "duration" query parameters must be specified with valid
101// durations, see NewInmemSink for details.
102func NewMetricSinkFromURL(urlStr string) (MetricSink, error) {
103 u, err := url.Parse(urlStr)
104 if err != nil {
105 return nil, err
106 }
107
108 sinkURLFactoryFunc := sinkRegistry[u.Scheme]
109 if sinkURLFactoryFunc == nil {
110 return nil, fmt.Errorf(
111 "cannot create metric sink, unrecognized sink name: %q", u.Scheme)
112 }
113
114 return sinkURLFactoryFunc(u)
115}