-
Notifications
You must be signed in to change notification settings - Fork 2
/
collector.go
153 lines (120 loc) · 3.17 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
package main
import (
"context"
"fmt"
"log"
"regexp"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/prometheus/client_golang/prometheus"
)
type collector struct {
client *sqs.SQS
mutex *sync.Mutex
queues []*queue
attributes []*string
metrics []*queueMetric
totalScrapes prometheus.Counter
}
func newCollector(ctx context.Context, updateInterval time.Duration, prefix *string, regex *regexp.Regexp, tagsAsLables bool) *collector {
sess := session.Must(session.NewSession())
c := &collector{
client: sqs.New(sess),
mutex: &sync.Mutex{},
metrics: metrics,
attributes: buildAttributes(),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "total_scrapes"),
Help: "Current total AWS SQS scrapes.",
}),
}
go c.queueListUpdater(ctx, updateInterval, queuePrefix, regex, tagsAsLables)
return c
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range c.metrics {
ch <- generateDesc(metric.name, metric.help, nil, nil)
}
ch <- c.totalScrapes.Desc()
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
c.totalScrapes.Inc()
defer func() {
ch <- c.totalScrapes
}()
log.Printf("Collecting metrics for %d queues", len(c.queues))
for _, q := range c.queues {
lables := []string{"name"}
values := []string{q.name}
for key, value := range q.tags {
lables = append(lables, fmt.Sprintf("tag_%s", key))
values = append(values, *value)
}
if err := q.getQueueAttributes(c.client, c.attributes); err != nil {
log.Printf("Failed to get queue attributes: %v", err)
continue
}
for _, m := range c.metrics {
ch <- prometheus.MustNewConstMetric(
generateDesc(m.name, m.help, lables, nil),
m.Type,
q.getAttributeValue(m.attribute),
values...,
)
}
}
}
func (c *collector) queueListUpdater(ctx context.Context, updateInverval time.Duration, prefix *string, regex *regexp.Regexp, tagsAsLables bool) {
log.Printf("Start queue list updater")
queueListInput := &sqs.ListQueuesInput{}
if len(*prefix) > 0 {
queueListInput.QueueNamePrefix = prefix
}
f := func() error {
result, err := c.client.ListQueues(queueListInput)
if err != nil {
return err
}
var tmpQueues []*queue
for _, q := range result.QueueUrls {
queueName := getQueueName(*q)
if !regex.MatchString(queueName) {
continue
}
tmpQ := &queue{name: queueName, url: q}
if tagsAsLables {
tagsInput := &sqs.ListQueueTagsInput{
QueueUrl: q,
}
tagsOutput, err := c.client.ListQueueTags(tagsInput)
if err != nil {
return err
}
tmpQ.tags = tagsOutput.Tags
}
tmpQueues = append(tmpQueues, tmpQ)
}
log.Printf("Found %d queues", len(tmpQueues))
c.mutex.Lock()
c.queues = tmpQueues
c.mutex.Unlock()
return nil
}
if err := f(); err != nil {
log.Fatalf("Failed to get queue list: %v", err)
}
innerTicker := time.NewTicker(updateInverval)
defer innerTicker.Stop()
for {
select {
case <-ctx.Done():
return
case <-innerTicker.C:
if err := f(); err != nil {
log.Fatalf("Failed to get queue list: %v", err)
}
}
}
}