| 1 |
// Copyright 2015 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// A simple example exposing fictional RPC latencies with different types of |
| 15 |
// random distributions (uniform, normal, and exponential) as Prometheus |
| 16 |
// metrics. |
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"flag" |
| 21 |
"log" |
| 22 |
"math" |
| 23 |
"math/rand" |
| 24 |
"net/http" |
| 25 |
"strconv" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/prometheus/client_golang/prometheus" |
| 29 |
"github.com/prometheus/client_golang/prometheus/collectors" |
| 30 |
"github.com/prometheus/client_golang/prometheus/promhttp" |
| 31 |
) |
| 32 |
|
| 33 |
type metrics struct { |
| 34 |
rpcDurations *prometheus.SummaryVec |
| 35 |
rpcDurationsHistogram prometheus.Histogram |
| 36 |
} |
| 37 |
|
| 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 |
| 41 |
// distinct services with different latency distributions. These services are |
| 42 |
// differentiated via a "service" label. |
| 43 |
rpcDurations: prometheus.NewSummaryVec( |
| 44 |
prometheus.SummaryOpts{ |
| 45 |
Name: "rpc_durations_seconds", |
| 46 |
Help: "RPC latency distributions.", |
| 47 |
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, |
| 48 |
}, |
| 49 |
[]string{"service"}, |
| 50 |
), |
| 51 |
// The same as above, but now as a histogram, and only for the |
| 52 |
// normal distribution. The histogram features both conventional |
| 53 |
// buckets as well as sparse buckets, the latter needed for the |
| 54 |
// experimental native histograms (ingested by a Prometheus |
| 55 |
// server v2.40 with the corresponding feature flag |
| 56 |
// enabled). The conventional buckets are targeted to the |
| 57 |
// parameters of the normal distribution, with 20 buckets |
| 58 |
// centered on the mean, each half-sigma wide. The sparse |
| 59 |
// buckets are always centered on zero, with a growth factor of |
| 60 |
// one bucket to the next of (at most) 1.1. (The precise factor |
| 61 |
// is 2^2^-3 = 1.0905077...) |
| 62 |
rpcDurationsHistogram: prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 63 |
Name: "rpc_durations_histogram_seconds", |
| 64 |
Help: "RPC latency distributions.", |
| 65 |
Buckets: prometheus.LinearBuckets(normMean-5*normDomain, .5*normDomain, 20), |
| 66 |
NativeHistogramBucketFactor: 1.1, |
| 67 |
}), |
| 68 |
} |
| 69 |
reg.MustRegister(m.rpcDurations) |
| 70 |
reg.MustRegister(m.rpcDurationsHistogram) |
| 71 |
return m |
| 72 |
} |
| 73 |
|
| 74 |
func 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 |
}() |
| 132 |
|
| 133 |
// Expose the registered metrics via HTTP. |
| 134 |
http.Handle("/metrics", promhttp.HandlerFor( |
| 135 |
reg, |
| 136 |
promhttp.HandlerOpts{ |
| 137 |
// Opt into OpenMetrics to support exemplars. |
| 138 |
EnableOpenMetrics: true, |
| 139 |
// Pass custom registry |
| 140 |
Registry: reg, |
| 141 |
}, |
| 142 |
)) |
| 143 |
log.Fatal(http.ListenAndServe(*addr, nil)) |
| 144 |
} |