-
Notifications
You must be signed in to change notification settings - Fork 2
/
domains.go
63 lines (53 loc) 路 1.63 KB
/
domains.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
package domain
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var lastTickMetric = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "domain_watch",
Name: "last_fetch_seconds",
Help: "Unix timestamp for when the last fetch occurred.",
})
var domainCountMetric = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "domain_watch",
Name: "domains",
Help: "Number of domains that are being watched.",
})
var successMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "domain_watch",
Name: "update_success",
Help: "Whether the last fetch succeeded.",
}, []string{"domain"})
var expirationMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "domain_watch",
Name: "expires_timestamp_seconds",
Help: "Unix timestamp for when the domain will expire.",
}, []string{"domain"})
type Domains struct {
Sleep time.Duration
Domains []*Domain
}
func (d *Domains) Add(domain Domain) {
domainCountMetric.Add(1)
d.Domains = append(d.Domains, &domain)
}
func (d Domains) Tick() {
defer func() {
lastTickMetric.SetToCurrentTime()
}()
for i, domain := range d.Domains {
if i != 0 {
time.Sleep(d.Sleep)
}
if err := domain.Run(); err == nil {
successMetric.With(prometheus.Labels{"domain": domain.Name}).Set(1)
} else {
successMetric.With(prometheus.Labels{"domain": domain.Name}).Set(0)
domain.Log().WithError(err).Error("domain update failed")
}
if domain.ExpiresAt.Unix() > 0 {
expirationMetric.With(prometheus.Labels{"domain": domain.Name}).Set(float64(domain.ExpiresAt.Unix()))
}
}
}