forked from huaweicse/cse-collector
/
cse_collector.go
executable file
·169 lines (145 loc) · 5.46 KB
/
cse_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
package metricsink
// Forked from github.com/afex/hystrix-go
// Some parts of this file have been modified to make it functional in this package
import (
"crypto/tls"
"net/http"
"time"
"github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix/metric_collector"
"github.com/rcrowley/go-metrics"
)
// CseCollector is a struct to keeps metric information of Http requests
type CseCollector struct {
attempts string
errors string
successes string
failures string
rejects string
shortCircuits string
timeouts string
fallbackSuccesses string
fallbackFailures string
totalDuration string
runDuration string
}
// CseCollectorConfig is a struct to keep monitoring information
type CseCollectorConfig struct {
// CseMonitorAddr is the http address of the csemonitor server
CseMonitorAddr string
// Headers for csemonitor server
Header http.Header
// TickInterval spcifies the period that this collector will send metrics to the server.
TimeInterval time.Duration
// Config structure to configure a TLS client for sending Metric data
TLSConfig *tls.Config
}
// InitializeCseCollector starts the CSE collector in a new Thread
func InitializeCseCollector(config *CseCollectorConfig, r metrics.Registry, app, version, service, env string) {
go NewReporter(r, config.CseMonitorAddr, config.Header, config.TimeInterval, config.TLSConfig, app, version, service, env).Run()
}
// NewCseCollector creates a new Collector Object
func NewCseCollector(name string) metricCollector.MetricCollector {
return &CseCollector{
attempts: name + ".attempts",
errors: name + ".errors",
successes: name + ".successes",
failures: name + ".failures",
rejects: name + ".rejects",
shortCircuits: name + ".shortCircuits",
timeouts: name + ".timeouts",
fallbackSuccesses: name + ".fallbackSuccesses",
fallbackFailures: name + ".fallbackFailures",
totalDuration: name + ".totalDuration",
runDuration: name + ".runDuration",
}
}
func (c *CseCollector) incrementCounterMetric(prefix string) {
count, ok := metrics.GetOrRegister(prefix, metrics.NewCounter).(metrics.Counter)
if !ok {
return
}
count.Inc(1)
}
func (c *CseCollector) updateTimerMetric(prefix string, dur time.Duration) {
count, ok := metrics.GetOrRegister(prefix, metrics.NewTimer).(metrics.Timer)
if !ok {
return
}
count.Update(dur)
}
func (c *CseCollector) cleanMetric(prefix string) {
count, ok := metrics.GetOrRegister(prefix, metrics.NewCounter).(metrics.Counter)
if !ok {
return
}
count.Clear()
}
// IncrementAttempts function increments the number of calls to this circuit.
// This registers as a counter
func (c *CseCollector) IncrementAttempts() {
c.incrementCounterMetric(c.attempts)
}
// IncrementErrors function increments the number of unsuccessful attempts.
// Attempts minus Errors will equal successes.
// Errors are result from an attempt that is not a success.
// This registers as a counter
func (c *CseCollector) IncrementErrors() {
c.incrementCounterMetric(c.errors)
}
// IncrementSuccesses function increments the number of requests that succeed.
// This registers as a counter
func (c *CseCollector) IncrementSuccesses() {
c.incrementCounterMetric(c.successes)
}
// IncrementFailures function increments the number of requests that fail.
// This registers as a counter
func (c *CseCollector) IncrementFailures() {
c.incrementCounterMetric(c.failures)
}
// IncrementRejects function increments the number of requests that are rejected.
// This registers as a counter
func (c *CseCollector) IncrementRejects() {
c.incrementCounterMetric(c.rejects)
}
// IncrementShortCircuits function increments the number of requests that short circuited due to the circuit being open.
// This registers as a counter
func (c *CseCollector) IncrementShortCircuits() {
c.incrementCounterMetric(c.shortCircuits)
}
// IncrementTimeouts function increments the number of timeouts that occurred in the circuit breaker.
// This registers as a counter
func (c *CseCollector) IncrementTimeouts() {
c.incrementCounterMetric(c.timeouts)
}
// IncrementFallbackSuccesses function increments the number of successes that occurred during the execution of the fallback function.
// This registers as a counter
func (c *CseCollector) IncrementFallbackSuccesses() {
c.incrementCounterMetric(c.fallbackSuccesses)
}
// IncrementFallbackFailures function increments the number of failures that occurred during the execution of the fallback function.
// This registers as a counter
func (c *CseCollector) IncrementFallbackFailures() {
c.incrementCounterMetric(c.fallbackFailures)
}
// UpdateTotalDuration function updates the internal counter of how long we've run for.
// This registers as a timer
func (c *CseCollector) UpdateTotalDuration(timeSinceStart time.Duration) {
c.updateTimerMetric(c.totalDuration, timeSinceStart)
}
// UpdateRunDuration function updates the internal counter of how long the last run took.
// This registers as a timer
func (c *CseCollector) UpdateRunDuration(runDuration time.Duration) {
c.updateTimerMetric(c.runDuration, runDuration)
}
// Reset function is a noop operation in this collector.
func (c *CseCollector) Reset() {
c.cleanMetric(c.attempts)
c.cleanMetric(c.failures)
c.cleanMetric(c.successes)
c.cleanMetric(c.shortCircuits)
c.cleanMetric(c.errors)
c.cleanMetric(c.rejects)
c.cleanMetric(c.timeouts)
c.cleanMetric(c.fallbackSuccesses)
c.cleanMetric(c.fallbackFailures)
}