forked from otm/cloudwatch-alarm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
collector.go
156 lines (137 loc) · 3.97 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package collector
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "cloudwatch"
subsystem = "alarm"
collector = namespace + subsystem
)
type alarmFetcher interface {
Alarms() ([]*cloudwatch.MetricAlarm, error)
TagsForAlarm(*cloudwatch.MetricAlarm) (map[string]string, error)
Tags() *[]string
}
// CloudWatchAlarms implements the Prometheus Collector interface
type CloudWatchAlarms struct {
desc *prometheus.Desc
alarmFetcher alarmFetcher
scrapeStatus *prometheus.GaugeVec
totalScrapeDuration prometheus.Summary
totalFetchedAlarms prometheus.Counter
metrics []prometheus.Metric
}
// New returns a CloudWatchAlarms instance
func New(alarmFetcher alarmFetcher) *CloudWatchAlarms {
return &CloudWatchAlarms{
totalFetchedAlarms: prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: collector,
Name: "total_fetched_cloudwatchalarms",
Help: "Alarms fetched from CloudWatch.",
},
),
alarmFetcher: alarmFetcher,
totalScrapeDuration: prometheus.NewSummary(
prometheus.SummaryOpts{
Namespace: collector,
Name: "total_scrape_duration",
Help: "The time the scrape of CloudWatch alarms took in seconds.",
},
),
scrapeStatus: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: collector,
Name: "up",
Help: "Status of the current scrape",
},
[]string{"collector"},
),
}
}
// Collect implements the Prometheus Collecter interface
func (c *CloudWatchAlarms) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
var alarmCount int
alarms, err := c.alarmFetcher.Alarms()
if err != nil {
c.scrapeStatus.WithLabelValues(collector).Set(0)
c.scrapeStatus.Collect(ch)
elapsed := time.Since(start)
c.totalScrapeDuration.Observe(float64(elapsed.Seconds()))
ch <- c.totalScrapeDuration
log.Printf("unable to fetch alarms: %v", err)
return
}
c.metrics = nil
scrapeStatus := true
for _, alarm := range alarms {
alarmCount++
value, err := translateCloudWatchState(*alarm.StateValue)
if err != nil {
scrapeStatus = false
continue
}
metricNames := []string{"name", "namespace", "metric"}
metricValues := []string{*alarm.AlarmName, *alarm.Namespace, *alarm.MetricName}
tags, err := c.alarmFetcher.TagsForAlarm(alarm)
if err != nil {
log.Printf("unable to fetch tags for %s: %v", *alarm.AlarmName, err)
}
for _, key := range *c.alarmFetcher.Tags() {
metricNames = append(metricNames, key)
if v, ok := tags[key]; ok {
metricValues = append(metricValues, v)
} else {
metricValues = append(metricValues, "")
}
}
metric := prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "state"),
"Cloudwatch alarm state: 0=OK, 1=Alarm, -1=InsufficientData, -1000=InternalCollectorError",
metricNames,
nil,
),
prometheus.GaugeValue,
value,
metricValues...,
)
ch <- metric
c.metrics = append(c.metrics, metric)
}
c.scrapeStatus.WithLabelValues(collector).Set(1)
if !scrapeStatus {
c.scrapeStatus.WithLabelValues(collector).Set(0)
}
c.scrapeStatus.Collect(ch)
c.totalFetchedAlarms.Add(float64(alarmCount))
elapsed := time.Since(start)
c.totalScrapeDuration.Observe(float64(elapsed.Seconds()))
ch <- c.totalScrapeDuration
}
// Describe implements the Prometheus Describer interface
func (c *CloudWatchAlarms) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range c.metrics {
ch <- metric.Desc()
}
c.scrapeStatus.Describe(ch)
c.totalScrapeDuration.Describe(ch)
c.totalFetchedAlarms.Describe(ch)
}
func translateCloudWatchState(state string) (float64, error) {
switch state {
case cloudwatch.StateValueOk:
return float64(0), nil
case cloudwatch.StateValueAlarm:
return float64(1), nil
case cloudwatch.StateValueInsufficientData:
return float64(-1), nil
default:
return 0, fmt.Errorf("unknown cloudwatch state: %s", state)
}
}