/
metricsmock.go
158 lines (130 loc) · 3.35 KB
/
metricsmock.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package metricstest
import (
"net/http"
"sync"
"time"
)
type MockMetrics struct {
Prefix string
mu sync.Mutex
// Metrics gathering
counters map[string]int64
floatCounters map[string]float64
measures map[string][]time.Duration
}
//
// Public thread safe access to metrics
//
func (m *MockMetrics) WithCounters(f func(counters map[string]int64)) {
m.mu.Lock()
defer m.mu.Unlock()
if m.counters == nil {
m.counters = make(map[string]int64)
}
f(m.counters)
}
func (m *MockMetrics) WithFloatCounters(f func(floatCounters map[string]float64)) {
m.mu.Lock()
defer m.mu.Unlock()
if m.floatCounters == nil {
m.floatCounters = make(map[string]float64)
}
f(m.floatCounters)
}
func (m *MockMetrics) WithMeasures(f func(measures map[string][]time.Duration)) {
m.mu.Lock()
defer m.mu.Unlock()
if m.measures == nil {
m.measures = make(map[string][]time.Duration)
}
f(m.measures)
}
//
// Interface Metrics
//
func (m *MockMetrics) MeasureSince(key string, start time.Time) {
key = m.Prefix + key
duration := time.Since(start)
m.WithMeasures(func(measures map[string][]time.Duration) {
measure, ok := m.measures[key]
if !ok {
measure = make([]time.Duration, 0)
}
measures[key] = append(measure, duration)
})
}
func (m *MockMetrics) IncCounter(key string) {
key = m.Prefix + key
m.WithCounters(func(counters map[string]int64) {
counter, ok := counters[key]
if !ok {
counter = 0
}
counters[key] = counter + 1
})
}
func (m *MockMetrics) IncCounterBy(key string, value int64) {
key = m.Prefix + key
m.WithCounters(func(counters map[string]int64) {
counter, ok := counters[key]
if !ok {
counter = 0
}
counters[key] = counter + value
})
}
func (m *MockMetrics) IncFloatCounterBy(key string, value float64) {
key = m.Prefix + key
m.WithFloatCounters(func(floatCounters map[string]float64) {
counter, ok := floatCounters[key]
if !ok {
counter = 0
}
floatCounters[key] = counter + value
})
}
func (*MockMetrics) MeasureRouteLookup(start time.Time) {
panic("implement me")
}
func (*MockMetrics) MeasureFilterRequest(filterName string, start time.Time) {
panic("implement me")
}
func (*MockMetrics) MeasureAllFiltersRequest(routeId string, start time.Time) {
panic("implement me")
}
func (*MockMetrics) MeasureBackend(routeId string, start time.Time) {
panic("implement me")
}
func (*MockMetrics) MeasureBackendHost(routeBackendHost string, start time.Time) {
panic("implement me")
}
func (*MockMetrics) MeasureFilterResponse(filterName string, start time.Time) {
panic("implement me")
}
func (*MockMetrics) MeasureAllFiltersResponse(routeId string, start time.Time) {
panic("implement me")
}
func (*MockMetrics) MeasureResponse(code int, method string, routeId string, start time.Time) {
panic("implement me")
}
func (*MockMetrics) MeasureServe(routeId, host, method string, code int, start time.Time) {
panic("implement me")
}
func (*MockMetrics) IncRoutingFailures() {
panic("implement me")
}
func (*MockMetrics) IncErrorsBackend(routeId string) {
panic("implement me")
}
func (*MockMetrics) MeasureBackend5xx(t time.Time) {
panic("implement me")
}
func (*MockMetrics) IncErrorsStreaming(routeId string) {
panic("implement me")
}
func (*MockMetrics) RegisterHandler(path string, handler *http.ServeMux) {
panic("implement me")
}
func (*MockMetrics) UpdateGauge(key string, value float64) {
panic("implement me")
}