func main() {
	flag.Parse()

	rpc_latency := metrics.NewHistogram(&metrics.HistogramSpecification{
		Starts:                metrics.EquallySizedBucketsFor(0, 200, 4),
		BucketBuilder:         metrics.AccumulatingBucketBuilder(metrics.EvictAndReplaceWith(10, maths.Average), 50),
		ReportablePercentiles: []float64{0.01, 0.05, 0.5, 0.90, 0.99},
	})

	rpc_calls := metrics.NewCounter()

	metrics := registry.NewRegistry()

	metrics.Register("rpc_latency_microseconds", "RPC latency.", registry.NilLabels, rpc_latency)
	metrics.Register("rpc_calls_total", "RPC calls.", registry.NilLabels, rpc_calls)

	go func() {
		for {
			rpc_latency.Add(map[string]string{"service": "foo"}, rand.Float64()*200)
			rpc_calls.Increment(map[string]string{"service": "foo"})

			rpc_latency.Add(map[string]string{"service": "bar"}, (rand.NormFloat64()*10.0)+100.0)
			rpc_calls.Increment(map[string]string{"service": "bar"})

			rpc_latency.Add(map[string]string{"service": "zed"}, rand.ExpFloat64())
			rpc_calls.Increment(map[string]string{"service": "zed"})

			time.Sleep(100 * time.Millisecond)
		}
	}()

	exporter := metrics.YieldExporter()

	http.Handle("/metrics.json", exporter)
	http.ListenAndServe(listeningAddress, nil)
}
)

/*
Boilerplate metrics about the metrics reporting subservice.  These are only
exposed if the DefaultRegistry's exporter is hooked into the HTTP request
handler.
*/
var (
	marshalErrorCount = metrics.NewCounter()
	dumpErrorCount    = metrics.NewCounter()

	requestCount          = metrics.NewCounter()
	requestLatencyBuckets = metrics.LogarithmicSizedBucketsFor(0, 1000)
	requestLatency        = metrics.NewHistogram(&metrics.HistogramSpecification{
		Starts:                requestLatencyBuckets,
		BucketBuilder:         metrics.AccumulatingBucketBuilder(metrics.EvictAndReplaceWith(50, maths.Average), 1000),
		ReportablePercentiles: []float64{0.01, 0.05, 0.5, 0.9, 0.99},
	})

	startTime = metrics.NewGauge()
)

func init() {
	startTime.Set(nil, float64(time.Now().Unix()))

	DefaultRegistry.Register("telemetry_requests_metrics_total", "A counter of the total requests made against the telemetry system.", NilLabels, requestCount)
	DefaultRegistry.Register("telemetry_requests_metrics_latency_microseconds", "A histogram of the response latency for requests made against the telemetry system.", NilLabels, requestLatency)

	DefaultRegistry.Register("instance_start_time_seconds", "The time at which the current instance started (UTC).", NilLabels, startTime)
}