Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | ) |
| 7 | |
| 8 | // The MetricSink interface is used to transmit metrics information |
| 9 | // to an external system |
| 10 | type 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 |
| 28 | type BlackholeSink struct{} |
| 29 | |
| 30 | func (*BlackholeSink) SetGauge(key []string, val float32) {} |
| 31 | func (*BlackholeSink) SetGaugeWithLabels(key []string, val float32, labels []Label) {} |
| 32 | func (*BlackholeSink) EmitKey(key []string, val float32) {} |
| 33 | func (*BlackholeSink) IncrCounter(key []string, val float32) {} |
| 34 | func (*BlackholeSink) IncrCounterWithLabels(key []string, val float32, labels []Label) {} |
| 35 | func (*BlackholeSink) AddSample(key []string, val float32) {} |
| 36 | func (*BlackholeSink) AddSampleWithLabels(key []string, val float32, labels []Label) {} |
| 37 | |
| 38 | // FanoutSink is used to sink to fanout values to multiple sinks |
| 39 | type FanoutSink []MetricSink |
| 40 | |
| 41 | func (fh FanoutSink) SetGauge(key []string, val float32) { |
| 42 | fh.SetGaugeWithLabels(key, val, nil) |
| 43 | } |
| 44 | |
| 45 | func (fh FanoutSink) SetGaugeWithLabels(key []string, val float32, labels []Label) { |
| 46 | for _, s := range fh { |
| 47 | s.SetGaugeWithLabels(key, val, labels) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func (fh FanoutSink) EmitKey(key []string, val float32) { |
| 52 | for _, s := range fh { |
| 53 | s.EmitKey(key, val) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func (fh FanoutSink) IncrCounter(key []string, val float32) { |
| 58 | fh.IncrCounterWithLabels(key, val, nil) |
| 59 | } |
| 60 | |
| 61 | func (fh FanoutSink) IncrCounterWithLabels(key []string, val float32, labels []Label) { |
| 62 | for _, s := range fh { |
| 63 | s.IncrCounterWithLabels(key, val, labels) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (fh FanoutSink) AddSample(key []string, val float32) { |
| 68 | fh.AddSampleWithLabels(key, val, nil) |
| 69 | } |
| 70 | |
| 71 | func (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 |
| 79 | type sinkURLFactoryFunc func(*url.URL) (MetricSink, error) |
| 80 | |
| 81 | // sinkRegistry supports the generic NewMetricSink function by mapping URL |
| 82 | // schemes to metric sink factory functions |
| 83 | var 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. |
| 102 | func 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 | } |