2012-05-24 22:02:44 +04:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012, Matt T. Proud
|
|
|
|
All rights reserved.
|
2012-05-22 11:20:09 +04:00
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file.
|
|
|
|
*/
|
2012-05-22 11:20:09 +04:00
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
/*
|
|
|
|
main.go provides a simple example of how to use this instrumentation
|
|
|
|
framework in the context of having something that emits values into
|
|
|
|
its collectors.
|
|
|
|
|
|
|
|
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"
|
2012-05-22 11:20:09 +04:00
|
|
|
"github.com/matttproud/golang_instrumentation"
|
|
|
|
"github.com/matttproud/golang_instrumentation/maths"
|
|
|
|
"github.com/matttproud/golang_instrumentation/metrics"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-01-12 01:52:36 +04:00
|
|
|
var (
|
|
|
|
listeningAddress string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.StringVar(&listeningAddress, "listeningAddress", ":8080", "The address to listen to requests on.")
|
|
|
|
}
|
|
|
|
|
2012-05-22 11:20:09 +04:00
|
|
|
func main() {
|
2013-01-12 01:52:36 +04:00
|
|
|
flag.Parse()
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
rpc_latency := metrics.NewHistogram(&metrics.HistogramSpecification{
|
2012-05-22 11:20:09 +04:00
|
|
|
Starts: metrics.EquallySizedBucketsFor(0, 200, 4),
|
2013-01-19 17:48:30 +04:00
|
|
|
BucketBuilder: metrics.AccumulatingBucketBuilder(metrics.EvictAndReplaceWith(10, maths.Average), 50),
|
2012-05-22 11:20:09 +04:00
|
|
|
ReportablePercentiles: []float64{0.01, 0.05, 0.5, 0.90, 0.99},
|
|
|
|
})
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
rpc_calls := metrics.NewCounter()
|
2013-01-15 23:18:43 +04:00
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
metrics := registry.NewRegistry()
|
2013-01-15 23:18:43 +04:00
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
metrics.Register("rpc_latency_microseconds", "RPC latency.", registry.NilLabels, rpc_latency)
|
|
|
|
metrics.Register("rpc_calls_total", "RPC calls.", registry.NilLabels, rpc_calls)
|
2012-05-22 11:20:09 +04:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2013-01-19 17:48:30 +04:00
|
|
|
rpc_latency.Add(map[string]string{"service": "foo"}, rand.Float64()*200)
|
|
|
|
rpc_calls.Increment(map[string]string{"service": "foo"})
|
2012-05-22 11:20:09 +04:00
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
rpc_latency.Add(map[string]string{"service": "bar"}, (rand.NormFloat64()*10.0)+100.0)
|
|
|
|
rpc_calls.Increment(map[string]string{"service": "bar"})
|
2012-05-22 11:20:09 +04:00
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
rpc_latency.Add(map[string]string{"service": "zed"}, rand.ExpFloat64())
|
|
|
|
rpc_calls.Increment(map[string]string{"service": "zed"})
|
2012-05-22 11:20:09 +04:00
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
exporter := metrics.YieldExporter()
|
|
|
|
|
|
|
|
http.Handle("/metrics.json", exporter)
|
2013-01-12 01:52:36 +04:00
|
|
|
http.ListenAndServe(listeningAddress, nil)
|
2012-05-22 11:20:09 +04:00
|
|
|
}
|