-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_counter.go
105 lines (85 loc) · 2.15 KB
/
metric_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package mtsdb
import (
"context"
"errors"
"sync"
"sync/atomic"
)
var _ Counter = &metricCounter{}
type MetricCounterConfig struct {
TableName string
Description string
}
type Counter interface {
MetricInterface
Inc(labelValues ...string) error
Add(value uint32, labels ...string) error
Get(labelValues ...string) (uint32, bool)
}
type metricCounter struct {
ctx context.Context
container *metricContainer
name string
labels []string
config MetricCounterConfig
}
func NewMetricCounter(ctx context.Context, name string, metricCounterConfig MetricCounterConfig, labels ...string) (Counter, error) {
mc := metricContainer{}
mc.Store(&sync.Map{})
m := metricCounter{
ctx: ctx,
name: name,
labels: labels,
config: metricCounterConfig,
container: &mc,
}
return &m, nil
}
func (m *metricCounter) Inc(labelValues ...string) error {
return m.Add(1, labelValues...)
}
func (m *metricCounter) Add(count uint32, labelValues ...string) error {
if len(labelValues) == 0 || len(labelValues) != len(m.labels) {
return errors.New("labels len do not match with initialised number of labels")
}
if m.ctx.Err() != nil { // no more inserts
return errors.New("context Done, no more inserts")
}
metricLabelValues := MetricLabelValues{
fields: labelValues,
count: atomic.Uint32{},
}
hashResult, err := hashLabels(labelValues)
if err != nil {
return err
}
value, _ := m.container.Load().LoadOrStore(hashResult, &metricLabelValues)
value.(*MetricLabelValues).count.Add(count)
return nil
}
func (m *metricCounter) Desc() string {
return m.config.Description
}
func (m *metricCounter) Write() *insertMetric {
oldContainer := m.reset()
return &insertMetric{
TableName: m.config.TableName,
Container: oldContainer,
Labels: m.labels,
}
}
func (m *metricCounter) Get(labelValues ...string) (uint32, bool) {
hash, err := hashLabels(labelValues)
if err != nil {
return 0, false
}
value, ok := m.container.Load().Load(hash)
count := uint32(0)
if ok {
count = value.(*MetricLabelValues).count.Load()
}
return count, ok
}
func (m *metricCounter) reset() *sync.Map {
return m.container.Swap(&sync.Map{})
}