main.go 3.4 KB

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