Skip to content

Commit f78dc19

Browse files
committed
Move flags and metrics into main()
Fixes #933. Signed-off-by: beorn7 <[email protected]>
1 parent 679eb0d commit f78dc19

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

examples/random/main.go

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,45 @@ import (
2929
"github.com/prometheus/client_golang/prometheus/promhttp"
3030
)
3131

32-
var (
33-
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
34-
uniformDomain = flag.Float64("uniform.domain", 0.0002, "The domain for the uniform distribution.")
35-
normDomain = flag.Float64("normal.domain", 0.0002, "The domain for the normal distribution.")
36-
normMean = flag.Float64("normal.mean", 0.00001, "The mean for the normal distribution.")
37-
oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
38-
)
32+
func main() {
33+
var (
34+
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
35+
uniformDomain = flag.Float64("uniform.domain", 0.0002, "The domain for the uniform distribution.")
36+
normDomain = flag.Float64("normal.domain", 0.0002, "The domain for the normal distribution.")
37+
normMean = flag.Float64("normal.mean", 0.00001, "The mean for the normal distribution.")
38+
oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
39+
)
3940

40-
var (
41-
// Create a summary to track fictional interservice RPC latencies for three
42-
// distinct services with different latency distributions. These services are
43-
// differentiated via a "service" label.
44-
rpcDurations = prometheus.NewSummaryVec(
45-
prometheus.SummaryOpts{
46-
Name: "rpc_durations_seconds",
47-
Help: "RPC latency distributions.",
48-
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
49-
},
50-
[]string{"service"},
41+
flag.Parse()
42+
43+
var (
44+
// Create a summary to track fictional interservice RPC latencies for three
45+
// distinct services with different latency distributions. These services are
46+
// differentiated via a "service" label.
47+
rpcDurations = prometheus.NewSummaryVec(
48+
prometheus.SummaryOpts{
49+
Name: "rpc_durations_seconds",
50+
Help: "RPC latency distributions.",
51+
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
52+
},
53+
[]string{"service"},
54+
)
55+
// The same as above, but now as a histogram, and only for the normal
56+
// distribution. The buckets are targeted to the parameters of the
57+
// normal distribution, with 20 buckets centered on the mean, each
58+
// half-sigma wide.
59+
rpcDurationsHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
60+
Name: "rpc_durations_histogram_seconds",
61+
Help: "RPC latency distributions.",
62+
Buckets: prometheus.LinearBuckets(*normMean-5**normDomain, .5**normDomain, 20),
63+
})
5164
)
52-
// The same as above, but now as a histogram, and only for the normal
53-
// distribution. The buckets are targeted to the parameters of the
54-
// normal distribution, with 20 buckets centered on the mean, each
55-
// half-sigma wide.
56-
rpcDurationsHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
57-
Name: "rpc_durations_histogram_seconds",
58-
Help: "RPC latency distributions.",
59-
Buckets: prometheus.LinearBuckets(*normMean-5**normDomain, .5**normDomain, 20),
60-
})
61-
)
6265

63-
func init() {
6466
// Register the summary and the histogram with Prometheus's default registry.
6567
prometheus.MustRegister(rpcDurations)
6668
prometheus.MustRegister(rpcDurationsHistogram)
6769
// Add Go module build info.
6870
prometheus.MustRegister(prometheus.NewBuildInfoCollector())
69-
}
70-
71-
func main() {
72-
flag.Parse()
7371

7472
start := time.Now()
7573

0 commit comments

Comments
 (0)