forked from wakeful/selenium_grid_exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselenium_grid_exporter.go
More file actions
186 lines (151 loc) · 4.29 KB
/
selenium_grid_exporter.go
File metadata and controls
186 lines (151 loc) · 4.29 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"strings"
"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, totalSlots, maxSession, sessionCount, sessionQueueSize prometheus.Gauge
}
type hubResponse struct {
Data struct {
Grid struct {
TotalSlots float64 `json:"totalSlots"`
MaxSession float64 `json:"maxSession"`
SessionCount float64 `json:"sessionCount"`
SessionQueueSize float64 `json:"sessionQueueSize"`
} `json:"grid"`
} `json:"data"`
}
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.",
}),
totalSlots: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: subSystem,
Name: "totalSlots",
Help: "total number of usedSlots",
}),
maxSession: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: subSystem,
Name: "maxSession",
Help: "maximum number of sessions",
}),
sessionCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: subSystem,
Name: "sessionCount",
Help: "number of active sessions",
}),
sessionQueueSize: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: subSystem,
Name: "sessionQueueSize",
Help: "number of queued sessions",
}),
}
}
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)
}
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
return
}
func (e *Exporter) scrape() {
e.totalSlots.Set(0)
e.maxSession.Set(0)
e.sessionCount.Set(0)
e.sessionQueueSize.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.totalSlots.Set(hResponse.Data.Grid.TotalSlots)
e.maxSession.Set(hResponse.Data.Grid.MaxSession)
e.sessionCount.Set(hResponse.Data.Grid.SessionCount)
e.sessionQueueSize.Set(hResponse.Data.Grid.SessionQueueSize)
}
func (e Exporter) fetch() (output []byte, err error) {
url := (e.URI + "/graphql")
method := "POST"
payload := strings.NewReader(`{
"query": "{ grid {totalSlots, maxSession, sessionCount, sessionQueueSize} }"
}`)
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()
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))
}