/
reporter.go
187 lines (152 loc) · 3.66 KB
/
reporter.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package cloudwatch
import (
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/efritz/glock"
"github.com/efritz/imperial/base"
)
type Reporter struct {
logger base.Logger
clock glock.Clock
configs []base.ConfigFunc
api cloudwatchiface.CloudWatchAPI
batchSize int
bufferSize int
tickDuration time.Duration
namespaces map[string]chan *cloudwatch.MetricDatum
mutex *sync.RWMutex
once *sync.Once
wg *sync.WaitGroup
}
var _ base.SimpleReporter = &Reporter{}
func NewReporter(namespace string, configs ...ConfigFunc) *Reporter {
config := newConfig(namespace)
for _, f := range configs {
f(config)
}
return &Reporter{
logger: config.logger,
clock: config.clock,
configs: config.configs,
api: makeAPI(config),
batchSize: config.batchSize,
bufferSize: config.bufferSize,
tickDuration: config.tickDuration,
namespaces: map[string]chan *cloudwatch.MetricDatum{},
mutex: &sync.RWMutex{},
once: &sync.Once{},
wg: &sync.WaitGroup{},
}
}
func (r *Reporter) Report(name string, value float64, configs ...base.ConfigFunc) {
var (
options = base.ApplyConfigs(r.configs, configs)
namespace = options.Namespace
datum = &cloudwatch.MetricDatum{
MetricName: aws.String(name),
Timestamp: aws.Time(r.clock.Now()),
Value: aws.Float64(value),
Unit: aws.String(string(options.Unit)),
Dimensions: serializeDimensions(options.Attributes),
}
)
r.ensurePublisher(namespace)
for {
select {
case r.namespaces[namespace] <- datum:
return
default:
}
select {
case <-r.namespaces[namespace]:
r.logger.Printf(
"Cloudwatch buffer for namespace %s full, dropping oldest datum",
namespace,
)
default:
}
}
}
func (r *Reporter) Shutdown() {
r.once.Do(func() {
r.mutex.Lock()
defer r.mutex.Unlock()
for _, ch := range r.namespaces {
close(ch)
}
})
r.wg.Wait()
}
func (r *Reporter) ensurePublisher(namespace string) {
r.mutex.RLock()
if _, ok := r.namespaces[namespace]; ok {
r.mutex.RUnlock()
return
}
r.mutex.RUnlock()
r.mutex.Lock()
defer r.mutex.Unlock()
if _, ok := r.namespaces[namespace]; ok {
return
}
ch := make(chan *cloudwatch.MetricDatum, r.bufferSize)
r.namespaces[namespace] = ch
r.wg.Add(1)
go r.publish(namespace, ch)
}
func (r *Reporter) publish(namespace string, ch <-chan *cloudwatch.MetricDatum) {
defer r.wg.Done()
var (
data = []*cloudwatch.MetricDatum{}
ticker = r.clock.NewTicker(r.tickDuration)
)
putMetricData := func() {
if len(data) == 0 {
return
}
input := &cloudwatch.PutMetricDataInput{
Namespace: aws.String(namespace),
MetricData: data,
}
if _, err := r.api.PutMetricData(input); err != nil {
r.logger.Printf(
"Failed to publish data for to Cloudwatch namespace %s (%s)",
namespace,
err.Error(),
)
}
data = data[:0]
}
loop:
for {
select {
case datum, ok := <-ch:
if !ok {
break loop
}
data = append(data, datum)
if len(data) < r.batchSize {
continue
}
case <-ticker.Chan():
}
putMetricData()
}
putMetricData()
}
func serializeDimensions(attributes map[string]string) []*cloudwatch.Dimension {
dimensions := make([]*cloudwatch.Dimension, 0, len(attributes))
for key, value := range attributes {
dimensions = append(dimensions, serializeDimension(key, value))
}
return dimensions
}
func serializeDimension(key, value string) *cloudwatch.Dimension {
return &cloudwatch.Dimension{
Name: aws.String(key),
Value: aws.String(value),
}
}