-
Notifications
You must be signed in to change notification settings - Fork 61
/
common.go
360 lines (314 loc) · 8.85 KB
/
common.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type (
resources struct {
CPUs float64 `json:"cpus"`
Disk float64 `json:"disk"`
Mem float64 `json:"mem"`
Ports ranges `json:"ports"`
}
task struct {
Name string `json:"name"`
ID string `json:"id"`
ExecutorID string `json:"executor_id"`
FrameworkID string `json:"framework_id"`
SlaveID string `json:"slave_id"`
State string `json:"state"`
Labels []label `json:"labels"`
Resources resources `json:"resources"`
Statuses []status `json:"statuses"`
}
label struct {
Key string `json:"key"`
Value string `json:"value"`
}
status struct {
State string `json:"state"`
Timestamp float64 `json:"timestamp"`
}
tokenResponse struct {
Token string `json:"token"`
}
tokenRequest struct {
UID string `json:"uid"`
Token string `json:"token"`
}
mesosSecret struct {
LoginEndpoint string `json:"login_endpoint"`
PrivateKey string `json:"private_key"`
Scheme string `json:"scheme"`
UID string `json:"uid"`
}
)
type metricMap map[string]float64
const LogErrNotFoundInMap = "Couldn't find key in map"
type settableCounterVec struct {
desc *prometheus.Desc
values []prometheus.Metric
}
func (c *settableCounterVec) Describe(ch chan<- *prometheus.Desc) {
ch <- c.desc
}
func (c *settableCounterVec) Collect(ch chan<- prometheus.Metric) {
for _, v := range c.values {
ch <- v
}
c.values = nil
}
func (c *settableCounterVec) Set(value float64, labelValues ...string) {
c.values = append(c.values, prometheus.MustNewConstMetric(c.desc, prometheus.CounterValue, value, labelValues...))
}
type settableCounter struct {
desc *prometheus.Desc
value prometheus.Metric
}
func (c *settableCounter) Describe(ch chan<- *prometheus.Desc) {
if c.desc == nil {
log.WithField("counter", c).Warn("NIL description")
}
ch <- c.desc
}
func (c *settableCounter) Collect(ch chan<- prometheus.Metric) {
if c.value == nil {
log.WithField("counter", c).Warn("NIL value")
}
ch <- c.value
}
func (c *settableCounter) Set(value float64) {
c.value = prometheus.MustNewConstMetric(c.desc, prometheus.CounterValue, value)
}
func newSettableCounter(subsystem, name, help string) *settableCounter {
return &settableCounter{
desc: prometheus.NewDesc(
prometheus.BuildFQName("mesos", subsystem, name),
help,
nil,
prometheus.Labels{},
),
}
}
func gauge(subsystem, name, help string, labels ...string) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "mesos",
Subsystem: subsystem,
Name: name,
Help: help,
}, labels)
}
func counter(subsystem, name, help string, labels ...string) *settableCounterVec {
desc := prometheus.NewDesc(
prometheus.BuildFQName("mesos", subsystem, name),
help,
labels,
prometheus.Labels{},
)
return &settableCounterVec{
desc: desc,
values: nil,
}
}
type authInfo struct {
username string
password string
loginURL string
token string
tokenExpire int64
signingKey []byte
strictMode bool
privateKey string
skipSSLVerify bool
}
type httpClient struct {
http.Client
url string
auth authInfo
userAgent string
}
type metricCollector struct {
*httpClient
metrics map[prometheus.Collector]func(metricMap, prometheus.Collector) error
}
func newMetricCollector(httpClient *httpClient, metrics map[prometheus.Collector]func(metricMap, prometheus.Collector) error) prometheus.Collector {
return &metricCollector{httpClient, metrics}
}
func signingToken(httpClient *httpClient) string {
signKey, err := jwt.ParseRSAPrivateKeyFromPEM(httpClient.auth.signingKey)
if err != nil {
log.WithField("error", err).Error("Error parsing privateKey")
}
expireToken := time.Now().Add(time.Hour * 1).Unix()
httpClient.auth.tokenExpire = expireToken
// Create the token
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
"uid": httpClient.auth.username,
"exp": expireToken,
})
log.WithFields(log.Fields{
"uid": httpClient.auth.username,
"expires": expireToken,
}).Debug("creating token")
// Sign and get the complete encoded token as a string
tokenString, err := token.SignedString(signKey)
if err != nil {
log.WithField("error", err).Error("Error creating login token")
return ""
}
return tokenString
}
func authToken(httpClient *httpClient) string {
currentTime := time.Now().Unix()
if currentTime > httpClient.auth.tokenExpire {
url := httpClient.auth.loginURL
signingToken := signingToken(httpClient)
body, err := json.Marshal(&tokenRequest{UID: httpClient.auth.username, Token: signingToken})
if err != nil {
log.WithField("error", err).Error("Error creating JSON request")
return ""
}
buffer := bytes.NewBuffer(body)
req, err := http.NewRequest("POST", url, buffer)
if err != nil {
log.WithFields(log.Fields{
"url": url,
"error": err,
}).Error("Error creating HTTP request")
return ""
}
req.Header.Add("User-Agent", httpClient.userAgent)
req.Header.Add("Content-Type", "application/json")
res, err := httpClient.Do(req)
if err != nil {
log.WithFields(log.Fields{
"url": url,
"error": err,
}).Error("Error fetching URL")
errorCounter.Inc()
return ""
}
defer res.Body.Close()
var token tokenResponse
if err := json.NewDecoder(res.Body).Decode(&token); err != nil {
log.WithFields(log.Fields{
"url": url,
"error": err,
}).Error("Error decoding response body")
errorCounter.Inc()
return ""
}
httpClient.auth.token = fmt.Sprintf("token=%s", token.Token)
}
return httpClient.auth.token
}
func (httpClient *httpClient) fetchAndDecode(endpoint string, target interface{}) bool {
url := strings.TrimSuffix(httpClient.url, "/") + endpoint
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.WithFields(log.Fields{
"url": url,
"error": err,
}).Error("Error creating HTTP request")
return false
}
req.Header.Add("User-Agent", httpClient.userAgent)
if httpClient.auth.username != "" && httpClient.auth.password != "" {
req.SetBasicAuth(httpClient.auth.username, httpClient.auth.password)
}
if httpClient.auth.strictMode {
req.Header.Add("Authorization", authToken(httpClient))
}
log.WithField("url", url).Debug("fetching URL")
res, err := httpClient.Do(req)
if err != nil {
log.WithFields(log.Fields{
"url": url,
"error": err,
}).Error("Error fetching URL")
errorCounter.Inc()
return false
}
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&target); err != nil {
log.WithFields(log.Fields{
"url": url,
"error": err,
}).Error("Error decoding response body")
errorCounter.Inc()
return false
}
return true
}
func (c *metricCollector) Collect(ch chan<- prometheus.Metric) {
var m metricMap
log.WithField("url", "/metrics/snapshot").Debug("fetching URL")
c.fetchAndDecode("/metrics/snapshot", &m)
for cm, f := range c.metrics {
if err := f(m, cm); err != nil {
ch := make(chan *prometheus.Desc, 1)
log.WithFields(log.Fields{
"metric": <-ch,
"error": err,
}).Error("Error extracting metric")
errorCounter.Inc()
continue
}
cm.Collect(ch)
}
}
func (c *metricCollector) Describe(ch chan<- *prometheus.Desc) {
for m := range c.metrics {
m.Describe(ch)
}
}
var invalidLabelNameCharRE = regexp.MustCompile("(^[^a-zA-Z_])|([^a-zA-Z0-9_])")
// Sanitize label names according to https://prometheus.io/docs/concepts/data_model/
func normaliseLabel(label string) string {
return invalidLabelNameCharRE.ReplaceAllString(label, "_")
}
func normaliseLabelList(labelList []string) []string {
normalisedLabelList := []string{}
for _, label := range labelList {
normalisedLabelList = append(normalisedLabelList, normaliseLabel(label))
}
return normalisedLabelList
}
func stringInSlice(string string, slice []string) bool {
for _, elem := range slice {
if string == elem {
return true
}
}
return false
}
func getLabelValuesFromMap(labels prometheus.Labels, orderedLabelKeys []string) []string {
labelValues := []string{}
for _, label := range orderedLabelKeys {
labelValues = append(labelValues, labels[label])
}
return labelValues
}
var (
text = regexp.MustCompile("^[-[:word:]/.]*$")
errDropAttribute = errors.New("value neither scalar nor text")
)
// attributeString converts a text attribute in json.RawMessage to string.
// see http://mesos.apache.org/documentation/latest/attributes-resources/
// for more information. note that scalar matches text for this purpose.
// attributeString returns string or errDropAttribute.
func attributeString(attribute json.RawMessage) (string, error) {
if value := strings.Trim(string(attribute), `"`); text.MatchString(value) {
return value, nil
}
return "", errDropAttribute
}