From fe4f71b333d9626fbbf62d0ad8ba6bcd62c11bec Mon Sep 17 00:00:00 2001 From: "Matt T. Proud" Date: Mon, 21 May 2012 11:05:41 +0200 Subject: [PATCH] - Provide an example of this working with a uniform distribution. --- README.md | 8 +++--- examples/uniform_random/main.go | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 examples/uniform_random/main.go diff --git a/README.md b/README.md index 5e546aa..9c8fed3 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,10 @@ re-implementation should other folks need to consume and analyze such telemetry. N.B. --- I have spent a bit of time working through the model in my head and probably haven't elucidated my ideas as clearly as I need to. If you examine -src/main.go and src/export/registry.go, you'll find an example of what type of -potential instrumentation use cases this package addresses. There are probably -numerous Go language idiomatic changes that need to be made, but this task has -been deferred for now. +examples/{simple,uniform_random}/main.go and registry.go, you'll find several +examples of what types of potential instrumentation use cases this package +addresses. There are probably numerous Go language idiomatic changes that need +to be made, but this task has been deferred for now. # Continuous Integration [![Build Status](https://secure.travis-ci.org/matttproud/golang_instrumentation.png?branch=master)](http://travis-ci.org/matttproud/golang_instrumentation) diff --git a/examples/uniform_random/main.go b/examples/uniform_random/main.go new file mode 100644 index 0000000..5849791 --- /dev/null +++ b/examples/uniform_random/main.go @@ -0,0 +1,46 @@ +// Copyright (c) 2012, Matt T. Proud +// All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// 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. + +package main + +import ( + "github.com/matttproud/golang_instrumentation" + "github.com/matttproud/golang_instrumentation/maths" + "github.com/matttproud/golang_instrumentation/metrics" + "math/rand" + "net/http" + "time" +) + +func main() { + foo_rpc_latency := metrics.CreateHistogram(&metrics.HistogramSpecification{ + Starts: metrics.EquallySizedBucketsFor(0, 200, 4), + BucketMaker: metrics.AccumulatingBucketBuilder(metrics.EvictAndReplaceWith(10, maths.Average), 50), + ReportablePercentiles: []float64{0.01, 0.5, 0.90, 0.99}, + }) + foo_rpc_calls := &metrics.GaugeMetric{} + + metrics := registry.NewRegistry() + metrics.Register("foo_rpc_latency_ms_histogram", foo_rpc_latency) + metrics.Register("foo_rpc_call_count", foo_rpc_calls) + + go func() { + for { + foo_rpc_latency.Add(rand.Float64() * 200) + foo_rpc_calls.Increment() + time.Sleep(500 * time.Millisecond) + } + }() + + exporter := metrics.YieldExporter() + + http.Handle("/metrics.json", exporter) + http.ListenAndServe(":8080", nil) +}