diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-03-24 11:43:12 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-03-27 09:24:13 +0300 |
commit | d2ecbcc4548140d5d8b89d3d24d80b98abac4d02 (patch) | |
tree | 680dd8634215b994242da13c076b49b08ebd8e1a | |
parent | 59d0b87438608e2d41399421b74563ab93b9db1f (diff) |
prometheus: Add Counter interface
Add a Counter interface that is a subset of the Prometheus Counter type
as well as a MockCounter that can be used by tests.
-rw-r--r-- | internal/prometheus/metrics/metrics.go | 6 | ||||
-rw-r--r-- | internal/testhelper/promtest/counter.go | 26 |
2 files changed, 32 insertions, 0 deletions
diff --git a/internal/prometheus/metrics/metrics.go b/internal/prometheus/metrics/metrics.go index 9ca576de6..62b72d374 100644 --- a/internal/prometheus/metrics/metrics.go +++ b/internal/prometheus/metrics/metrics.go @@ -4,6 +4,12 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +// Counter is a subset of a prometheus Counter +type Counter interface { + Inc() + Add(float64) +} + // Gauge is a subset of a prometheus Gauge type Gauge interface { Inc() diff --git a/internal/testhelper/promtest/counter.go b/internal/testhelper/promtest/counter.go new file mode 100644 index 000000000..665023436 --- /dev/null +++ b/internal/testhelper/promtest/counter.go @@ -0,0 +1,26 @@ +package promtest + +import ( + "sync" +) + +type MockCounter struct { + m sync.RWMutex + value float64 +} + +func (m *MockCounter) Value() float64 { + m.m.RLock() + defer m.m.RUnlock() + return m.value +} + +func (m *MockCounter) Inc() { + m.Add(1) +} + +func (m *MockCounter) Add(v float64) { + m.m.Lock() + defer m.m.Unlock() + m.value += v +} |