/
prometheus.go
203 lines (172 loc) · 6.46 KB
/
prometheus.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
package metrics
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
promNamespace = "goresilience"
promCommandSubsystem = "command"
promRetrySubsystem = "retry"
promTimeoutSubsystem = "timeout"
promBulkheadSubsystem = "bulkhead"
promCBSubsystem = "circuitbreaker"
promChaosSubsystem = "chaos"
promConcurrencyLimitSubsystem = "concurrencylimit"
)
type prometheusRec struct {
// Metrics.
cmdExecutionDuration *prometheus.HistogramVec
retryRetries *prometheus.CounterVec
timeoutTimeouts *prometheus.CounterVec
bulkQueued *prometheus.CounterVec
bulkProcessed *prometheus.CounterVec
bulkTimeouts *prometheus.CounterVec
cbStateChanges *prometheus.CounterVec
chaosFailureInjections *prometheus.CounterVec
concurrencyLimitInflights *prometheus.GaugeVec
concurrencyLimitResult *prometheus.CounterVec
concurrencyLimitLimit *prometheus.GaugeVec
id string
reg prometheus.Registerer
}
// NewPrometheusRecorder returns a new Recorder that knows how to measure
// using Prometheus kind metrics.
func NewPrometheusRecorder(reg prometheus.Registerer) Recorder {
p := &prometheusRec{
reg: reg,
}
p.registerMetrics()
return p
}
func (p prometheusRec) WithID(id string) Recorder {
return &prometheusRec{
cmdExecutionDuration: p.cmdExecutionDuration,
retryRetries: p.retryRetries,
timeoutTimeouts: p.timeoutTimeouts,
bulkQueued: p.bulkQueued,
bulkProcessed: p.bulkProcessed,
bulkTimeouts: p.bulkTimeouts,
cbStateChanges: p.cbStateChanges,
chaosFailureInjections: p.chaosFailureInjections,
concurrencyLimitInflights: p.concurrencyLimitInflights,
concurrencyLimitResult: p.concurrencyLimitResult,
concurrencyLimitLimit: p.concurrencyLimitLimit,
id: id,
reg: p.reg,
}
}
func (p *prometheusRec) registerMetrics() {
p.cmdExecutionDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: promNamespace,
Subsystem: promCommandSubsystem,
Name: "execution_duration_seconds",
Help: "The duration of the command execution in seconds.",
}, []string{"id", "success"})
p.retryRetries = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promRetrySubsystem,
Name: "retries_total",
Help: "Total number of retries made by the retry runner.",
}, []string{"id"})
p.timeoutTimeouts = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promTimeoutSubsystem,
Name: "timeouts_total",
Help: "Total number of timeouts made by the timeout runner.",
}, []string{"id"})
p.bulkQueued = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promBulkheadSubsystem,
Name: "queued_total",
Help: "Total number of queued funcs made by the bulkhead runner.",
}, []string{"id"})
p.bulkProcessed = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promBulkheadSubsystem,
Name: "processed_total",
Help: "Total number of processed funcs made by the bulkhead runner.",
}, []string{"id"})
p.bulkTimeouts = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promBulkheadSubsystem,
Name: "timeouts_total",
Help: "Total number of timeouts funcs waiting for execution made by the bulkhead runner.",
}, []string{"id"})
p.cbStateChanges = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promCBSubsystem,
Name: "state_changes_total",
Help: "Total number of state changes made by the circuit breaker runner.",
}, []string{"id", "state"})
p.chaosFailureInjections = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promChaosSubsystem,
Name: "failure_injections_total",
Help: "Total number of failure injectionsmade by the chaos runner.",
}, []string{"id", "kind"})
p.concurrencyLimitInflights = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: promNamespace,
Subsystem: promConcurrencyLimitSubsystem,
Name: "inflight_executions",
Help: "The number of inflight executions, these are executing and queued.",
}, []string{"id"})
p.concurrencyLimitResult = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promConcurrencyLimitSubsystem,
Name: "result_total",
Help: "Total results of the executions measured by the limiter algorithm.",
}, []string{"id", "result"})
p.concurrencyLimitLimit = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: promNamespace,
Subsystem: promConcurrencyLimitSubsystem,
Name: "limiter_limit",
Help: "The concurrency limit measured and calculated by the limiter algorithm.",
}, []string{"id"})
p.reg.MustRegister(p.cmdExecutionDuration,
p.retryRetries,
p.timeoutTimeouts,
p.bulkQueued,
p.bulkProcessed,
p.bulkTimeouts,
p.cbStateChanges,
p.chaosFailureInjections,
p.concurrencyLimitInflights,
p.concurrencyLimitResult,
p.concurrencyLimitLimit,
)
}
func (p prometheusRec) ObserveCommandExecution(start time.Time, success bool) {
secs := time.Since(start).Seconds()
p.cmdExecutionDuration.WithLabelValues(p.id, fmt.Sprintf("%t", success)).Observe(secs)
}
func (p prometheusRec) IncRetry() {
p.retryRetries.WithLabelValues(p.id).Inc()
}
func (p prometheusRec) IncTimeout() {
p.timeoutTimeouts.WithLabelValues(p.id).Inc()
}
func (p prometheusRec) IncBulkheadQueued() {
p.bulkQueued.WithLabelValues(p.id).Inc()
}
func (p prometheusRec) IncBulkheadProcessed() {
p.bulkProcessed.WithLabelValues(p.id).Inc()
}
func (p prometheusRec) IncBulkheadTimeout() {
p.bulkTimeouts.WithLabelValues(p.id).Inc()
}
func (p prometheusRec) IncCircuitbreakerState(state string) {
p.cbStateChanges.WithLabelValues(p.id, state).Inc()
}
func (p prometheusRec) IncChaosInjectedFailure(kind string) {
p.chaosFailureInjections.WithLabelValues(p.id, kind).Inc()
}
func (p prometheusRec) SetConcurrencyLimitInflightExecutions(q int) {
p.concurrencyLimitInflights.WithLabelValues(p.id).Set(float64(q))
}
func (p prometheusRec) IncConcurrencyLimitResult(result string) {
p.concurrencyLimitResult.WithLabelValues(p.id, result).Inc()
}
func (p prometheusRec) SetConcurrencyLimitLimiterLimit(limit int) {
p.concurrencyLimitLimit.WithLabelValues(p.id).Set(float64(limit))
}