From 4d9d3a095fdee7209b330304c00cdd0f9497cc1c Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 27 Nov 2019 14:04:12 +0200 Subject: [PATCH] Add examples for Histogram --- histogram_example_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 histogram_example_test.go diff --git a/histogram_example_test.go b/histogram_example_test.go new file mode 100644 index 0000000..358050e --- /dev/null +++ b/histogram_example_test.go @@ -0,0 +1,27 @@ +package metrics_test + +import ( + "fmt" + "time" + + "github.com/VictoriaMetrics/metrics" +) + +func ExampleHistogram() { + // Define a histogram in global scope. + var h = metrics.NewHistogram(`request_duration_seconds{path="/foo/bar"}`) + + // Update the histogram with the duration of processRequest call. + startTime := time.Now() + processRequest() + h.UpdateDuration(startTime) +} + +func ExampleHistogram_vec() { + for i := 0; i < 3; i++ { + // Dynamically construct metric name and pass it to GetOrCreateHistogram. + name := fmt.Sprintf(`response_size_bytes{path=%q}`, "/foo/bar") + response := processRequest() + metrics.GetOrCreateHistogram(name).Update(float64(len(response))) + } +}