MCPcopy
hub / github.com/prometheus/client_golang / main

Function main

examples/random/main.go:74–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

72}
73
74func main() {
75 var (
76 addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
77 uniformDomain = flag.Float64("uniform.domain", 0.0002, "The domain for the uniform distribution.")
78 normDomain = flag.Float64("normal.domain", 0.0002, "The domain for the normal distribution.")
79 normMean = flag.Float64("normal.mean", 0.00001, "The mean for the normal distribution.")
80 oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
81 )
82
83 flag.Parse()
84
85 // Create a non-global registry.
86 reg := prometheus.NewRegistry()
87
88 // Create new metrics and register them using the custom registry.
89 m := NewMetrics(reg, *normMean, *normDomain)
90 // Add Go module build info.
91 reg.MustRegister(collectors.NewBuildInfoCollector())
92
93 start := time.Now()
94
95 oscillationFactor := func() float64 {
96 return 2 + math.Sin(math.Sin(2*math.Pi*float64(time.Since(start))/float64(*oscillationPeriod)))
97 }
98
99 // Periodically record some sample latencies for the three services.
100 go func() {
101 for {
102 v := rand.Float64() * *uniformDomain
103 m.rpcDurations.WithLabelValues("uniform").Observe(v)
104 time.Sleep(time.Duration(100*oscillationFactor()) * time.Millisecond)
105 }
106 }()
107
108 go func() {
109 for {
110 v := (rand.NormFloat64() * *normDomain) + *normMean
111 m.rpcDurations.WithLabelValues("normal").Observe(v)
112 // Demonstrate exemplar support with a dummy ID. This
113 // would be something like a trace ID in a real
114 // application. Note the necessary type assertion. We
115 // already know that rpcDurationsHistogram implements
116 // the ExemplarObserver interface and thus don't need to
117 // check the outcome of the type assertion.
118 m.rpcDurationsHistogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
119 v, prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))},
120 )
121 time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)
122 }
123 }()
124
125 go func() {
126 for {
127 v := rand.ExpFloat64() / 1e6
128 m.rpcDurations.WithLabelValues("exponential").Observe(v)
129 time.Sleep(time.Duration(50*oscillationFactor()) * time.Millisecond)
130 }
131 }()

Callers

nothing calls this directly

Calls 10

MustRegisterMethod · 0.95
NewRegistryFunction · 0.92
NewBuildInfoCollectorFunction · 0.92
HandlerForFunction · 0.92
NewMetricsFunction · 0.85
HandleMethod · 0.80
ObserveMethod · 0.65
WithLabelValuesMethod · 0.65
ObserveWithExemplarMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected