Merge pull request #1152 from jessicalins/update-to-custom-reg · prometheus/client_golang@0859bb8 (original) (raw)
`@@ -30,45 +30,57 @@ import (
`
30
30
`"github.com/prometheus/client_golang/prometheus/promhttp"
`
31
31
`)
`
32
32
``
33
``
`-
func main() {
`
34
``
`-
var (
`
35
``
`-
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
`
36
``
`-
uniformDomain = flag.Float64("uniform.domain", 0.0002, "The domain for the uniform distribution.")
`
37
``
`-
normDomain = flag.Float64("normal.domain", 0.0002, "The domain for the normal distribution.")
`
38
``
`-
normMean = flag.Float64("normal.mean", 0.00001, "The mean for the normal distribution.")
`
39
``
`-
oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
`
40
``
`-
)
`
41
``
-
42
``
`-
flag.Parse()
`
``
33
`+
type metrics struct {
`
``
34
`+
rpcDurations *prometheus.SummaryVec
`
``
35
`+
rpcDurationsHistogram prometheus.Histogram
`
``
36
`+
}
`
43
37
``
44
``
`-
var (
`
45
``
`-
// Create a summary to track fictional interservice RPC latencies for three
`
``
38
`+
func NewMetrics(reg prometheus.Registerer, normMean, normDomain float64) *metrics {
`
``
39
`+
m := &metrics{
`
``
40
`+
// Create a summary to track fictional inter service RPC latencies for three
`
46
41
`// distinct services with different latency distributions. These services are
`
47
42
`// differentiated via a "service" label.
`
48
``
`-
rpcDurations = prometheus.NewSummaryVec(
`
``
43
`+
rpcDurations: prometheus.NewSummaryVec(
`
49
44
` prometheus.SummaryOpts{
`
50
45
`Name: "rpc_durations_seconds",
`
51
46
`Help: "RPC latency distributions.",
`
52
47
`Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
`
53
48
` },
`
54
49
` []string{"service"},
`
55
``
`-
)
`
``
50
`+
),
`
56
51
`// The same as above, but now as a histogram, and only for the normal
`
57
52
`// distribution. The buckets are targeted to the parameters of the
`
58
53
`// normal distribution, with 20 buckets centered on the mean, each
`
59
54
`// half-sigma wide.
`
60
``
`-
rpcDurationsHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
`
``
55
`+
rpcDurationsHistogram: prometheus.NewHistogram(prometheus.HistogramOpts{
`
61
56
`Name: "rpc_durations_histogram_seconds",
`
62
57
`Help: "RPC latency distributions.",
`
63
``
`-
Buckets: prometheus.LinearBuckets(*normMean-5normDomain, .5normDomain, 20),
`
64
``
`-
})
`
``
58
`+
Buckets: prometheus.LinearBuckets(normMean-5normDomain, .5normDomain, 20),
`
``
59
`+
}),
`
``
60
`+
}
`
``
61
`+
reg.MustRegister(m.rpcDurations)
`
``
62
`+
reg.MustRegister(m.rpcDurationsHistogram)
`
``
63
`+
return m
`
``
64
`+
}
`
``
65
+
``
66
`+
func main() {
`
``
67
`+
var (
`
``
68
`+
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
`
``
69
`+
uniformDomain = flag.Float64("uniform.domain", 0.0002, "The domain for the uniform distribution.")
`
``
70
`+
normDomain = flag.Float64("normal.domain", 0.0002, "The domain for the normal distribution.")
`
``
71
`+
normMean = flag.Float64("normal.mean", 0.00001, "The mean for the normal distribution.")
`
``
72
`+
oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
`
65
73
` )
`
66
74
``
67
``
`-
// Register the summary and the histogram with Prometheus's default registry.
`
68
``
`-
prometheus.MustRegister(rpcDurations)
`
69
``
`-
prometheus.MustRegister(rpcDurationsHistogram)
`
``
75
`+
flag.Parse()
`
``
76
+
``
77
`+
// Create a non-global registry.
`
``
78
`+
reg := prometheus.NewRegistry()
`
``
79
+
``
80
`+
// Create new metrics and register them using the custom registry.
`
``
81
`+
m := NewMetrics(reg, *normMean, *normDomain)
`
70
82
`// Add Go module build info.
`
71
``
`-
prometheus.MustRegister(collectors.NewBuildInfoCollector())
`
``
83
`+
reg.MustRegister(collectors.NewBuildInfoCollector())
`
72
84
``
73
85
`start := time.Now()
`
74
86
``
`@@ -80,22 +92,22 @@ func main() {
`
80
92
`go func() {
`
81
93
`for {
`
82
94
`v := rand.Float64() * *uniformDomain
`
83
``
`-
rpcDurations.WithLabelValues("uniform").Observe(v)
`
``
95
`+
m.rpcDurations.WithLabelValues("uniform").Observe(v)
`
84
96
`time.Sleep(time.Duration(100*oscillationFactor()) * time.Millisecond)
`
85
97
` }
`
86
98
` }()
`
87
99
``
88
100
`go func() {
`
89
101
`for {
`
90
102
`v := (rand.NormFloat64() * *normDomain) + *normMean
`
91
``
`-
rpcDurations.WithLabelValues("normal").Observe(v)
`
``
103
`+
m.rpcDurations.WithLabelValues("normal").Observe(v)
`
92
104
`// Demonstrate exemplar support with a dummy ID. This
`
93
105
`// would be something like a trace ID in a real
`
94
106
`// application. Note the necessary type assertion. We
`
95
107
`// already know that rpcDurationsHistogram implements
`
96
108
`// the ExemplarObserver interface and thus don't need to
`
97
109
`// check the outcome of the type assertion.
`
98
``
`-
rpcDurationsHistogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
`
``
110
`+
m.rpcDurationsHistogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
`
99
111
`v, prometheus.Labels{"dummyID": fmt.Sprint(rand.Intn(100000))},
`
100
112
` )
`
101
113
`time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)
`
`@@ -105,17 +117,19 @@ func main() {
`
105
117
`go func() {
`
106
118
`for {
`
107
119
`v := rand.ExpFloat64() / 1e6
`
108
``
`-
rpcDurations.WithLabelValues("exponential").Observe(v)
`
``
120
`+
m.rpcDurations.WithLabelValues("exponential").Observe(v)
`
109
121
`time.Sleep(time.Duration(50*oscillationFactor()) * time.Millisecond)
`
110
122
` }
`
111
123
` }()
`
112
124
``
113
125
`// Expose the registered metrics via HTTP.
`
114
126
`http.Handle("/metrics", promhttp.HandlerFor(
`
115
``
`-
prometheus.DefaultGatherer,
`
``
127
`+
reg,
`
116
128
` promhttp.HandlerOpts{
`
117
129
`// Opt into OpenMetrics to support exemplars.
`
118
130
`EnableOpenMetrics: true,
`
``
131
`+
// Pass custom registry
`
``
132
`+
Registry: reg,
`
119
133
` },
`
120
134
` ))
`
121
135
`log.Fatal(http.ListenAndServe(*addr, nil))
`