-
Notifications
You must be signed in to change notification settings - Fork 460
/
fakemetrics.go
127 lines (109 loc) · 2.99 KB
/
fakemetrics.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package fakemetrics
import (
"sync"
"time"
"github.com/spiffe/spire/pkg/common/telemetry"
)
type MetricType int
const (
SetGaugeType MetricType = iota
SetGaugeWithLabelsType
EmitKeyType
IncrCounterType
IncrCounterWithLabelsType
AddSampleType
AddSampleWithLabelsType
MeasureSinceType
MeasureSinceWithLabelsType
)
type FakeMetrics struct {
metrics []MetricItem
mu sync.Mutex
}
type MetricItem struct {
Type MetricType
Key []string
Val float32
Labels []telemetry.Label
Start time.Time
}
func New() *FakeMetrics {
return &FakeMetrics{}
}
func (m *FakeMetrics) Reset() {
m.mu.Lock()
defer m.mu.Unlock()
m.metrics = nil
}
// AllMetrics return all collected metrics
func (m *FakeMetrics) AllMetrics() []MetricItem {
m.mu.Lock()
defer m.mu.Unlock()
return m.metrics
}
func (m *FakeMetrics) SetGauge(key []string, val float32) {
m.mu.Lock()
defer m.mu.Unlock()
m.metrics = append(m.metrics, MetricItem{Type: SetGaugeType, Key: key, Val: val})
}
func (m *FakeMetrics) SetGaugeWithLabels(key []string, val float32, labels []telemetry.Label) {
m.mu.Lock()
defer m.mu.Unlock()
m.metrics = append(m.metrics, MetricItem{
Type: SetGaugeWithLabelsType,
Key: key,
Val: val,
Labels: telemetry.SanitizeLabels(labels),
})
}
func (m *FakeMetrics) EmitKey(key []string, val float32) {
m.mu.Lock()
defer m.mu.Unlock()
m.metrics = append(m.metrics, MetricItem{Type: EmitKeyType, Key: key, Val: val})
}
func (m *FakeMetrics) IncrCounter(key []string, val float32) {
m.mu.Lock()
defer m.mu.Unlock()
m.metrics = append(m.metrics, MetricItem{Type: IncrCounterType, Key: key, Val: val})
}
func (m *FakeMetrics) IncrCounterWithLabels(key []string, val float32, labels []telemetry.Label) {
m.mu.Lock()
defer m.mu.Unlock()
m.metrics = append(m.metrics, MetricItem{
Type: IncrCounterWithLabelsType,
Key: key,
Val: val,
Labels: telemetry.SanitizeLabels(labels),
})
}
func (m *FakeMetrics) AddSample(key []string, val float32) {
m.mu.Lock()
defer m.mu.Unlock()
m.metrics = append(m.metrics, MetricItem{Type: AddSampleType, Key: key, Val: val})
}
func (m *FakeMetrics) AddSampleWithLabels(key []string, val float32, labels []telemetry.Label) {
m.mu.Lock()
defer m.mu.Unlock()
m.metrics = append(m.metrics, MetricItem{
Type: AddSampleWithLabelsType,
Key: key,
Val: val,
Labels: telemetry.SanitizeLabels(labels),
})
}
func (m *FakeMetrics) MeasureSince(key []string, _ time.Time) {
m.mu.Lock()
defer m.mu.Unlock()
// TODO: record `start` when it is convenient to thread a clock through all the telemetry helpers
m.metrics = append(m.metrics, MetricItem{Type: MeasureSinceType, Key: key})
}
func (m *FakeMetrics) MeasureSinceWithLabels(key []string, _ time.Time, labels []telemetry.Label) {
m.mu.Lock()
defer m.mu.Unlock()
// TODO: record `start` when it is convenient to thread a clock through all the telemetry helpers
m.metrics = append(m.metrics, MetricItem{
Type: MeasureSinceWithLabelsType,
Key: key,
Labels: telemetry.SanitizeLabels(labels),
})
}