Add examples for Histogram
This commit is contained in:
parent
007e78a6c7
commit
4d9d3a095f
27
histogram_example_test.go
Normal file
27
histogram_example_test.go
Normal file
@ -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)))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user