-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathselenium_grid_exporter.go
163 lines (128 loc) · 3.66 KB
/
selenium_grid_exporter.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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"net/http"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
)
const (
nameSpace = "selenium_grid"
subSystem = "hub"
)
var (
listenAddress = flag.String("listen-address", ":8080", "Address on which to expose metrics.")
metricsPath = flag.String("telemetry-path", "/metrics", "Path under which to expose metrics.")
scrapeURI = flag.String("scrape-uri", "http://grid.local", "URI on which to scrape Selenium Grid.")
)
type Exporter struct {
URI string
mutex sync.RWMutex
up, slotsTotal, slotsFree, newSessionRequestCount prometheus.Gauge
}
type hubResponse struct {
Success bool `json:"success"`
Debug bool `json:"debug"`
CleanUpCycle int `json:"cleanUpCycle"`
Slots slotCounts `json:"slotCounts"`
NewSession float64 `json:"newSessionRequestCount"`
}
type slotCounts struct {
Free float64 `json:"free"`
Total float64 `json:"total"`
}
func NewExporter(uri string) *Exporter {
log.Infoln("Collecting data from:", uri)
return &Exporter{
URI: uri,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Name: "up",
Help: "was the last scrape of Selenium Grid successful.",
}),
slotsTotal: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: subSystem,
Name: "slotsTotal",
Help: "total number of slots",
}),
slotsFree: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: subSystem,
Name: "slotsFree",
Help: "number of free slots",
}),
newSessionRequestCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: subSystem,
Name: "sessions_backlog",
Help: "number of sessions waiting for a slot",
}),
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.up.Describe(ch)
e.slotsTotal.Describe(ch)
e.slotsFree.Describe(ch)
e.newSessionRequestCount.Describe(ch)
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.scrape()
ch <- e.up
ch <- e.slotsTotal
ch <- e.slotsFree
ch <- e.newSessionRequestCount
return
}
func (e *Exporter) scrape() {
e.slotsTotal.Set(0)
e.slotsFree.Set(0)
body, err := e.fetch()
if err != nil {
e.up.Set(0)
log.Errorf("Can't scrape Selenium Grid: %v", err)
return
}
e.up.Set(1)
var hResponse hubResponse
if err := json.Unmarshal(body, &hResponse); err != nil {
log.Errorf("Can't decode Selenium Grid response: %v", err)
return
}
e.slotsTotal.Set(hResponse.Slots.Total)
e.slotsFree.Set(hResponse.Slots.Free)
e.newSessionRequestCount.Set(hResponse.NewSession)
}
func (e Exporter) fetch() (output []byte, err error) {
client := http.Client{
Timeout: 3 * time.Second,
}
response, err := client.Get(e.URI + "/grid/api/hub")
if err != nil {
return nil, err
}
defer response.Body.Close()
output, err = ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return
}
func main() {
flag.Parse()
log.Infoln("Starting selenium_grid_exporter")
prometheus.MustRegister(NewExporter(*scrapeURI))
prometheus.Unregister(prometheus.NewGoCollector())
prometheus.Unregister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, *metricsPath, http.StatusMovedPermanently)
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}