blob: 0b7d6e4be43fe7ce7211f0023e64449cbb1b748b [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -07001package metrics
2
3import (
4 "fmt"
5 "net/url"
6)
7
8// 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)
13 SetGaugeWithLabels(key []string, val float32, labels []Label)
14
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)
20 IncrCounterWithLabels(key []string, val float32, labels []Label)
21
22 // Samples are for timing information, where quantiles are used
23 AddSample(key []string, val float32)
24 AddSampleWithLabels(key []string, val float32, labels []Label)
25}
26
27// BlackholeSink is used to just blackhole messages
28type BlackholeSink struct{}
29
30func (*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) {}
37
38// FanoutSink is used to sink to fanout values to multiple sinks
39type FanoutSink []MetricSink
40
41func (fh FanoutSink) SetGauge(key []string, val float32) {
42 fh.SetGaugeWithLabels(key, val, nil)
43}
44
45func (fh FanoutSink) SetGaugeWithLabels(key []string, val float32, labels []Label) {
46 for _, s := range fh {
47 s.SetGaugeWithLabels(key, val, labels)
48 }
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) {
58 fh.IncrCounterWithLabels(key, val, nil)
59}
60
61func (fh FanoutSink) IncrCounterWithLabels(key []string, val float32, labels []Label) {
62 for _, s := range fh {
63 s.IncrCounterWithLabels(key, val, labels)
64 }
65}
66
67func (fh FanoutSink) AddSample(key []string, val float32) {
68 fh.AddSampleWithLabels(key, val, nil)
69}
70
71func (fh FanoutSink) AddSampleWithLabels(key []string, val float32, labels []Label) {
72 for _, s := range fh {
73 s.AddSampleWithLabels(key, val, labels)
74 }
75}
76
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}