Merge pull request #267 from DSchalla/metrics/naming
Remove naming prefixes from metric backends (closes #266)
Peter Bourgon
7 years ago
23 | 23 | |
24 | 24 | const maxBufferSize = 1400 // bytes |
25 | 25 | |
26 | type dogstatsdCounter struct { | |
26 | type counter struct { | |
27 | 27 | key string |
28 | 28 | c chan string |
29 | 29 | tags []metrics.Field |
41 | 41 | // NewCounterTick is the same as NewCounter, but allows the user to pass in a |
42 | 42 | // ticker channel instead of invoking time.Tick. |
43 | 43 | func NewCounterTick(w io.Writer, key string, reportTicker <-chan time.Time, tags []metrics.Field) metrics.Counter { |
44 | c := &dogstatsdCounter{ | |
44 | c := &counter{ | |
45 | 45 | key: key, |
46 | 46 | c: make(chan string), |
47 | 47 | tags: tags, |
50 | 50 | return c |
51 | 51 | } |
52 | 52 | |
53 | func (c *dogstatsdCounter) Name() string { return c.key } | |
54 | ||
55 | func (c *dogstatsdCounter) With(f metrics.Field) metrics.Counter { | |
56 | return &dogstatsdCounter{ | |
53 | func (c *counter) Name() string { return c.key } | |
54 | ||
55 | func (c *counter) With(f metrics.Field) metrics.Counter { | |
56 | return &counter{ | |
57 | 57 | key: c.key, |
58 | 58 | c: c.c, |
59 | 59 | tags: append(c.tags, f), |
60 | 60 | } |
61 | 61 | } |
62 | 62 | |
63 | func (c *dogstatsdCounter) Add(delta uint64) { c.c <- applyTags(fmt.Sprintf("%d|c", delta), c.tags) } | |
64 | ||
65 | type dogstatsdGauge struct { | |
63 | func (c *counter) Add(delta uint64) { c.c <- applyTags(fmt.Sprintf("%d|c", delta), c.tags) } | |
64 | ||
65 | type gauge struct { | |
66 | 66 | key string |
67 | 67 | lastValue uint64 // math.Float64frombits |
68 | 68 | g chan string |
81 | 81 | // NewGaugeTick is the same as NewGauge, but allows the user to pass in a ticker |
82 | 82 | // channel instead of invoking time.Tick. |
83 | 83 | func NewGaugeTick(w io.Writer, key string, reportTicker <-chan time.Time, tags []metrics.Field) metrics.Gauge { |
84 | g := &dogstatsdGauge{ | |
84 | g := &gauge{ | |
85 | 85 | key: key, |
86 | 86 | g: make(chan string), |
87 | 87 | tags: tags, |
90 | 90 | return g |
91 | 91 | } |
92 | 92 | |
93 | func (g *dogstatsdGauge) Name() string { return g.key } | |
94 | ||
95 | func (g *dogstatsdGauge) With(f metrics.Field) metrics.Gauge { | |
96 | return &dogstatsdGauge{ | |
93 | func (g *gauge) Name() string { return g.key } | |
94 | ||
95 | func (g *gauge) With(f metrics.Field) metrics.Gauge { | |
96 | return &gauge{ | |
97 | 97 | key: g.key, |
98 | 98 | lastValue: g.lastValue, |
99 | 99 | g: g.g, |
101 | 101 | } |
102 | 102 | } |
103 | 103 | |
104 | func (g *dogstatsdGauge) Add(delta float64) { | |
104 | func (g *gauge) Add(delta float64) { | |
105 | 105 | // https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges |
106 | 106 | sign := "+" |
107 | 107 | if delta < 0 { |
110 | 110 | g.g <- applyTags(fmt.Sprintf("%s%f|g", sign, delta), g.tags) |
111 | 111 | } |
112 | 112 | |
113 | func (g *dogstatsdGauge) Set(value float64) { | |
113 | func (g *gauge) Set(value float64) { | |
114 | 114 | atomic.StoreUint64(&g.lastValue, math.Float64bits(value)) |
115 | 115 | g.g <- applyTags(fmt.Sprintf("%f|g", value), g.tags) |
116 | 116 | } |
117 | 117 | |
118 | func (g *dogstatsdGauge) Get() float64 { | |
118 | func (g *gauge) Get() float64 { | |
119 | 119 | return math.Float64frombits(atomic.LoadUint64(&g.lastValue)) |
120 | 120 | } |
121 | 121 | |
146 | 146 | return c |
147 | 147 | } |
148 | 148 | |
149 | type dogstatsdHistogram struct { | |
149 | type histogram struct { | |
150 | 150 | key string |
151 | 151 | h chan string |
152 | 152 | tags []metrics.Field |
176 | 176 | // NewHistogramTick is the same as NewHistogram, but allows the user to pass a |
177 | 177 | // ticker channel instead of invoking time.Tick. |
178 | 178 | func NewHistogramTick(w io.Writer, key string, reportTicker <-chan time.Time, tags []metrics.Field) metrics.Histogram { |
179 | h := &dogstatsdHistogram{ | |
179 | h := &histogram{ | |
180 | 180 | key: key, |
181 | 181 | h: make(chan string), |
182 | 182 | tags: tags, |
185 | 185 | return h |
186 | 186 | } |
187 | 187 | |
188 | func (h *dogstatsdHistogram) Name() string { return h.key } | |
189 | ||
190 | func (h *dogstatsdHistogram) With(f metrics.Field) metrics.Histogram { | |
191 | return &dogstatsdHistogram{ | |
188 | func (h *histogram) Name() string { return h.key } | |
189 | ||
190 | func (h *histogram) With(f metrics.Field) metrics.Histogram { | |
191 | return &histogram{ | |
192 | 192 | key: h.key, |
193 | 193 | h: h.h, |
194 | 194 | tags: append(h.tags, f), |
195 | 195 | } |
196 | 196 | } |
197 | 197 | |
198 | func (h *dogstatsdHistogram) Observe(value int64) { | |
198 | func (h *histogram) Observe(value int64) { | |
199 | 199 | h.h <- applyTags(fmt.Sprintf("%d|ms", value), h.tags) |
200 | 200 | } |
201 | 201 | |
202 | func (h *dogstatsdHistogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) { | |
202 | func (h *histogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) { | |
203 | 203 | // TODO(pb): no way to do this without introducing e.g. codahale/hdrhistogram |
204 | 204 | return []metrics.Bucket{}, []metrics.Quantile{} |
205 | 205 | } |
13 | 13 | // PrometheusLabelValueUnknown. |
14 | 14 | var PrometheusLabelValueUnknown = "unknown" |
15 | 15 | |
16 | type prometheusCounter struct { | |
16 | type counter struct { | |
17 | 17 | *prometheus.CounterVec |
18 | 18 | name string |
19 | 19 | Pairs map[string]string |
28 | 28 | for _, fieldName := range fieldKeys { |
29 | 29 | p[fieldName] = PrometheusLabelValueUnknown |
30 | 30 | } |
31 | return prometheusCounter{ | |
31 | return counter{ | |
32 | 32 | CounterVec: m, |
33 | 33 | name: opts.Name, |
34 | 34 | Pairs: p, |
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
38 | func (c prometheusCounter) Name() string { return c.name } | |
39 | ||
40 | func (c prometheusCounter) With(f metrics.Field) metrics.Counter { | |
41 | return prometheusCounter{ | |
38 | func (c counter) Name() string { return c.name } | |
39 | ||
40 | func (c counter) With(f metrics.Field) metrics.Counter { | |
41 | return counter{ | |
42 | 42 | CounterVec: c.CounterVec, |
43 | 43 | name: c.name, |
44 | 44 | Pairs: merge(c.Pairs, f), |
45 | 45 | } |
46 | 46 | } |
47 | 47 | |
48 | func (c prometheusCounter) Add(delta uint64) { | |
48 | func (c counter) Add(delta uint64) { | |
49 | 49 | c.CounterVec.With(prometheus.Labels(c.Pairs)).Add(float64(delta)) |
50 | 50 | } |
51 | 51 | |
52 | type prometheusGauge struct { | |
52 | type gauge struct { | |
53 | 53 | *prometheus.GaugeVec |
54 | 54 | name string |
55 | 55 | Pairs map[string]string |
60 | 60 | func NewGauge(opts prometheus.GaugeOpts, fieldKeys []string) metrics.Gauge { |
61 | 61 | m := prometheus.NewGaugeVec(opts, fieldKeys) |
62 | 62 | prometheus.MustRegister(m) |
63 | return prometheusGauge{ | |
63 | return gauge{ | |
64 | 64 | GaugeVec: m, |
65 | 65 | name: opts.Name, |
66 | 66 | Pairs: pairsFrom(fieldKeys), |
67 | 67 | } |
68 | 68 | } |
69 | 69 | |
70 | func (g prometheusGauge) Name() string { return g.name } | |
71 | ||
72 | func (g prometheusGauge) With(f metrics.Field) metrics.Gauge { | |
73 | return prometheusGauge{ | |
70 | func (g gauge) Name() string { return g.name } | |
71 | ||
72 | func (g gauge) With(f metrics.Field) metrics.Gauge { | |
73 | return gauge{ | |
74 | 74 | GaugeVec: g.GaugeVec, |
75 | 75 | name: g.name, |
76 | 76 | Pairs: merge(g.Pairs, f), |
77 | 77 | } |
78 | 78 | } |
79 | 79 | |
80 | func (g prometheusGauge) Set(value float64) { | |
80 | func (g gauge) Set(value float64) { | |
81 | 81 | g.GaugeVec.With(prometheus.Labels(g.Pairs)).Set(value) |
82 | 82 | } |
83 | 83 | |
84 | func (g prometheusGauge) Add(delta float64) { | |
84 | func (g gauge) Add(delta float64) { | |
85 | 85 | g.GaugeVec.With(prometheus.Labels(g.Pairs)).Add(delta) |
86 | 86 | } |
87 | 87 | |
88 | func (g prometheusGauge) Get() float64 { | |
88 | func (g gauge) Get() float64 { | |
89 | 89 | // TODO(pb): see https://github.com/prometheus/client_golang/issues/58 |
90 | 90 | return 0.0 |
91 | 91 | } |
98 | 98 | prometheus.MustRegister(prometheus.NewGaugeFunc(opts, callback)) |
99 | 99 | } |
100 | 100 | |
101 | type prometheusSummary struct { | |
101 | type summary struct { | |
102 | 102 | *prometheus.SummaryVec |
103 | 103 | name string |
104 | 104 | Pairs map[string]string |
112 | 112 | func NewSummary(opts prometheus.SummaryOpts, fieldKeys []string) metrics.Histogram { |
113 | 113 | m := prometheus.NewSummaryVec(opts, fieldKeys) |
114 | 114 | prometheus.MustRegister(m) |
115 | return prometheusSummary{ | |
115 | return summary{ | |
116 | 116 | SummaryVec: m, |
117 | 117 | name: opts.Name, |
118 | 118 | Pairs: pairsFrom(fieldKeys), |
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
122 | func (s prometheusSummary) Name() string { return s.name } | |
123 | ||
124 | func (s prometheusSummary) With(f metrics.Field) metrics.Histogram { | |
125 | return prometheusSummary{ | |
122 | func (s summary) Name() string { return s.name } | |
123 | ||
124 | func (s summary) With(f metrics.Field) metrics.Histogram { | |
125 | return summary{ | |
126 | 126 | SummaryVec: s.SummaryVec, |
127 | 127 | name: s.name, |
128 | 128 | Pairs: merge(s.Pairs, f), |
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
132 | func (s prometheusSummary) Observe(value int64) { | |
132 | func (s summary) Observe(value int64) { | |
133 | 133 | s.SummaryVec.With(prometheus.Labels(s.Pairs)).Observe(float64(value)) |
134 | 134 | } |
135 | 135 | |
136 | func (s prometheusSummary) Distribution() ([]metrics.Bucket, []metrics.Quantile) { | |
136 | func (s summary) Distribution() ([]metrics.Bucket, []metrics.Quantile) { | |
137 | 137 | // TODO(pb): see https://github.com/prometheus/client_golang/issues/58 |
138 | 138 | return []metrics.Bucket{}, []metrics.Quantile{} |
139 | 139 | } |
140 | 140 | |
141 | type prometheusHistogram struct { | |
141 | type histogram struct { | |
142 | 142 | *prometheus.HistogramVec |
143 | 143 | name string |
144 | 144 | Pairs map[string]string |
152 | 152 | func NewHistogram(opts prometheus.HistogramOpts, fieldKeys []string) metrics.Histogram { |
153 | 153 | m := prometheus.NewHistogramVec(opts, fieldKeys) |
154 | 154 | prometheus.MustRegister(m) |
155 | return prometheusHistogram{ | |
155 | return histogram{ | |
156 | 156 | HistogramVec: m, |
157 | 157 | name: opts.Name, |
158 | 158 | Pairs: pairsFrom(fieldKeys), |
159 | 159 | } |
160 | 160 | } |
161 | 161 | |
162 | func (h prometheusHistogram) Name() string { return h.name } | |
163 | ||
164 | func (h prometheusHistogram) With(f metrics.Field) metrics.Histogram { | |
165 | return prometheusHistogram{ | |
162 | func (h histogram) Name() string { return h.name } | |
163 | ||
164 | func (h histogram) With(f metrics.Field) metrics.Histogram { | |
165 | return histogram{ | |
166 | 166 | HistogramVec: h.HistogramVec, |
167 | 167 | name: h.name, |
168 | 168 | Pairs: merge(h.Pairs, f), |
169 | 169 | } |
170 | 170 | } |
171 | 171 | |
172 | func (h prometheusHistogram) Observe(value int64) { | |
172 | func (h histogram) Observe(value int64) { | |
173 | 173 | h.HistogramVec.With(prometheus.Labels(h.Pairs)).Observe(float64(value)) |
174 | 174 | } |
175 | 175 | |
176 | func (h prometheusHistogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) { | |
176 | func (h histogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) { | |
177 | 177 | // TODO(pb): see https://github.com/prometheus/client_golang/issues/58 |
178 | 178 | return []metrics.Bucket{}, []metrics.Quantile{} |
179 | 179 | } |
29 | 29 | |
30 | 30 | const maxBufferSize = 1400 // bytes |
31 | 31 | |
32 | type statsdCounter struct { | |
32 | type counter struct { | |
33 | 33 | key string |
34 | 34 | c chan string |
35 | 35 | } |
47 | 47 | // NewCounterTick is the same as NewCounter, but allows the user to pass in a |
48 | 48 | // ticker channel instead of invoking time.Tick. |
49 | 49 | func NewCounterTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Counter { |
50 | c := &statsdCounter{ | |
50 | c := &counter{ | |
51 | 51 | key: key, |
52 | 52 | c: make(chan string), |
53 | 53 | } |
55 | 55 | return c |
56 | 56 | } |
57 | 57 | |
58 | func (c *statsdCounter) Name() string { return c.key } | |
59 | ||
60 | func (c *statsdCounter) With(metrics.Field) metrics.Counter { return c } | |
61 | ||
62 | func (c *statsdCounter) Add(delta uint64) { c.c <- fmt.Sprintf("%d|c", delta) } | |
63 | ||
64 | type statsdGauge struct { | |
58 | func (c *counter) Name() string { return c.key } | |
59 | ||
60 | func (c *counter) With(metrics.Field) metrics.Counter { return c } | |
61 | ||
62 | func (c *counter) Add(delta uint64) { c.c <- fmt.Sprintf("%d|c", delta) } | |
63 | ||
64 | type gauge struct { | |
65 | 65 | key string |
66 | 66 | lastValue uint64 // math.Float64frombits |
67 | 67 | g chan string |
80 | 80 | // NewGaugeTick is the same as NewGauge, but allows the user to pass in a ticker |
81 | 81 | // channel instead of invoking time.Tick. |
82 | 82 | func NewGaugeTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Gauge { |
83 | g := &statsdGauge{ | |
83 | g := &gauge{ | |
84 | 84 | key: key, |
85 | 85 | g: make(chan string), |
86 | 86 | } |
88 | 88 | return g |
89 | 89 | } |
90 | 90 | |
91 | func (g *statsdGauge) Name() string { return g.key } | |
92 | ||
93 | func (g *statsdGauge) With(metrics.Field) metrics.Gauge { return g } | |
94 | ||
95 | func (g *statsdGauge) Add(delta float64) { | |
91 | func (g *gauge) Name() string { return g.key } | |
92 | ||
93 | func (g *gauge) With(metrics.Field) metrics.Gauge { return g } | |
94 | ||
95 | func (g *gauge) Add(delta float64) { | |
96 | 96 | // https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges |
97 | 97 | sign := "+" |
98 | 98 | if delta < 0 { |
101 | 101 | g.g <- fmt.Sprintf("%s%f|g", sign, delta) |
102 | 102 | } |
103 | 103 | |
104 | func (g *statsdGauge) Set(value float64) { | |
104 | func (g *gauge) Set(value float64) { | |
105 | 105 | atomic.StoreUint64(&g.lastValue, math.Float64bits(value)) |
106 | 106 | g.g <- fmt.Sprintf("%f|g", value) |
107 | 107 | } |
108 | 108 | |
109 | func (g *statsdGauge) Get() float64 { | |
109 | func (g *gauge) Get() float64 { | |
110 | 110 | return math.Float64frombits(atomic.LoadUint64(&g.lastValue)) |
111 | 111 | } |
112 | 112 | |
137 | 137 | return c |
138 | 138 | } |
139 | 139 | |
140 | type statsdHistogram struct { | |
140 | type histogram struct { | |
141 | 141 | key string |
142 | 142 | h chan string |
143 | 143 | } |
166 | 166 | // NewHistogramTick is the same as NewHistogram, but allows the user to pass a |
167 | 167 | // ticker channel instead of invoking time.Tick. |
168 | 168 | func NewHistogramTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Histogram { |
169 | h := &statsdHistogram{ | |
169 | h := &histogram{ | |
170 | 170 | key: key, |
171 | 171 | h: make(chan string), |
172 | 172 | } |
174 | 174 | return h |
175 | 175 | } |
176 | 176 | |
177 | func (h *statsdHistogram) Name() string { return h.key } | |
178 | ||
179 | func (h *statsdHistogram) With(metrics.Field) metrics.Histogram { return h } | |
180 | ||
181 | func (h *statsdHistogram) Observe(value int64) { | |
177 | func (h *histogram) Name() string { return h.key } | |
178 | ||
179 | func (h *histogram) With(metrics.Field) metrics.Histogram { return h } | |
180 | ||
181 | func (h *histogram) Observe(value int64) { | |
182 | 182 | h.h <- fmt.Sprintf("%d|ms", value) |
183 | 183 | } |
184 | 184 | |
185 | func (h *statsdHistogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) { | |
185 | func (h *histogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) { | |
186 | 186 | // TODO(pb): no way to do this without introducing e.g. codahale/hdrhistogram |
187 | 187 | return []metrics.Bucket{}, []metrics.Quantile{} |
188 | 188 | } |