forked from giuliov/seleniumv4_grid_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selenium_grid_exporter.go
312 lines (274 loc) · 8.5 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
)
const (
nameSpace = "selenium"
gridSubsystem = "grid"
nodeSubsystem = "node"
nodeIdLabel = "node_id"
nodeUriLabel = "node_uri"
statusLabel = "status"
versionLabel = "version"
)
var (
versionFlag = flag.Bool("version", false, "Prints the version and exits.")
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.")
)
var (
version string
gitCommit string
)
type Exporter struct {
URI string
mutex sync.RWMutex
up, totalSlots, maxSession, sessionCount, sessionQueueSize prometheus.Gauge
version *prometheus.GaugeVec
nodeCount prometheus.Gauge
nodeStatus, nodeMaxSession, nodeSlotCount, nodeSessionCount *prometheus.GaugeVec
nodeVersion *prometheus.GaugeVec
}
type hubResponse struct {
Data struct {
Grid struct {
TotalSlots float64 `json:"totalSlots"`
MaxSession float64 `json:"maxSession"`
SessionCount float64 `json:"sessionCount"`
SessionQueueSize float64 `json:"sessionQueueSize"`
// new information
NodeCount float64 `json:"nodeCount"`
Version string `json:"version"`
} `json:"grid"`
NodesInfo struct {
Nodes []HubResponseNode `json:"nodes"`
} `json:"nodesInfo"`
// TODO sessionsInfo { sessionQueueRequests, sessions { capabilities, startTime, nodeId, sessionDurationMillis } }
} `json:"data"`
}
type HubResponseNode struct {
Id string `json:"id"`
Uri string `json:"uri"`
Status string `json:"status"`
MaxSession float64 `json:"maxSession"`
SlotCount float64 `json:"slotCount"`
SessionCount float64 `json:"sessionCount"`
Version string `json:"version"`
}
func NewExporter(uri string) *Exporter {
log.Infoln("Collecting data from:", uri)
return &Exporter{
URI: uri,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: gridSubsystem,
Name: "up",
Help: "was the last scrape of Selenium Grid successful.",
}),
totalSlots: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: gridSubsystem,
Name: "total_slots",
Help: "total number of usedSlots",
}),
maxSession: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: gridSubsystem,
Name: "max_session",
Help: "maximum number of sessions",
}),
sessionCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: gridSubsystem,
Name: "session_count",
Help: "number of active sessions",
}),
sessionQueueSize: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: gridSubsystem,
Name: "session_queue_size",
Help: "number of queued sessions",
}),
nodeCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: gridSubsystem,
Name: "node_count",
Help: "number of nodes",
}),
// NEW
version: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: gridSubsystem,
Name: "version",
Help: "Hub/Router version",
}, []string{versionLabel}),
// nodes
nodeStatus: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: nodeSubsystem,
Name: "status",
Help: "node status",
}, []string{nodeIdLabel, nodeUriLabel, statusLabel}),
nodeMaxSession: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: nodeSubsystem,
Name: "max_session",
Help: "maximum number of sessions on node",
}, []string{nodeIdLabel, nodeUriLabel}),
nodeSlotCount: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: nodeSubsystem,
Name: "slot_count",
Help: "number of slots on node",
}, []string{nodeIdLabel, nodeUriLabel}),
nodeSessionCount: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: nodeSubsystem,
Name: "session_count",
Help: "number of active sessions on node",
}, []string{nodeIdLabel, nodeUriLabel}),
nodeVersion: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: nodeSubsystem,
Name: "version",
Help: "Node version",
}, []string{nodeIdLabel, nodeUriLabel, versionLabel}),
}
}
/*
Describe is called by Prometheus on startup of this monitor. It needs to tell
the caller about all of the available metrics. It is also called during "unregister".
*/
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.up.Describe(ch)
e.totalSlots.Describe(ch)
e.maxSession.Describe(ch)
e.sessionCount.Describe(ch)
e.sessionQueueSize.Describe(ch)
e.nodeCount.Describe(ch)
e.version.Describe(ch)
e.nodeStatus.Describe(ch)
e.nodeMaxSession.Describe(ch)
e.nodeSlotCount.Describe(ch)
e.nodeSessionCount.Describe(ch)
e.nodeVersion.Describe(ch)
}
/*
Collect is called by Prometheus at regular intervals to provide current data
*/
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.scrape()
ch <- e.up
ch <- e.totalSlots
ch <- e.maxSession
ch <- e.sessionCount
ch <- e.sessionQueueSize
//new
ch <- e.nodeCount
e.version.Collect(ch)
e.nodeStatus.Collect(ch)
e.nodeMaxSession.Collect(ch)
e.nodeSlotCount.Collect(ch)
e.nodeSessionCount.Collect(ch)
e.nodeVersion.Collect(ch)
return
}
func (e *Exporter) scrape() {
e.totalSlots.Set(0)
e.maxSession.Set(0)
e.sessionCount.Set(0)
e.sessionQueueSize.Set(0)
e.nodeCount.Set(0)
e.version.Reset()
e.nodeStatus.Reset()
e.nodeMaxSession.Reset()
e.nodeSlotCount.Reset()
e.nodeSessionCount.Reset()
e.nodeVersion.Reset()
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
}
grid := hResponse.Data.Grid
e.totalSlots.Set(grid.TotalSlots)
e.maxSession.Set(grid.MaxSession)
e.sessionCount.Set(grid.SessionCount)
e.sessionQueueSize.Set(grid.SessionQueueSize)
//new
e.nodeCount.Set(grid.NodeCount)
e.version.WithLabelValues(grid.Version).Add(1.0)
for _, n := range hResponse.Data.NodesInfo.Nodes {
e.nodeStatus.WithLabelValues(n.Id, n.Uri, n.Status).Add(1.0)
e.nodeMaxSession.WithLabelValues(n.Id, n.Uri).Add(n.MaxSession)
e.nodeSlotCount.WithLabelValues(n.Id, n.Uri).Add(n.SlotCount)
e.nodeSessionCount.WithLabelValues(n.Id, n.Uri).Add(n.SessionCount)
e.nodeVersion.WithLabelValues(n.Id, n.Uri, n.Version).Add(1.0)
}
}
func (e Exporter) fetch() (output []byte, err error) {
url := (e.URI + "/graphql")
method := "POST"
payload := strings.NewReader(`{
"query": "{
grid {totalSlots, maxSession, sessionCount, sessionQueueSize, nodeCount, version },
nodesInfo { nodes { id, uri, status, maxSession, slotCount, sessionCount, version } }
}"
}`)
client := http.Client{
Timeout: 3 * time.Second,
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
//s := string(body)
//fmt.Println(s)
return body, err
}
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("Selenium Grid Exporter v%s (%s)\n", version, gitCommit)
os.Exit(0)
}
log.Infoln("Starting selenium_grid_exporter", version)
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))
}