forked from louisfelix/alertmanager-silences-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.go
106 lines (85 loc) · 3.01 KB
/
collector.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"regexp"
"time"
"github.com/prometheus/common/log"
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/client_golang/prometheus"
)
var (
invalidMetricChars = regexp.MustCompile("[^a-zA-Z0-9:_]")
)
type Silence struct {
Gettable *models.GettableSilence
Labels map[string]string
Status string
}
func (s *Silence) Decorate() {
s.Labels = map[string]string{}
s.Labels["id"] = *s.Gettable.ID
s.Labels["comment"] = *s.Gettable.Comment
s.Labels["createdBy"] = *s.Gettable.CreatedBy
s.Labels["status"] = *s.Gettable.Status.State
for _, m := range s.Gettable.Matchers {
s.Labels["matcher_"+*m.Name] = *m.Value
}
s.Status = *s.Gettable.Status.State
}
// AlertmanagerSilencesCollector collects Alertmanager Silence metrics
type AlertmanagerSilencesCollector struct {
Config *Config
AlertmanagerClient AlertmanagerAPI
}
// NewAlertmanagerSilencesCollector returns the collector
func NewAlertmanagerSilencesCollector(conf *Config, client AlertmanagerAPI) *AlertmanagerSilencesCollector {
return &AlertmanagerSilencesCollector{Config: conf, AlertmanagerClient: client}
}
// Describe to satisfy the collector interface.
func (c *AlertmanagerSilencesCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc("AlertmanagerSilencesCollector", "dummy", nil, nil)
}
// Collect metrics from Alertmanager
func (c *AlertmanagerSilencesCollector) Collect(ch chan<- prometheus.Metric) {
silences, err := c.AlertmanagerClient.ListSilences()
if err != nil {
log.Errorf("unable to list silences: %s", err.Error())
ch <- prometheus.NewInvalidMetric(amErrorDesc, err)
return
}
for _, s := range silences {
silence := &Silence{Gettable: s}
silence.Decorate()
c.extractMetric(ch, silence)
}
}
func (c *AlertmanagerSilencesCollector) extractMetric(ch chan<- prometheus.Metric, silence *Silence) {
startTime, err := time.Parse(time.RFC3339, silence.Gettable.StartsAt.String())
if err != nil {
log.Errorf("cannot parse start time of silence with ID '%s'\n", silence.Labels["id"])
return
}
endTime, err := time.Parse(time.RFC3339, silence.Gettable.EndsAt.String())
if err != nil {
log.Errorf("cannot parse end time of silence with ID '%s'\n", silence.Labels["id"])
return
}
state := 0
if silence.Status == "active" {
state = 1
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("alertmanager_silence_info", "Alertmanager silence info metric", nil, silence.Labels),
prometheus.GaugeValue,
float64(state),
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("alertmanager_silence_start_seconds", "Alertmanager silence start time, elapsed seconds since epoch", nil, map[string]string{"id": silence.Labels["id"]}),
prometheus.GaugeValue,
float64(startTime.Unix()),
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("alertmanager_silence_end_seconds", "Alertmanager silence end time, elapsed seconds since epoch", nil, map[string]string{"id": silence.Labels["id"]}),
prometheus.GaugeValue,
float64(endTime.Unix()),
)
}