Codebase list golang-github-go-kit-kit / ec0f171
metrics: prometheus: fully lean on MetricOpts Following Björn's advice, don't try to itemize each configuration option as constructor params; rather, take a prometheus.{Summary,Histogram}Opts struct directly. Peter Bourgon 8 years ago
3 changed file(s) with 37 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
9595 quantiles := []int{50, 90, 99}
9696 h := metrics.NewMultiHistogram(
9797 expvar.NewHistogram("omicron", 0, 100, 3, quantiles...),
98 prometheus.NewSummary("test", "multi_histogram", "nu", "Nu histogram.", []string{}),
98 prometheus.NewSummary(stdprometheus.SummaryOpts{
99 Namespace: "test",
100 Subsystem: "multi_histogram",
101 Name: "nu",
102 Help: "Nu histogram.",
103 }, []string{}),
99104 )
100105
101106 const seed, mean, stdev int64 = 123, 50, 10
11 package prometheus
22
33 import (
4 "time"
5
64 "github.com/prometheus/client_golang/prometheus"
75
86 "github.com/peterbourgon/gokit/metrics"
138136 Pairs map[string]string
139137 }
140138
141 // NewSummary returns a new Histogram backed by a Prometheus summary. It uses
142 // a 10-second max age for bucketing, emulating statsd. The histogram is
143 // automatically registered via prometheus.Register.
144 func NewSummary(namespace, subsystem, name, help string, fieldKeys []string) metrics.Histogram {
145 return NewSummaryWithLabels(namespace, subsystem, name, help, fieldKeys, prometheus.Labels{})
146 }
147
148 // NewSummaryWithLabels is the same as NewSummary, but attaches a set of const
149 // label pairs to the metric.
150 func NewSummaryWithLabels(namespace, subsystem, name, help string, fieldKeys []string, constLabels prometheus.Labels) metrics.Histogram {
151 m := prometheus.NewSummaryVec(
152 prometheus.SummaryOpts{
153 Namespace: namespace,
154 Subsystem: subsystem,
155 Name: name,
156 Help: help,
157 ConstLabels: constLabels,
158 MaxAge: 10 * time.Second,
159 },
160 fieldKeys,
161 )
162 prometheus.MustRegister(m)
163
139 // NewSummary returns a new Histogram backed by a Prometheus summary. The
140 // histogram is automatically registered via prometheus.Register.
141 //
142 // For more information on Prometheus histograms and summaries, refer to
143 // http://prometheus.io/docs/practices/histograms.
144 func NewSummary(opts prometheus.SummaryOpts, fieldKeys []string) metrics.Histogram {
145 m := prometheus.NewSummaryVec(opts, fieldKeys)
146 prometheus.MustRegister(m)
164147 return prometheusSummary{
165148 SummaryVec: m,
166149 Pairs: pairsFrom(fieldKeys),
183166 Pairs map[string]string
184167 }
185168
186 // NewHistogram returns a new Histogram backed by a Prometheus Histogram.
187 // Observations are counted into buckets; see Prometheus documentation for
188 // details. The histogram is automatically registered via prometheus.Register.
189 func NewHistogram(namespace, subsystem, name, help string, fieldKeys []string, buckets []float64) metrics.Histogram {
190 return NewHistogramWithLabels(namespace, subsystem, name, help, fieldKeys, buckets, prometheus.Labels{})
191 }
192
193 // NewHistogramWithLabels is the same as NewHistogram, but attaches a set of const
194 // label pairs to the metric.
195 func NewHistogramWithLabels(namespace, subsystem, name, help string, fieldKeys []string, buckets []float64, constLabels prometheus.Labels) metrics.Histogram {
196 m := prometheus.NewHistogramVec(
197 prometheus.HistogramOpts{
198 Namespace: namespace,
199 Subsystem: subsystem,
200 Name: name,
201 Help: help,
202 ConstLabels: constLabels,
203 Buckets: buckets,
204 },
205 fieldKeys,
206 )
207 prometheus.MustRegister(m)
208
169 // NewHistogram returns a new Histogram backed by a Prometheus Histogram. The
170 // histogram is automatically registered via prometheus.Register.
171 //
172 // For more information on Prometheus histograms and summaries, refer to
173 // http://prometheus.io/docs/practices/histograms.
174 func NewHistogram(opts prometheus.HistogramOpts, fieldKeys []string) metrics.Histogram {
175 m := prometheus.NewHistogramVec(opts, fieldKeys)
176 prometheus.MustRegister(m)
209177 return prometheusHistogram{
210178 HistogramVec: m,
211179 Pairs: pairsFrom(fieldKeys),
22 import (
33 "strings"
44 "testing"
5
6 stdprometheus "github.com/prometheus/client_golang/prometheus"
57
68 "github.com/peterbourgon/gokit/metrics"
79 "github.com/peterbourgon/gokit/metrics/prometheus"
7981 }
8082
8183 func TestPrometheusSummary(t *testing.T) {
82 h := prometheus.NewSummary("test", "prometheus_summary_histogram", "foobar", "Qwerty asdf.", []string{})
84 h := prometheus.NewSummary(stdprometheus.SummaryOpts{
85 Namespace: "test",
86 Subsystem: "prometheus_summary_histogram",
87 Name: "foobar",
88 Help: "Qwerty asdf.",
89 }, []string{})
8390
8491 const mean, stdev int64 = 50, 10
8592 teststat.PopulateNormalHistogram(t, h, 34, mean, stdev)
8895
8996 func TestPrometheusHistogram(t *testing.T) {
9097 buckets := []float64{20, 40, 60, 80, 100}
91 h := prometheus.NewHistogram("test", "prometheus_histogram_histogram", "quux", "Qwerty asdf.", []string{}, buckets)
98 h := prometheus.NewHistogram(stdprometheus.HistogramOpts{
99 Namespace: "test",
100 Subsystem: "prometheus_histogram_histogram",
101 Name: "quux",
102 Help: "Qwerty asdf.",
103 Buckets: buckets,
104 }, []string{})
92105
93106 const mean, stdev int64 = 50, 10
94107 teststat.PopulateNormalHistogram(t, h, 34, mean, stdev)