-
Notifications
You must be signed in to change notification settings - Fork 3
/
checks.go
81 lines (73 loc) · 1.8 KB
/
checks.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
package collector
import (
"strconv"
"sync"
"github.com/DazWilkin/updown-exporter/updown"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
)
// ChecksCollector is a type that represents updown Checks
type ChecksCollector struct {
System System
Client *updown.Client
Log logr.Logger
Enabled *prometheus.Desc
}
// NewChecksCollector is a function that returns a new ChecksCollector
func NewChecksCollector(s System, client *updown.Client, log logr.Logger) *ChecksCollector {
subsystem := "checks"
return &ChecksCollector{
System: s,
Client: client,
Log: log,
Enabled: prometheus.NewDesc(
prometheus.BuildFQName(s.Namespace, subsystem, "enabled"),
"status of check (enabled=1)",
[]string{
"token",
"url",
"status",
"ssl_valid",
},
nil,
),
}
}
// Collect implements Prometheus' Collector interface and is used to collect metrics
func (c *ChecksCollector) Collect(ch chan<- prometheus.Metric) {
log := c.Log.WithName("Collect")
checks, err := c.Client.GetChecks()
if err != nil {
log.Info("Unable to get Checks")
return
}
var wg sync.WaitGroup
for _, check := range checks {
wg.Add(1)
go func(check updown.Check) {
defer wg.Done()
ch <- prometheus.MustNewConstMetric(
c.Enabled,
prometheus.CounterValue,
func(enabled bool) (result float64) {
if enabled {
result = 1.0
}
return result
}(check.Enabled),
[]string{
check.Token,
check.URL,
strconv.FormatUint(uint64(check.LastStatus), 10),
strconv.FormatBool(check.SSL.Valid),
}...,
)
}(check)
}
wg.Wait()
}
// Describe implements Prometheus' Collector interface is used to describe metrics
func (c *ChecksCollector) Describe(ch chan<- *prometheus.Desc) {
// log := c.Log.WithName("Describe")
ch <- c.Enabled
}