File: floatcounter_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 (41 lines) | stat: -rw-r--r-- 913 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package metrics_test

import (
	"fmt"
	"github.com/VictoriaMetrics/metrics"
)

func ExampleFloatCounter() {
	// Define a float64 counter in global scope.
	var fc = metrics.NewFloatCounter(`float_metric_total{label1="value1", label2="value2"}`)

	// Add to the counter when needed.
	for i := 0; i < 10; i++ {
		fc.Add(1.01)
	}
	n := fc.Get()
	fmt.Println(n)

	// Output:
	// 10.1
}

func ExampleFloatCounter_vec() {
	for i := 0; i < 3; i++ {
		// Dynamically construct metric name and pass it to GetOrCreateFloatCounter.
		name := fmt.Sprintf(`float_metric_total{label1=%q, label2="%d"}`, "value1", i)
		metrics.GetOrCreateFloatCounter(name).Add(float64(i) + 1.01)
	}

	// Read counter values.
	for i := 0; i < 3; i++ {
		name := fmt.Sprintf(`float_metric_total{label1=%q, label2="%d"}`, "value1", i)
		n := metrics.GetOrCreateFloatCounter(name).Get()
		fmt.Println(n)
	}

	// Output:
	// 1.01
	// 2.01
	// 3.01
}