Adjust examples to modern usage

- Use seconds instead of microseconds.
- Use promhttp.Handler instead of prometheus.Handler
This commit is contained in:
beorn7 2016-11-12 16:36:39 +01:00
parent 198ef620eb
commit d703c70ef1
2 changed files with 10 additions and 9 deletions

View File

@ -25,13 +25,14 @@ import (
"time" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
) )
var ( var (
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
uniformDomain = flag.Float64("uniform.domain", 200, "The domain for the uniform distribution.") uniformDomain = flag.Float64("uniform.domain", 0.0002, "The domain for the uniform distribution.")
normDomain = flag.Float64("normal.domain", 200, "The domain for the normal distribution.") normDomain = flag.Float64("normal.domain", 0.0002, "The domain for the normal distribution.")
normMean = flag.Float64("normal.mean", 10, "The mean for the normal distribution.") normMean = flag.Float64("normal.mean", 0.00001, "The mean for the normal distribution.")
oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.") oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
) )
@ -41,7 +42,7 @@ var (
// differentiated via a "service" label. // differentiated via a "service" label.
rpcDurations = prometheus.NewSummaryVec( rpcDurations = prometheus.NewSummaryVec(
prometheus.SummaryOpts{ prometheus.SummaryOpts{
Name: "rpc_durations_microseconds", Name: "rpc_durations_seconds",
Help: "RPC latency distributions.", Help: "RPC latency distributions.",
}, },
[]string{"service"}, []string{"service"},
@ -51,7 +52,7 @@ var (
// normal distribution, with 20 buckets centered on the mean, each // normal distribution, with 20 buckets centered on the mean, each
// half-sigma wide. // half-sigma wide.
rpcDurationsHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{ rpcDurationsHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "rpc_durations_histogram_microseconds", Name: "rpc_durations_histogram_seconds",
Help: "RPC latency distributions.", Help: "RPC latency distributions.",
Buckets: prometheus.LinearBuckets(*normMean-5**normDomain, .5**normDomain, 20), Buckets: prometheus.LinearBuckets(*normMean-5**normDomain, .5**normDomain, 20),
}) })
@ -92,13 +93,13 @@ func main() {
go func() { go func() {
for { for {
v := rand.ExpFloat64() v := rand.ExpFloat64() / 1e6
rpcDurations.WithLabelValues("exponential").Observe(v) rpcDurations.WithLabelValues("exponential").Observe(v)
time.Sleep(time.Duration(50*oscillationFactor()) * time.Millisecond) time.Sleep(time.Duration(50*oscillationFactor()) * time.Millisecond)
} }
}() }()
// Expose the registered metrics via HTTP. // Expose the registered metrics via HTTP.
http.Handle("/metrics", prometheus.Handler()) http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil)) log.Fatal(http.ListenAndServe(*addr, nil))
} }

View File

@ -19,13 +19,13 @@ import (
"log" "log"
"net/http" "net/http"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
func main() { func main() {
flag.Parse() flag.Parse()
http.Handle("/metrics", prometheus.Handler()) http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil)) log.Fatal(http.ListenAndServe(*addr, nil))
} }