-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
70 lines (65 loc) · 2.27 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"net/http"
)
var (
resolveErrorsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "porkbun_resolve_errors_total",
Help: "The total number of errors trying to resolve the current ip address",
},
[]string{"record_type"},
)
updateErrorsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "porkbun_update_errors_total",
Help: "The total number of errors found",
},
[]string{"host", "domain"},
)
credentialsErrorsTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "porkbun_credentials_errors_total",
Help: "The total number of credentials errors",
})
connectionErrorsTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "porkbun_connection_errors_total",
Help: "The total number of connection errors (while connecting to porkbun)",
})
updateSuccessTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "porkbun_update_success_total",
Help: "The total number of successful updates",
},
[]string{"host", "domain", "record_type"},
)
)
// initMetrics initializes the metrics with the records provided in the configuration
// We want to have 0 as a baseline for all possible labels instead of non-existing metrics
func initMetrics(records []Record) {
for _, record := range records {
updateSuccessTotal.WithLabelValues(record.Host, record.Domain, "A").Add(0)
updateSuccessTotal.WithLabelValues(record.Host, record.Domain, "AAAA").Add(0)
updateErrorsTotal.WithLabelValues(record.Host, record.Domain).Add(0)
resolveErrorsTotal.WithLabelValues("A").Add(0)
resolveErrorsTotal.WithLabelValues("AAAA").Add(0)
}
}
func healthcheckHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, "OK")
if err != nil {
log.Errorf("error responding to request %v", err)
}
}
func startMetricsServer(port int) error {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/health", healthcheckHandler)
log.Printf("Starting metrics server on port %d", port)
return http.ListenAndServe(fmt.Sprintf(":%d", port), mux)
}