-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
80 lines (68 loc) · 2.11 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"github.com/coralogix/prometheus-alerts-readiness/internal/config"
"github.com/coralogix/prometheus-alerts-readiness/internal/responses"
"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
"log"
"net/http"
"time"
)
func main() {
c, err := config.New()
if err != nil {
log.Fatalf("Error reading Configuration: %v\n", err)
}
// initialize prometheus client
client, err := api.NewClient(api.Config{
Address: c.PrometheusEndpoint,
})
if err != nil {
log.Fatalf("Error creating Prometheus client: %v\n", err)
}
v1api := v1.NewAPI(client)
// register at live path
http.HandleFunc(c.KubernetesLivenessPath, func(writer http.ResponseWriter, request *http.Request) {
responses.Ready(writer)
})
// register at ready path
http.HandleFunc(c.KubernetesReadinessPath, func(writer http.ResponseWriter, request *http.Request) {
// query the prometheus endpoint with the query
ctx, cancel := context.WithTimeout(request.Context(), c.PrometheusApiTimeout*time.Second)
defer cancel()
alertsResult, err := v1api.Alerts(ctx)
if err != nil {
responses.NotReady(writer, err)
return
}
// note that alertsResult.Alerts only contains active alerts, not all alerts.
// but we're not interested in inactive alerts anyway.
for _, alert := range alertsResult.Alerts {
// scan until we reach the alert we're interested in
severity := string(alert.Labels["severity"])
severityIsRelevant := false
for _, configuredSeverity := range c.PrometheusAlertSeverities {
if severity == configuredSeverity {
severityIsRelevant = true
break
}
}
if !severityIsRelevant {
continue
}
if alert.State == v1.AlertStateFiring {
errMsg := fmt.Sprintf("The Prometheus alert is firing: %v", alert.Labels)
log.Println("ERROR: " + errMsg)
responses.NotReady(writer, errors.New(errMsg))
return
}
}
// if there are no issues, then report readiness
responses.Ready(writer)
})
log.Print("Starting HTTP listener...")
log.Fatal(http.ListenAndServe(":"+c.KubeProbeListenPort, nil))
}