Codebase list golang-github-go-kit-kit / 4479f01
Merge pull request #267 from DSchalla/metrics/naming Remove naming prefixes from metric backends (closes #266) Peter Bourgon 7 years ago
3 changed file(s) with 82 addition(s) and 82 deletion(s). Raw diff Collapse all Expand all
2323
2424 const maxBufferSize = 1400 // bytes
2525
26 type dogstatsdCounter struct {
26 type counter struct {
2727 key string
2828 c chan string
2929 tags []metrics.Field
4141 // NewCounterTick is the same as NewCounter, but allows the user to pass in a
4242 // ticker channel instead of invoking time.Tick.
4343 func NewCounterTick(w io.Writer, key string, reportTicker <-chan time.Time, tags []metrics.Field) metrics.Counter {
44 c := &dogstatsdCounter{
44 c := &counter{
4545 key: key,
4646 c: make(chan string),
4747 tags: tags,
5050 return c
5151 }
5252
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{
5757 key: c.key,
5858 c: c.c,
5959 tags: append(c.tags, f),
6060 }
6161 }
6262
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 {
6666 key string
6767 lastValue uint64 // math.Float64frombits
6868 g chan string
8181 // NewGaugeTick is the same as NewGauge, but allows the user to pass in a ticker
8282 // channel instead of invoking time.Tick.
8383 func NewGaugeTick(w io.Writer, key string, reportTicker <-chan time.Time, tags []metrics.Field) metrics.Gauge {
84 g := &dogstatsdGauge{
84 g := &gauge{
8585 key: key,
8686 g: make(chan string),
8787 tags: tags,
9090 return g
9191 }
9292
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{
9797 key: g.key,
9898 lastValue: g.lastValue,
9999 g: g.g,
101101 }
102102 }
103103
104 func (g *dogstatsdGauge) Add(delta float64) {
104 func (g *gauge) Add(delta float64) {
105105 // https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges
106106 sign := "+"
107107 if delta < 0 {
110110 g.g <- applyTags(fmt.Sprintf("%s%f|g", sign, delta), g.tags)
111111 }
112112
113 func (g *dogstatsdGauge) Set(value float64) {
113 func (g *gauge) Set(value float64) {
114114 atomic.StoreUint64(&g.lastValue, math.Float64bits(value))
115115 g.g <- applyTags(fmt.Sprintf("%f|g", value), g.tags)
116116 }
117117
118 func (g *dogstatsdGauge) Get() float64 {
118 func (g *gauge) Get() float64 {
119119 return math.Float64frombits(atomic.LoadUint64(&g.lastValue))
120120 }
121121
146146 return c
147147 }
148148
149 type dogstatsdHistogram struct {
149 type histogram struct {
150150 key string
151151 h chan string
152152 tags []metrics.Field
176176 // NewHistogramTick is the same as NewHistogram, but allows the user to pass a
177177 // ticker channel instead of invoking time.Tick.
178178 func NewHistogramTick(w io.Writer, key string, reportTicker <-chan time.Time, tags []metrics.Field) metrics.Histogram {
179 h := &dogstatsdHistogram{
179 h := &histogram{
180180 key: key,
181181 h: make(chan string),
182182 tags: tags,
185185 return h
186186 }
187187
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{
192192 key: h.key,
193193 h: h.h,
194194 tags: append(h.tags, f),
195195 }
196196 }
197197
198 func (h *dogstatsdHistogram) Observe(value int64) {
198 func (h *histogram) Observe(value int64) {
199199 h.h <- applyTags(fmt.Sprintf("%d|ms", value), h.tags)
200200 }
201201
202 func (h *dogstatsdHistogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
202 func (h *histogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
203203 // TODO(pb): no way to do this without introducing e.g. codahale/hdrhistogram
204204 return []metrics.Bucket{}, []metrics.Quantile{}
205205 }
1313 // PrometheusLabelValueUnknown.
1414 var PrometheusLabelValueUnknown = "unknown"
1515
16 type prometheusCounter struct {
16 type counter struct {
1717 *prometheus.CounterVec
1818 name string
1919 Pairs map[string]string
2828 for _, fieldName := range fieldKeys {
2929 p[fieldName] = PrometheusLabelValueUnknown
3030 }
31 return prometheusCounter{
31 return counter{
3232 CounterVec: m,
3333 name: opts.Name,
3434 Pairs: p,
3535 }
3636 }
3737
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{
4242 CounterVec: c.CounterVec,
4343 name: c.name,
4444 Pairs: merge(c.Pairs, f),
4545 }
4646 }
4747
48 func (c prometheusCounter) Add(delta uint64) {
48 func (c counter) Add(delta uint64) {
4949 c.CounterVec.With(prometheus.Labels(c.Pairs)).Add(float64(delta))
5050 }
5151
52 type prometheusGauge struct {
52 type gauge struct {
5353 *prometheus.GaugeVec
5454 name string
5555 Pairs map[string]string
6060 func NewGauge(opts prometheus.GaugeOpts, fieldKeys []string) metrics.Gauge {
6161 m := prometheus.NewGaugeVec(opts, fieldKeys)
6262 prometheus.MustRegister(m)
63 return prometheusGauge{
63 return gauge{
6464 GaugeVec: m,
6565 name: opts.Name,
6666 Pairs: pairsFrom(fieldKeys),
6767 }
6868 }
6969
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{
7474 GaugeVec: g.GaugeVec,
7575 name: g.name,
7676 Pairs: merge(g.Pairs, f),
7777 }
7878 }
7979
80 func (g prometheusGauge) Set(value float64) {
80 func (g gauge) Set(value float64) {
8181 g.GaugeVec.With(prometheus.Labels(g.Pairs)).Set(value)
8282 }
8383
84 func (g prometheusGauge) Add(delta float64) {
84 func (g gauge) Add(delta float64) {
8585 g.GaugeVec.With(prometheus.Labels(g.Pairs)).Add(delta)
8686 }
8787
88 func (g prometheusGauge) Get() float64 {
88 func (g gauge) Get() float64 {
8989 // TODO(pb): see https://github.com/prometheus/client_golang/issues/58
9090 return 0.0
9191 }
9898 prometheus.MustRegister(prometheus.NewGaugeFunc(opts, callback))
9999 }
100100
101 type prometheusSummary struct {
101 type summary struct {
102102 *prometheus.SummaryVec
103103 name string
104104 Pairs map[string]string
112112 func NewSummary(opts prometheus.SummaryOpts, fieldKeys []string) metrics.Histogram {
113113 m := prometheus.NewSummaryVec(opts, fieldKeys)
114114 prometheus.MustRegister(m)
115 return prometheusSummary{
115 return summary{
116116 SummaryVec: m,
117117 name: opts.Name,
118118 Pairs: pairsFrom(fieldKeys),
119119 }
120120 }
121121
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{
126126 SummaryVec: s.SummaryVec,
127127 name: s.name,
128128 Pairs: merge(s.Pairs, f),
129129 }
130130 }
131131
132 func (s prometheusSummary) Observe(value int64) {
132 func (s summary) Observe(value int64) {
133133 s.SummaryVec.With(prometheus.Labels(s.Pairs)).Observe(float64(value))
134134 }
135135
136 func (s prometheusSummary) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
136 func (s summary) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
137137 // TODO(pb): see https://github.com/prometheus/client_golang/issues/58
138138 return []metrics.Bucket{}, []metrics.Quantile{}
139139 }
140140
141 type prometheusHistogram struct {
141 type histogram struct {
142142 *prometheus.HistogramVec
143143 name string
144144 Pairs map[string]string
152152 func NewHistogram(opts prometheus.HistogramOpts, fieldKeys []string) metrics.Histogram {
153153 m := prometheus.NewHistogramVec(opts, fieldKeys)
154154 prometheus.MustRegister(m)
155 return prometheusHistogram{
155 return histogram{
156156 HistogramVec: m,
157157 name: opts.Name,
158158 Pairs: pairsFrom(fieldKeys),
159159 }
160160 }
161161
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{
166166 HistogramVec: h.HistogramVec,
167167 name: h.name,
168168 Pairs: merge(h.Pairs, f),
169169 }
170170 }
171171
172 func (h prometheusHistogram) Observe(value int64) {
172 func (h histogram) Observe(value int64) {
173173 h.HistogramVec.With(prometheus.Labels(h.Pairs)).Observe(float64(value))
174174 }
175175
176 func (h prometheusHistogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
176 func (h histogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
177177 // TODO(pb): see https://github.com/prometheus/client_golang/issues/58
178178 return []metrics.Bucket{}, []metrics.Quantile{}
179179 }
2929
3030 const maxBufferSize = 1400 // bytes
3131
32 type statsdCounter struct {
32 type counter struct {
3333 key string
3434 c chan string
3535 }
4747 // NewCounterTick is the same as NewCounter, but allows the user to pass in a
4848 // ticker channel instead of invoking time.Tick.
4949 func NewCounterTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Counter {
50 c := &statsdCounter{
50 c := &counter{
5151 key: key,
5252 c: make(chan string),
5353 }
5555 return c
5656 }
5757
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 {
6565 key string
6666 lastValue uint64 // math.Float64frombits
6767 g chan string
8080 // NewGaugeTick is the same as NewGauge, but allows the user to pass in a ticker
8181 // channel instead of invoking time.Tick.
8282 func NewGaugeTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Gauge {
83 g := &statsdGauge{
83 g := &gauge{
8484 key: key,
8585 g: make(chan string),
8686 }
8888 return g
8989 }
9090
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) {
9696 // https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges
9797 sign := "+"
9898 if delta < 0 {
101101 g.g <- fmt.Sprintf("%s%f|g", sign, delta)
102102 }
103103
104 func (g *statsdGauge) Set(value float64) {
104 func (g *gauge) Set(value float64) {
105105 atomic.StoreUint64(&g.lastValue, math.Float64bits(value))
106106 g.g <- fmt.Sprintf("%f|g", value)
107107 }
108108
109 func (g *statsdGauge) Get() float64 {
109 func (g *gauge) Get() float64 {
110110 return math.Float64frombits(atomic.LoadUint64(&g.lastValue))
111111 }
112112
137137 return c
138138 }
139139
140 type statsdHistogram struct {
140 type histogram struct {
141141 key string
142142 h chan string
143143 }
166166 // NewHistogramTick is the same as NewHistogram, but allows the user to pass a
167167 // ticker channel instead of invoking time.Tick.
168168 func NewHistogramTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Histogram {
169 h := &statsdHistogram{
169 h := &histogram{
170170 key: key,
171171 h: make(chan string),
172172 }
174174 return h
175175 }
176176
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) {
182182 h.h <- fmt.Sprintf("%d|ms", value)
183183 }
184184
185 func (h *statsdHistogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
185 func (h *histogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
186186 // TODO(pb): no way to do this without introducing e.g. codahale/hdrhistogram
187187 return []metrics.Bucket{}, []metrics.Quantile{}
188188 }