main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // A simple example exposing fictional RPC latencies with different types of
  14. // random distributions (uniform, normal, and exponential) as Prometheus
  15. // metrics.
  16. package main
  17. import (
  18. "flag"
  19. "log"
  20. "math"
  21. "math/rand"
  22. "net/http"
  23. "time"
  24. "github.com/prometheus/client_golang/prometheus"
  25. "github.com/prometheus/client_golang/prometheus/promhttp"
  26. )
  27. var (
  28. addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
  29. uniformDomain = flag.Float64("uniform.domain", 0.0002, "The domain for the uniform distribution.")
  30. normDomain = flag.Float64("normal.domain", 0.0002, "The domain for the normal distribution.")
  31. normMean = flag.Float64("normal.mean", 0.00001, "The mean for the normal distribution.")
  32. oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
  33. )
  34. var (
  35. // Create a summary to track fictional interservice RPC latencies for three
  36. // distinct services with different latency distributions. These services are
  37. // differentiated via a "service" label.
  38. rpcDurations = prometheus.NewSummaryVec(
  39. prometheus.SummaryOpts{
  40. Name: "rpc_durations_seconds",
  41. Help: "RPC latency distributions.",
  42. Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
  43. },
  44. []string{"service"},
  45. )
  46. // The same as above, but now as a histogram, and only for the normal
  47. // distribution. The buckets are targeted to the parameters of the
  48. // normal distribution, with 20 buckets centered on the mean, each
  49. // half-sigma wide.
  50. rpcDurationsHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
  51. Name: "rpc_durations_histogram_seconds",
  52. Help: "RPC latency distributions.",
  53. Buckets: prometheus.LinearBuckets(*normMean-5**normDomain, .5**normDomain, 20),
  54. })
  55. )
  56. func init() {
  57. // Register the summary and the histogram with Prometheus's default registry.
  58. prometheus.MustRegister(rpcDurations)
  59. prometheus.MustRegister(rpcDurationsHistogram)
  60. }
  61. func main() {
  62. flag.Parse()
  63. start := time.Now()
  64. oscillationFactor := func() float64 {
  65. return 2 + math.Sin(math.Sin(2*math.Pi*float64(time.Since(start))/float64(*oscillationPeriod)))
  66. }
  67. // Periodically record some sample latencies for the three services.
  68. go func() {
  69. for {
  70. v := rand.Float64() * *uniformDomain
  71. rpcDurations.WithLabelValues("uniform").Observe(v)
  72. time.Sleep(time.Duration(100*oscillationFactor()) * time.Millisecond)
  73. }
  74. }()
  75. go func() {
  76. for {
  77. v := (rand.NormFloat64() * *normDomain) + *normMean
  78. rpcDurations.WithLabelValues("normal").Observe(v)
  79. rpcDurationsHistogram.Observe(v)
  80. time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)
  81. }
  82. }()
  83. go func() {
  84. for {
  85. v := rand.ExpFloat64() / 1e6
  86. rpcDurations.WithLabelValues("exponential").Observe(v)
  87. time.Sleep(time.Duration(50*oscillationFactor()) * time.Millisecond)
  88. }
  89. }()
  90. // Expose the registered metrics via HTTP.
  91. http.Handle("/metrics", promhttp.Handler())
  92. log.Fatal(http.ListenAndServe(*addr, nil))
  93. }