-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
168 lines (140 loc) · 5.06 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
)
// Default to checking queues every 30 seconds
const defaultMonitorInterval = 30 * time.Second
// SQSClientInterface defines the interface for SQS operations we use
type SQSClientInterface interface {
GetQueueAttributes(ctx context.Context, params *sqs.GetQueueAttributesInput, optFns ...func(*sqs.Options)) (*sqs.GetQueueAttributesOutput, error)
}
var svc SQSClientInterface
var monitorInterval time.Duration
var labelNames = []string{"queue"}
var promMessages = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "sqs_approximatenumberofmessages",
Help: "The approximate number of messages available for retrieval from the queue.",
},
labelNames)
var promMessagesDelayed = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "sqs_approximatenumberofmessagesdelayed",
Help: "The approximate number of messages in the queue that are delayed and not available for reading immediately.",
}, labelNames)
var promMessagesNotVisible = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "sqs_approximatenumberofmessagesnotvisible",
Help: "The approximate number of messages that are in flight.",
}, labelNames)
// Struct to hold queue URL and name, as these aren't included in SQS response
type queueResult struct {
QueueURL string
QueueName string
QueueResults *sqs.GetQueueAttributesOutput
}
func getSqsClient() SQSClientInterface {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Error().Str("errorMessage", err.Error()).Msg("error loading AWS config")
os.Exit(1)
}
return sqs.NewFromConfig(cfg)
}
func getMonitorInterval() time.Duration {
monitorIntervalStr, varSet := os.LookupEnv("SQS_MONITOR_INTERVAL_SECONDS")
if !varSet || monitorIntervalStr == "" {
log.Warn().Msg(fmt.Sprintf("Monitor interval not set, defaulting to %v", defaultMonitorInterval))
return defaultMonitorInterval
}
monitorInterval, err := strconv.Atoi(monitorIntervalStr)
if err != nil {
log.Warn().Str("errorMessage", err.Error()).Msg("Invalid value for SQS_MONITOR_INTERVAL, using default")
return defaultMonitorInterval
}
return time.Duration(monitorInterval) * time.Second
}
func monitorQueue(queueURL string, c chan queueResult) {
queueComponents := strings.Split(queueURL, "/")
queueName := queueComponents[len(queueComponents)-1]
params := &sqs.GetQueueAttributesInput{
QueueUrl: &queueURL,
AttributeNames: []types.QueueAttributeName{
types.QueueAttributeNameApproximateNumberOfMessages,
types.QueueAttributeNameApproximateNumberOfMessagesDelayed,
types.QueueAttributeNameApproximateNumberOfMessagesNotVisible,
},
}
resp, err := svc.GetQueueAttributes(context.TODO(), params)
if err != nil {
log.Error().Str("errorMessage", err.Error()).Msg("error checking queue")
c <- queueResult{queueURL, queueName, nil} // Send a result with nil QueueResults to indicate error
return
}
c <- queueResult{queueURL, queueName, resp}
}
func monitorQueues(queueUrls []string) {
c := make(chan queueResult)
for {
for _, queueURL := range queueUrls {
go monitorQueue(queueURL, c)
}
for i := 0; i < len(queueUrls); i++ {
queueResult := <-c
if queueResult.QueueResults == nil {
continue // Skip this queue if there was an error
}
for attrib := range queueResult.QueueResults.Attributes {
prop := queueResult.QueueResults.Attributes[attrib]
nMessages, _ := strconv.ParseFloat(prop, 64)
switch attrib {
case "ApproximateNumberOfMessages":
promMessages.WithLabelValues(queueResult.QueueName).Set(nMessages)
case "ApproximateNumberOfMessagesDelayed":
promMessagesDelayed.WithLabelValues(queueResult.QueueName).Set(nMessages)
case "ApproximateNumberOfMessagesNotVisible":
promMessagesNotVisible.WithLabelValues(queueResult.QueueName).Set(nMessages)
default:
log.Warn().Msg(fmt.Sprintf("unknown attribute %v", attrib))
}
}
}
time.Sleep(monitorInterval)
}
}
// Return an empty 200 response for healthchecks
func healthcheck(w http.ResponseWriter, r *http.Request) {
}
func main() {
queueVar, varSet := os.LookupEnv("SQS_QUEUE_URLS")
if !varSet || queueVar == "" {
log.Error().Msg("No URLs supplied")
os.Exit(1)
}
queueUrls := strings.Split(queueVar, ",")
port, portSet := os.LookupEnv("PORT")
if !portSet || port == "" {
port = "8080"
}
monitorInterval = getMonitorInterval()
log.Info().Dur("interval", monitorInterval).Strs("queueUrls", queueUrls).Str("port", port).Msg("Starting queue monitors")
svc = getSqsClient()
go monitorQueues(queueUrls)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", healthcheck)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Error().Str("errorMessage", err.Error()).Msg("Could not start http listener")
os.Exit(1)
}
}