master
go 103 lines 2.48 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package oldmetrix
4
5 import (
6 "time"
7
8 "github.com/netdata/netdata/go/plugins/pkg/stm"
9 )
10
11 type (
12 // Gauge is a Metric that represents a single numerical value that can
13 // arbitrarily go up and down.
14 //
15 // A Gauge is typically used for measured values like temperatures or current
16 // memory usage, but also "counts" that can go up and down, like the number of
17 // running goroutines.
18 Gauge float64
19
20 // GaugeVec is a Collector that bundles a set of Gauges which have different values for their names.
21 // This is used if you want to count the same thing partitioned by various dimensions
22 //
23 // Create instances with NewGaugeVec.
24 GaugeVec map[string]*Gauge
25 )
26
27 var (
28 _ stm.Value = Gauge(0)
29 _ stm.Value = GaugeVec{}
30 )
31
32 // WriteTo writes its value into given map.
33 func (g Gauge) WriteTo(rv map[string]int64, key string, mul, div int) {
34 rv[key] = int64(float64(g) * float64(mul) / float64(div))
35 }
36
37 // Value gets current counter.
38 func (g Gauge) Value() float64 {
39 return float64(g)
40 }
41
42 // Set sets the atomicGauge to an arbitrary bits.
43 func (g *Gauge) Set(v float64) {
44 *g = Gauge(v)
45 }
46
47 // Inc increments the atomicGauge by 1. Use Add to increment it by arbitrary
48 // values.
49 func (g *Gauge) Inc() {
50 *g++
51 }
52
53 // Dec decrements the atomicGauge by 1. Use Sub to decrement it by arbitrary
54 // values.
55 func (g *Gauge) Dec() {
56 *g--
57 }
58
59 // Add adds the given bits to the atomicGauge. (The bits can be negative,
60 // resulting in a decrease of the atomicGauge.)
61 func (g *Gauge) Add(delta float64) {
62 *g += Gauge(delta)
63 }
64
65 // Sub subtracts the given bits from the atomicGauge. (The bits can be
66 // negative, resulting in an increase of the atomicGauge.)
67 func (g *Gauge) Sub(delta float64) {
68 *g -= Gauge(delta)
69 }
70
71 // SetToCurrentTime sets the atomicGauge to the current Unix time in second.
72 func (g *Gauge) SetToCurrentTime() {
73 *g = Gauge(time.Now().UnixNano()) / 1e9
74 }
75
76 // NewGaugeVec creates a new GaugeVec
77 func NewGaugeVec() GaugeVec {
78 return GaugeVec{}
79 }
80
81 // WriteTo writes its value into given map.
82 func (g GaugeVec) WriteTo(rv map[string]int64, key string, mul, div int) {
83 for name, value := range g {
84 rv[key+"_"+name] = int64(value.Value() * float64(mul) / float64(div))
85 }
86 }
87
88 // Get gets counter instance by name
89 func (g GaugeVec) Get(name string) *Gauge {
90 item, _ := g.GetP(name)
91 return item
92 }
93
94 // GetP gets counter instance by name
95 func (g GaugeVec) GetP(name string) (gauge *Gauge, ok bool) {
96 gauge, ok = g[name]
97 if ok {
98 return
99 }
100 gauge = new(Gauge)
101 g[name] = gauge
102 return
103 }