forked from aquasecurity/tfsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.go
54 lines (45 loc) · 1.13 KB
/
count.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
package metrics
import (
"fmt"
"sync"
)
type CounterMetric interface {
Metric
Increment(delta int)
}
type counter struct {
sync.Mutex
name string
count uint64
}
// Counter creates a new counter metric (or returns an existing one if one already exists with this name and category)
func Counter(category, name string) CounterMetric {
return newCounter(category, name, false)
}
// DebugCounter creates a new debug counter metric (or returns an existing one if one already exists with this name and category)
func DebugCounter(category, name string) CounterMetric {
return newCounter(category, name, true)
}
func newCounter(category string, name string, debug bool) CounterMetric {
if metric := useCategory(category, debug).findMetric(name); metric != nil {
if c, ok := metric.(CounterMetric); ok {
return c
}
}
count := &counter{
name: name,
}
useCategory(category, debug).setMetric(count)
return count
}
func (c *counter) Name() string {
return c.name
}
func (c *counter) Value() string {
return fmt.Sprintf("%d", c.count)
}
func (c *counter) Increment(delta int) {
c.Lock()
defer c.Unlock()
c.count += uint64(delta)
}