-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsns_metrics.go
94 lines (85 loc) · 2.34 KB
/
sns_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
package window
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
type (
TopicStats struct {
PublishedPerSecond float64
PublishSizeAvgBytes int64
DeliveredPerSecond float64
FailedPerSecond float64
}
snsmetric struct {
name *string
statistics []*string
unit *string
processor func(stats *TopicStats, point *cloudwatch.Datapoint)
}
)
var (
SNSTopicMetrics = []*snsmetric{
{
name: aws.String("NumberOfMessagesPublished"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *TopicStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.PublishedPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("PublishSize"),
statistics: []*string{aws.String("Average")},
unit: aws.String("Bytes"),
processor: func(stats *TopicStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.PublishSizeAvgBytes = int64(*point.Average)
}
},
},
{
name: aws.String("NumberOfNotificationsDelivered"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *TopicStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.DeliveredPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("NumberOfNotificationsFailed"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *TopicStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.FailedPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
}
)
func (m *snsmetric) RunFor(t *SNSTopic) 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/SNS"),
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("TopicName"),
Value: aws.String(t.TopicName),
},
},
MetricName: m.name,
Statistics: m.statistics,
Unit: m.unit, // fuck this in teh face
})
if err == nil && len(resp.Datapoints) > 0 {
m.processor(t.Stats, resp.Datapoints[0])
}
return err
}