-
Notifications
You must be signed in to change notification settings - Fork 53
/
collectors.go
68 lines (55 loc) · 2.43 KB
/
collectors.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
/*
Module for defining collectors and their good/bad events API.
There are registered manually in preconfigured metrics builder in `def.go`
There are registered automatically to an auto-instrumentation server
in `pkg/test/instrumentation.go`
*/
package query
import (
"fmt"
"math/rand"
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
var verbs = []string{"POST", "GET", "PUT", "DELETE"}
var goodCodes = []int{200, 201, 202}
var badCodes = []int{404, 429, 500, 502, 503}
var (
uptimeCollector *prometheus.GaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "uptime_good",
}, []string{"hostname", "ip", "job"})
uptimeGoodEvents http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
uptimeCollector.WithLabelValues(r.Host, r.RemoteAddr, MockTestServerName).Set(1)
}
uptimeBadEvents http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
uptimeCollector.WithLabelValues(r.Host, r.RemoteAddr, MockTestServerName).Set(0)
}
availabilityCollector *prometheus.CounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "http_request_duration_seconds_count",
},
[]string{"code", "verb"},
)
availabilityGoodEvents http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
randomStatusCode := goodCodes[rand.Intn(len(goodCodes))]
randomVerb := verbs[rand.Intn(len(verbs))]
availabilityCollector.WithLabelValues(fmt.Sprintf("%d", randomStatusCode), randomVerb).Inc()
}
availabilityBadEvents http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
randomStatusCode := badCodes[rand.Intn(len(badCodes))]
randomVerb := verbs[rand.Intn(len(verbs))]
availabilityCollector.WithLabelValues(fmt.Sprintf("%d", randomStatusCode), randomVerb).Inc()
}
latencyCollector *prometheus.HistogramVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_request_duration_seconds_bucket",
}, []string{"hostname", "ip", "job"})
latencyGoodEvents http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
// request duration faster than 0.3s / 0.300 ms
randLatency := rand.Float64() * 0.29
latencyCollector.WithLabelValues(r.Host, r.RemoteAddr, MockTestServerName).Observe(randLatency)
}
latencyBadEvents http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
// request duration slower than 0.3s / 0.300 ms
randLatency := rand.Float64() + 0.30
latencyCollector.WithLabelValues(r.Host, r.RemoteAddr, MockTestServerName).Observe(randLatency)
}
)