-
Notifications
You must be signed in to change notification settings - Fork 4
/
counter.go
84 lines (69 loc) · 1.85 KB
/
counter.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package metric
import (
"fmt"
"sync/atomic"
"github.com/prometheus/client_golang/prometheus"
)
var _ Metric = &counter{}
// Counter stores a numerical value that only ever goes up.
type Counter interface {
Metric
}
// CounterOpts is an alias of Opts.
type CounterOpts Opts
type counter struct {
val int64
}
// NewCounter creates a new Counter based on the CounterOpts.
func NewCounter(opts CounterOpts) Counter {
return &counter{}
}
func (c *counter) Add(val int64) {
if val < 0 {
panic(fmt.Errorf("stat/metric: cannot decrease in negative value. val: %d", val))
}
atomic.AddInt64(&c.val, val)
}
func (c *counter) Value() int64 {
return atomic.LoadInt64(&c.val)
}
// CounterVecOpts is an alias of VectorOpts.
type CounterVecOpts VectorOpts
// CounterVec counter vec.
type CounterVec interface {
// Inc increments the counter by 1. Use Add to increment it by arbitrary
// non-negative values.
Inc(labels ...string)
// Add adds the given value to the counter. It panics if the value is <
// 0.
Add(v float64, labels ...string)
}
// counterVec counter vec.
type promCounterVec struct {
counter *prometheus.CounterVec
}
// NewCounterVec .
func NewCounterVec(cfg *CounterVecOpts) CounterVec {
if cfg == nil {
return nil
}
vec := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: cfg.Namespace,
Subsystem: cfg.Subsystem,
Name: cfg.Name,
Help: cfg.Help,
}, cfg.Labels)
prometheus.MustRegister(vec)
return &promCounterVec{
counter: vec,
}
}
// Inc Inc increments the counter by 1. Use Add to increment it by arbitrary.
func (counter *promCounterVec) Inc(labels ...string) {
counter.counter.WithLabelValues(labels...).Inc()
}
// Add Inc increments the counter by 1. Use Add to increment it by arbitrary.
func (counter *promCounterVec) Add(v float64, labels ...string) {
counter.counter.WithLabelValues(labels...).Add(v)
}