forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
default_metric_collector.go
executable file
·212 lines (181 loc) · 6.16 KB
/
default_metric_collector.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package metricCollector
import (
"sync"
"time"
"github.com/ServiceComb/go-chassis/third_party/forked/afex/hystrix-go/hystrix/rolling"
)
// DefaultMetricCollector holds information about the circuit state.
// This implementation of MetricCollector is the canonical source of information about the circuit.
// It is used for for all internal hystrix operations
// including circuit health checks and metrics sent to the hystrix dashboard.
//
// Metric Collectors do not need Mutexes as they are updated by circuits within a locked context.
type DefaultMetricCollector struct {
mutex *sync.RWMutex
numRequests *rolling.Number
errors *rolling.Number
successes *rolling.Number
failures *rolling.Number
rejects *rolling.Number
shortCircuits *rolling.Number
timeouts *rolling.Number
fallbackSuccesses *rolling.Number
fallbackFailures *rolling.Number
totalDuration *rolling.Timing
runDuration *rolling.Timing
}
func newDefaultMetricCollector(name string) MetricCollector {
m := &DefaultMetricCollector{}
m.mutex = &sync.RWMutex{}
m.Reset()
return m
}
// NumRequests returns the rolling number of requests
func (d *DefaultMetricCollector) NumRequests() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.numRequests
}
// Errors returns the rolling number of errors
func (d *DefaultMetricCollector) Errors() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.errors
}
// Successes returns the rolling number of successes
func (d *DefaultMetricCollector) Successes() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.successes
}
// Failures returns the rolling number of failures
func (d *DefaultMetricCollector) Failures() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.failures
}
// Rejects returns the rolling number of rejects
func (d *DefaultMetricCollector) Rejects() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.rejects
}
// ShortCircuits returns the rolling number of short circuits
func (d *DefaultMetricCollector) ShortCircuits() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.shortCircuits
}
// Timeouts returns the rolling number of timeouts
func (d *DefaultMetricCollector) Timeouts() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.timeouts
}
// FallbackSuccesses returns the rolling number of fallback successes
func (d *DefaultMetricCollector) FallbackSuccesses() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.fallbackSuccesses
}
// FallbackFailures returns the rolling number of fallback failures
func (d *DefaultMetricCollector) FallbackFailures() *rolling.Number {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.fallbackFailures
}
// TotalDuration returns the rolling total duration
func (d *DefaultMetricCollector) TotalDuration() *rolling.Timing {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.totalDuration
}
// RunDuration returns the rolling run duration
func (d *DefaultMetricCollector) RunDuration() *rolling.Timing {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.runDuration
}
// IncrementAttempts increments the number of requests seen in the latest time bucket.
func (d *DefaultMetricCollector) IncrementAttempts() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.numRequests.Increment(1)
}
// IncrementErrors increments the number of errors seen in the latest time bucket.
// Errors are any result from an attempt that is not a success.
func (d *DefaultMetricCollector) IncrementErrors() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.errors.Increment(1)
}
// IncrementSuccesses increments the number of successes seen in the latest time bucket.
func (d *DefaultMetricCollector) IncrementSuccesses() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.successes.Increment(1)
}
// IncrementFailures increments the number of failures seen in the latest time bucket.
func (d *DefaultMetricCollector) IncrementFailures() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.failures.Increment(1)
}
// IncrementRejects increments the number of rejected requests seen in the latest time bucket.
func (d *DefaultMetricCollector) IncrementRejects() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.rejects.Increment(1)
}
// IncrementShortCircuits increments the number of rejected requests seen in the latest time bucket.
func (d *DefaultMetricCollector) IncrementShortCircuits() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.shortCircuits.Increment(1)
}
// IncrementTimeouts increments the number of requests that timed out in the latest time bucket.
func (d *DefaultMetricCollector) IncrementTimeouts() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.timeouts.Increment(1)
}
// IncrementFallbackSuccesses increments the number of successful calls to the fallback function in the latest time bucket.
func (d *DefaultMetricCollector) IncrementFallbackSuccesses() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.fallbackSuccesses.Increment(1)
}
// IncrementFallbackFailures increments the number of failed calls to the fallback function in the latest time bucket.
func (d *DefaultMetricCollector) IncrementFallbackFailures() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.fallbackFailures.Increment(1)
}
// UpdateTotalDuration updates the total amount of time this circuit has been running.
func (d *DefaultMetricCollector) UpdateTotalDuration(timeSinceStart time.Duration) {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.totalDuration.Add(timeSinceStart)
}
// UpdateRunDuration updates the amount of time the latest request took to complete.
func (d *DefaultMetricCollector) UpdateRunDuration(runDuration time.Duration) {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.runDuration.Add(runDuration)
}
// Reset resets all metrics in this collector to 0.
func (d *DefaultMetricCollector) Reset() {
d.mutex.Lock()
defer d.mutex.Unlock()
d.numRequests = rolling.NewNumber()
d.errors = rolling.NewNumber()
d.successes = rolling.NewNumber()
d.rejects = rolling.NewNumber()
d.shortCircuits = rolling.NewNumber()
d.failures = rolling.NewNumber()
d.timeouts = rolling.NewNumber()
d.fallbackSuccesses = rolling.NewNumber()
d.fallbackFailures = rolling.NewNumber()
d.totalDuration = rolling.NewTiming()
d.runDuration = rolling.NewTiming()
}