diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 12:08:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 12:08:18 +0000 |
commit | 5da14042f70711ea5cf66e034699730335462f66 (patch) | |
tree | 0f6354ccac934ed87a2d555f45be4c831cf92f4a /src/go/collectors/go.d.plugin/pkg/metrics/counter.go | |
parent | Releasing debian version 1.44.3-2. (diff) | |
download | netdata-5da14042f70711ea5cf66e034699730335462f66.tar.xz netdata-5da14042f70711ea5cf66e034699730335462f66.zip |
Merging upstream version 1.45.3+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/go/collectors/go.d.plugin/pkg/metrics/counter.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/pkg/metrics/counter.go b/src/go/collectors/go.d.plugin/pkg/metrics/counter.go new file mode 100644 index 000000000..7231fc7a4 --- /dev/null +++ b/src/go/collectors/go.d.plugin/pkg/metrics/counter.go @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package metrics + +import ( + "errors" + + "github.com/netdata/netdata/go/go.d.plugin/pkg/stm" +) + +type ( + // Counter is a Metric that represents a single numerical bits that only ever + // goes up. That implies that it cannot be used to count items whose number can + // also go down, e.g. the number of currently running goroutines. Those + // "counters" are represented by Gauges. + // + // A Counter is typically used to count requests served, tasks completed, errors + // occurred, etc. + Counter struct { + valInt int64 + valFloat float64 + } + + // CounterVec is a Collector that bundles a set of Counters which have different values for their names. + // This is used if you want to count the same thing partitioned by various dimensions + // (e.g. number of HTTP requests, partitioned by response code and method). + // + // Create instances with NewCounterVec. + CounterVec map[string]*Counter +) + +var ( + _ stm.Value = Counter{} + _ stm.Value = CounterVec{} +) + +// WriteTo writes its value into given map. +func (c Counter) WriteTo(rv map[string]int64, key string, mul, div int) { + rv[key] = int64(c.Value() * float64(mul) / float64(div)) +} + +// Value gets current counter. +func (c Counter) Value() float64 { + return float64(c.valInt) + c.valFloat +} + +// Inc increments the counter by 1. Use Add to increment it by arbitrary +// non-negative values. +func (c *Counter) Inc() { + c.valInt++ +} + +// Add adds the given bits to the counter. It panics if the value is < 0. +func (c *Counter) Add(v float64) { + if v < 0 { + panic(errors.New("counter cannot decrease in value")) + } + val := int64(v) + if float64(val) == v { + c.valInt += val + return + } + c.valFloat += v +} + +// NewCounterVec creates a new CounterVec +func NewCounterVec() CounterVec { + return CounterVec{} +} + +// WriteTo writes its value into given map. +func (c CounterVec) WriteTo(rv map[string]int64, key string, mul, div int) { + for name, value := range c { + rv[key+"_"+name] = int64(value.Value() * float64(mul) / float64(div)) + } +} + +// Get gets counter instance by name +func (c CounterVec) Get(name string) *Counter { + item, _ := c.GetP(name) + return item +} + +// GetP gets counter instance by name +func (c CounterVec) GetP(name string) (counter *Counter, ok bool) { + counter, ok = c[name] + if ok { + return + } + counter = &Counter{} + c[name] = counter + return +} |