-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqs_metrics.go
115 lines (101 loc) · 2.93 KB
/
sqs_metrics.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
package window
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
type (
QueueStats struct {
// The number of messages added to a queue.
SentPerSecond float64
// The size of messages added to a queue.
MessageSizeAvgBytes int64
// The number of messages returned by calls to the ReceiveMessage API action.
ReceivedPerSecond float64
// The number of ReceiveMessage API calls that did not return a message.
EmptyReceivesPerSecond float64
// The number of messages deleted from the queue.
DeletedPerSecond float64
}
sqsmetric struct {
name *string
statistics []*string
unit *string
processor func(*QueueStats, *cloudwatch.Datapoint)
}
)
var (
SQSQueueMetrics = []*sqsmetric{
{
name: aws.String("NumberOfMessagesSent"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *QueueStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.SentPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("SentMessageSize"),
statistics: []*string{aws.String("Average")},
unit: aws.String("Bytes"),
processor: func(stats *QueueStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.MessageSizeAvgBytes = int64(*point.Average)
}
},
},
{
name: aws.String("NumberOfMessagesReceived"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *QueueStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.ReceivedPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("NumberOfEmptyReceives"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *QueueStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.EmptyReceivesPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("NumberOfMessagesDeleted"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *QueueStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.DeletedPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
}
)
func (m *sqsmetric) RunFor(s *SQSQueue) error {
resp, err := CloudWatchClient.GetMetricStatistics(&cloudwatch.GetMetricStatisticsInput{
StartTime: aws.Time(time.Now().Add(-PeriodInMinutes * time.Minute)),
EndTime: aws.Time(time.Now()),
Period: aws.Int64(PeriodInMinutes * 60),
Namespace: aws.String("AWS/SQS"),
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("QueueName"),
Value: aws.String(s.Name),
},
},
MetricName: m.name,
Statistics: m.statistics,
Unit: m.unit, // fuck this in teh face
})
if err == nil && len(resp.Datapoints) > 0 {
m.processor(s.Stats, resp.Datapoints[0])
}
return err
}