~ubuntu-branches/debian/sid/golang-github-prometheus-client-golang/sid

« back to all changes in this revision

Viewing changes to examples/random/main.go

  • Committer: Package Import Robot
  • Author(s): Martín Ferrari
  • Date: 2016-08-18 12:06:03 UTC
  • Revision ID: package-import@ubuntu.com-20160818120603-xevgulhsaf9vktsr
Tags: upstream-0.8.0
Import upstream version 0.8.0

Show diffs side-by-side

added added

removed removed

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