forked from phsym/xk6-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
294 lines (238 loc) · 6.81 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package prometheus
import (
"strings"
"fmt"
"net"
"net/http"
"net/url"
"github.com/gorilla/schema"
"github.com/loadimpact/k6/output"
"github.com/loadimpact/k6/stats"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Register the extensions on module initialization.
func init() {
output.RegisterExtension("prometheus", New)
}
type options struct {
Port int
Host string
Subsystem string
Namespace string
}
type outputImpl struct {
params *output.Params
options *options
metrics map[string]interface{}
}
func New(params output.Params) (output.Output, error) {
o := &outputImpl{params: ¶ms, metrics: make(map[string]interface{})}
return o, nil
}
func (o *outputImpl) Description() string {
return fmt.Sprintf("prometheus (%s:%d)", o.options.Host, o.options.Port)
}
func getopts(qs string) (*options, error) {
opts := &options{
Port: 5656,
Host: "",
Namespace: "",
Subsystem: "",
}
if qs == "" {
return opts, nil
}
v, err := url.ParseQuery(qs)
if err != nil {
return nil, err
}
decoder := schema.NewDecoder()
if err = decoder.Decode(opts, v); err != nil {
return nil, err
}
return opts, nil
}
func (o *outputImpl) Start() (err error) {
if o.options, err = getopts(o.params.ConfigArgument); err != nil {
return err
}
addr := fmt.Sprintf("%s:%d", o.options.Host, o.options.Port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
go func() {
if err := http.Serve(listener, promhttp.Handler()); err != nil {
o.params.Logger.Error(err)
}
}()
return nil
}
func (o *outputImpl) Stop() error {
return nil
}
func (o *outputImpl) AddMetricSamples(samples []stats.SampleContainer) {
for i := range samples {
all := samples[i].GetSamples()
for j := range all {
o.handleSample(&all[j])
}
}
}
func (o *outputImpl) handleSample(sample *stats.Sample) {
var handler func(*stats.Sample)
switch sample.Metric.Type {
case stats.Counter:
handler = o.handleCounter
case stats.Gauge:
handler = o.handleGauge
case stats.Rate:
handler = o.handleRate
case stats.Trend:
handler = o.handleTrend
default:
o.params.Logger.Warnf("Unknown metric type: %v", sample.Metric.Type)
return
}
handler(sample)
}
func (o *outputImpl) handleCounter(sample *stats.Sample) {
if counter := o.getCounter(sample.Metric.Name, "k6 counter"); counter != nil {
counter.Add(sample.Value)
}
}
func (o *outputImpl) handleGauge(sample *stats.Sample) {
if gauge := o.getGauge(sample.Metric.Name, "k6 gauge"); gauge != nil {
gauge.Set(sample.Value)
}
}
func (o *outputImpl) handleRate(sample *stats.Sample) {
if histogram := o.getHistogram(sample.Metric.Name, "k6 rate", []float64{0}); histogram != nil {
histogram.Observe(sample.Value)
}
}
func (o *outputImpl) handleTrend(sample *stats.Sample) {
if summary := o.getSummary(sample.Metric.Name, "k6 trend"); summary != nil {
summary.Observe(sample.Value)
}
if gauge := o.getGauge(sample.Metric.Name+"_value", "k6 trend value"); gauge != nil {
gauge.Set(sample.Value)
}
}
func (o *outputImpl) getCounter(name string, helpSuffix string) (counter prometheus.Counter) {
if col, ok := o.metrics[name]; ok {
if c, tok := col.(prometheus.Counter); tok {
counter = c
}
}
if counter == nil {
counter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: o.options.Namespace,
Subsystem: o.options.Subsystem,
Name: name,
Help: helpFor(name, helpSuffix),
})
if err := prometheus.Register(counter); err != nil {
o.params.Logger.Error(err)
return nil
}
o.metrics[name] = counter
}
return counter
}
func (o *outputImpl) getGauge(name string, helpSuffix string) (gauge prometheus.Gauge) {
if gau, ok := o.metrics[name]; ok {
if g, tok := gau.(prometheus.Gauge); tok {
gauge = g
}
}
if gauge == nil {
gauge = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: o.options.Namespace,
Subsystem: o.options.Subsystem,
Name: name,
Help: helpFor(name, helpSuffix),
})
if err := prometheus.Register(gauge); err != nil {
o.params.Logger.Error(err)
return nil
}
o.metrics[name] = gauge
}
return gauge
}
func (o *outputImpl) getSummary(name string, helpSuffix string) (summary prometheus.Summary) {
if sum, ok := o.metrics[name]; ok {
if s, tok := sum.(prometheus.Summary); tok {
summary = s
}
}
if summary == nil {
summary = prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: o.options.Namespace,
Subsystem: o.options.Subsystem,
Name: name,
Help: helpFor(name, helpSuffix),
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.001},
})
if err := prometheus.Register(summary); err != nil {
o.params.Logger.Error(err)
return nil
}
o.metrics[name] = summary
}
return summary
}
func (o *outputImpl) getHistogram(name string, helpSuffix string, buckets []float64) (histogram prometheus.Histogram) {
if his, ok := o.metrics[name]; ok {
if h, tok := his.(prometheus.Histogram); tok {
histogram = h
}
}
if histogram == nil {
histogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: o.options.Namespace,
Subsystem: o.options.Subsystem,
Name: name,
Help: helpFor(name, helpSuffix),
Buckets: buckets,
})
if err := prometheus.Register(histogram); err != nil {
o.params.Logger.Error(err)
return nil
}
o.metrics[name] = histogram
}
return histogram
}
func helpFor(name string, helpSuffix string) string {
if h, ok := builtinMetrics[name]; ok {
return h
}
if h, ok := builtinMetrics[strings.TrimSuffix(name, "_value")]; ok {
return h + " (value)"
}
return name + " " + helpSuffix
}
var (
builtinMetrics = map[string]string{
"vus": "Current number of active virtual users",
"vus_max": "Max possible number of virtual users",
"iterations": "The aggregate number of times the VUs in the test have executed",
"iteration_duration": "The time it took to complete one full iteration",
"dropped_iterations": "The number of iterations that could not be started",
"data_received": "The amount of received data",
"data_sent": "The amount of data sent",
"checks": "The rate of successful checks",
"http_reqs": "How many HTTP requests has k6 generated, in total",
"http_req_blocked": "Time spent blocked before initiating the request",
"http_req_connecting": "Time spent establishing TCP connection",
"http_req_tls_handshaking": "Time spent handshaking TLS session",
"http_req_sending": "Time spent sending data",
"http_req_waiting": "Time spent waiting for response",
"http_req_receiving": "Time spent receiving response data",
"http_req_duration": "Total time for the request",
"http_req_failed": "The rate of failed requests",
}
)