forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
executable file
·150 lines (120 loc) · 3.62 KB
/
metrics.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
package hystrix
import (
"sync"
"time"
"github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix/metric_collector"
"github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix/rolling"
)
type commandExecution struct {
Types []string `json:"types"`
Start time.Time `json:"start_time"`
RunDuration time.Duration `json:"run_duration"`
}
type metricExchange struct {
Name string
Updates chan *commandExecution
Mutex *sync.RWMutex
metricCollectors []metricCollector.MetricCollector
}
func newMetricExchange(name string) *metricExchange {
m := &metricExchange{}
m.Name = name
m.Updates = make(chan *commandExecution, 2000)
m.Mutex = &sync.RWMutex{}
m.metricCollectors = metricCollector.Registry.InitializeMetricCollectors(name)
m.Reset()
go m.Monitor()
return m
}
// The Default Collector function will panic if collectors are not setup to specification.
func (m *metricExchange) DefaultCollector() *metricCollector.DefaultMetricCollector {
if len(m.metricCollectors) < 1 {
panic("No Metric Collectors Registered")
}
collection, ok := m.metricCollectors[0].(*metricCollector.DefaultMetricCollector)
if !ok {
panic("Default metric collector is not registered correctly. The default metric collector must be registered first")
}
return collection
}
func (m *metricExchange) Monitor() {
for update := range m.Updates {
// we only grab a read lock to make sure Reset() isn't changing the numbers.
m.Mutex.RLock()
totalDuration := time.Since(update.Start)
wg := &sync.WaitGroup{}
for _, collector := range m.metricCollectors {
wg.Add(1)
go m.IncrementMetrics(wg, collector, update, totalDuration)
}
wg.Wait()
m.Mutex.RUnlock()
}
}
func (m *metricExchange) IncrementMetrics(wg *sync.WaitGroup, collector metricCollector.MetricCollector, update *commandExecution, totalDuration time.Duration) {
// granular metrics
if update.Types[0] == "success" {
collector.IncrementAttempts()
collector.IncrementSuccesses()
}
if update.Types[0] == "failure" {
collector.IncrementFailures()
collector.IncrementAttempts()
collector.IncrementErrors()
}
if update.Types[0] == "rejected" {
collector.IncrementRejects()
collector.IncrementAttempts()
collector.IncrementErrors()
}
if update.Types[0] == "short-circuit" {
collector.IncrementShortCircuits()
collector.IncrementAttempts()
}
if update.Types[0] == "timeout" {
collector.IncrementTimeouts()
collector.IncrementAttempts()
collector.IncrementErrors()
}
if len(update.Types) > 1 {
// fallback metrics
if update.Types[1] == "fallback-success" {
collector.IncrementFallbackSuccesses()
}
if update.Types[1] == "fallback-failure" {
collector.IncrementFallbackFailures()
}
}
collector.UpdateTotalDuration(totalDuration)
collector.UpdateRunDuration(update.RunDuration)
wg.Done()
}
func (m *metricExchange) Reset() {
m.Mutex.Lock()
defer m.Mutex.Unlock()
for _, collector := range m.metricCollectors {
collector.Reset()
}
}
func (m *metricExchange) Requests() *rolling.Number {
m.Mutex.RLock()
defer m.Mutex.RUnlock()
return m.requestsLocked()
}
func (m *metricExchange) requestsLocked() *rolling.Number {
return m.DefaultCollector().NumRequests()
}
func (m *metricExchange) ErrorPercent(now time.Time) int {
m.Mutex.RLock()
defer m.Mutex.RUnlock()
var errPct float64
reqs := m.requestsLocked().Sum(now)
errs := m.DefaultCollector().Errors().Sum(now)
if reqs > 0 {
errPct = (float64(errs) / float64(reqs)) * 100
}
return int(errPct + 0.5)
}
func (m *metricExchange) IsHealthy(now time.Time) bool {
return m.ErrorPercent(now) < getSettings(m.Name).ErrorPercentThreshold
}