File: histogram_example_test.go

package info (click to toggle)
golang-github-victoriametrics-metrics 1.35.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 308 kB
  • sloc: makefile: 2
file content (27 lines) | stat: -rw-r--r-- 674 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)))
	}
}