Skip to content

Commit 764e6a7

Browse files
committed
Merge pull request #82 from prometheus/beorn7/histogram
Add a histogram to the random example.
2 parents 54238de + 28973f4 commit 764e6a7

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

examples/random/main.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
var (
3030
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
3131
uniformDomain = flag.Float64("uniform.domain", 200, "The domain for the uniform distribution.")
32-
normDomain = flag.Float64("exponential.domain", 200, "The domain for the normal distribution.")
33-
normMean = flag.Float64("exponential.mean", 10, "The mean for the normal distribution.")
32+
normDomain = flag.Float64("normal.domain", 200, "The domain for the normal distribution.")
33+
normMean = flag.Float64("normal.mean", 10, "The mean for the normal distribution.")
3434
oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
3535
)
3636

@@ -45,11 +45,21 @@ var (
4545
},
4646
[]string{"service"},
4747
)
48+
// The same as above, but now as a histogram, and only for the normal
49+
// distribution. The buckets are targeted to the parameters of the
50+
// normal distribution, with 20 buckets centered on the mean, each
51+
// half-sigma wide.
52+
rpcDurationsHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
53+
Name: "rpc_durations_histogram_microseconds",
54+
Help: "RPC latency distributions.",
55+
Buckets: prometheus.LinearBuckets(*normMean-5**normDomain, .5**normDomain, 20),
56+
})
4857
)
4958

5059
func init() {
51-
// Register the summary with Prometheus's default registry.
60+
// Register the summary and the histogram with Prometheus's default registry.
5261
prometheus.MustRegister(rpcDurations)
62+
prometheus.MustRegister(rpcDurationsHistogram)
5363
}
5464

5565
func main() {
@@ -64,21 +74,25 @@ func main() {
6474
// Periodically record some sample latencies for the three services.
6575
go func() {
6676
for {
67-
rpcDurations.WithLabelValues("uniform").Observe(rand.Float64() * *uniformDomain)
77+
v := rand.Float64() * *uniformDomain
78+
rpcDurations.WithLabelValues("uniform").Observe(v)
6879
time.Sleep(time.Duration(100*oscillationFactor()) * time.Millisecond)
6980
}
7081
}()
7182

7283
go func() {
7384
for {
74-
rpcDurations.WithLabelValues("normal").Observe((rand.NormFloat64() * *normDomain) + *normMean)
85+
v := (rand.NormFloat64() * *normDomain) + *normMean
86+
rpcDurations.WithLabelValues("normal").Observe(v)
87+
rpcDurationsHistogram.Observe(v)
7588
time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)
7689
}
7790
}()
7891

7992
go func() {
8093
for {
81-
rpcDurations.WithLabelValues("exponential").Observe(rand.ExpFloat64())
94+
v := rand.ExpFloat64()
95+
rpcDurations.WithLabelValues("exponential").Observe(v)
8296
time.Sleep(time.Duration(50*oscillationFactor()) * time.Millisecond)
8397
}
8498
}()

0 commit comments

Comments
 (0)