-
Notifications
You must be signed in to change notification settings - Fork 4
/
metrics.go
54 lines (45 loc) · 1.26 KB
/
metrics.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
package main
import (
"fmt"
"io"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type metrics struct {
service service
}
var validationSuccessfulGauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: "trivrost",
Name: "validation_ok",
Help: "1 if validation ok, 0 otherwise",
},
)
var promHttpHandler = promhttp.Handler()
func registerMetrics() {
if err := prometheus.Register(validationSuccessfulGauge); err != nil {
panic(fmt.Errorf("registering validationSuccessfulGauge failed: %w", err))
}
}
func (s metrics) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
w.Header()["allow"] = []string{"GET"}
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
configUrl := getConfigUrl(req, s.service.flags)
if configUrl == "" {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Required query parameter '"+configUrlParameterName+"' is missing.\n")
return
}
reps := validateDeploymentConfig(configUrl, s.service.flags.SkipUrlCheck, s.service.flags.SkipJarChek)
if reps.HaveError() {
logReports(reps, true)
validationSuccessfulGauge.Set(0)
} else {
validationSuccessfulGauge.Set(1)
}
promHttpHandler.ServeHTTP(w, req)
}