2013-04-03 20:33:32 +04:00
|
|
|
// Copyright (c) 2013, Prometheus Team
|
2013-02-12 05:36:06 +04:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-02-18 08:09:21 +04:00
|
|
|
// A simple example of how to use this instrumentation framework in the context
|
|
|
|
// of having something that emits values into its collectors.
|
2013-02-12 05:36:06 +04:00
|
|
|
//
|
|
|
|
// The emitted values correspond to uniform, normal, and exponential
|
|
|
|
// distributions.
|
2012-05-22 11:20:09 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-01-12 01:52:36 +04:00
|
|
|
"flag"
|
2013-04-03 20:33:32 +04:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2012-05-22 11:20:09 +04:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-01-12 01:52:36 +04:00
|
|
|
var (
|
2013-03-29 00:03:01 +04:00
|
|
|
barDomain = flag.Float64("random.fooDomain", 200, "The domain for the random parameter foo.")
|
|
|
|
barMean = flag.Float64("random.barDomain", 10, "The domain for the random parameter bar.")
|
|
|
|
fooDomain = flag.Float64("random.barMean", 100, "The mean for the random parameter bar.")
|
2013-02-12 05:36:06 +04:00
|
|
|
|
|
|
|
// Create a histogram to track fictitious interservice RPC latency for three
|
|
|
|
// distinct services.
|
2013-04-03 20:33:32 +04:00
|
|
|
rpcLatency = prometheus.NewHistogram(&prometheus.HistogramSpecification{
|
2013-02-12 05:36:06 +04:00
|
|
|
// Four distinct histogram buckets for values:
|
|
|
|
// - equally-sized,
|
|
|
|
// - 0 to 50, 50 to 100, 100 to 150, and 150 to 200.
|
2013-04-03 20:33:32 +04:00
|
|
|
Starts: prometheus.EquallySizedBucketsFor(0, 200, 4),
|
2013-02-12 05:36:06 +04:00
|
|
|
// Create histogram buckets using an accumulating bucket, a bucket that
|
|
|
|
// holds sample values subject to an eviction policy:
|
|
|
|
// - 50 elements are allowed per bucket.
|
|
|
|
// - Once 50 have been reached, the bucket empties 10 elements, averages the
|
|
|
|
// evicted elements, and re-appends that back to the bucket.
|
2013-04-03 20:33:32 +04:00
|
|
|
BucketBuilder: prometheus.AccumulatingBucketBuilder(prometheus.EvictAndReplaceWith(10, prometheus.AverageReducer), 50),
|
2013-02-12 05:36:06 +04:00
|
|
|
// The histogram reports percentiles 1, 5, 50, 90, and 99.
|
|
|
|
ReportablePercentiles: []float64{0.01, 0.05, 0.5, 0.90, 0.99},
|
|
|
|
})
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
rpcCalls = prometheus.NewCounter()
|
2013-02-12 05:36:06 +04:00
|
|
|
|
|
|
|
// If for whatever reason you are resistant to the idea of having a static
|
|
|
|
// registry for metrics, which is a really bad idea when using Prometheus-
|
|
|
|
// enabled library code, you can create your own.
|
2013-04-03 20:33:32 +04:00
|
|
|
customRegistry = prometheus.NewRegistry()
|
2013-01-12 01:52:36 +04:00
|
|
|
)
|
|
|
|
|
2012-05-22 11:20:09 +04:00
|
|
|
func main() {
|
2013-01-12 01:52:36 +04:00
|
|
|
flag.Parse()
|
|
|
|
|
2012-05-22 11:20:09 +04:00
|
|
|
go func() {
|
|
|
|
for {
|
2013-03-29 00:03:01 +04:00
|
|
|
rpcLatency.Add(map[string]string{"service": "foo"}, rand.Float64()**fooDomain)
|
|
|
|
rpcCalls.Increment(map[string]string{"service": "foo"})
|
2012-05-22 11:20:09 +04:00
|
|
|
|
2013-03-29 00:03:01 +04:00
|
|
|
rpcLatency.Add(map[string]string{"service": "bar"}, (rand.NormFloat64()**barDomain)+*barMean)
|
|
|
|
rpcCalls.Increment(map[string]string{"service": "bar"})
|
2012-05-22 11:20:09 +04:00
|
|
|
|
2013-03-29 00:03:01 +04:00
|
|
|
rpcLatency.Add(map[string]string{"service": "zed"}, rand.ExpFloat64())
|
|
|
|
rpcCalls.Increment(map[string]string{"service": "zed"})
|
2012-05-22 11:20:09 +04:00
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
http.Handle(prometheus.ExpositionResource, customRegistry.Handler())
|
2013-03-29 00:03:01 +04:00
|
|
|
http.ListenAndServe(*listeningAddress, nil)
|
2012-05-22 11:20:09 +04:00
|
|
|
}
|
2013-02-12 05:36:06 +04:00
|
|
|
|
|
|
|
func init() {
|
2013-04-03 20:33:32 +04:00
|
|
|
customRegistry.Register("rpc_latency_microseconds", "RPC latency.", prometheus.NilLabels, rpcLatency)
|
|
|
|
customRegistry.Register("rpc_calls_total", "RPC calls.", prometheus.NilLabels, rpcCalls)
|
2013-02-12 05:36:06 +04:00
|
|
|
}
|
2013-03-29 00:03:01 +04:00
|
|
|
|
|
|
|
var (
|
|
|
|
listeningAddress = flag.String("listeningAddress", ":8080", "The address to listen to requests on.")
|
|
|
|
)
|