11package cloudwatch
22
33import (
4+ "fmt"
45 "sync"
5- 66 "time"
77
8- "fmt"
9- 108 "github.com/aws/aws-sdk-go/aws"
119 "github.com/aws/aws-sdk-go/service/cloudwatch"
1210 "github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
11+ 1312 "github.com/go-kit/kit/log"
1413 "github.com/go-kit/kit/metrics"
1514 "github.com/go-kit/kit/metrics/generic"
@@ -24,54 +23,54 @@ type CloudWatch struct {
2423 mtx sync.RWMutex
2524 namespace string
2625 svc cloudwatchiface.CloudWatchAPI
27- counters map [string ]* Counter
28- gauges map [string ]* Gauge
29- histograms map [string ]* Histogram
26+ counters map [string ]* counter
27+ gauges map [string ]* gauge
28+ histograms map [string ]* histogram
3029 logger log.Logger
3130}
3231
3332// New returns a CloudWatch object that may be used to create metrics. Namespace is
3433// applied to all created metrics and maps to the CloudWatch namespace.
3534// Callers must ensure that regular calls to Send are performed, either manually or with one of the helper methods.
36- func New (namespace string , logger log. Logger , svc cloudwatchiface. CloudWatchAPI ) * CloudWatch {
35+ func New (namespace string , svc cloudwatchiface. CloudWatchAPI , logger log. Logger ) * CloudWatch {
3736 return & CloudWatch {
3837 namespace : namespace ,
3938 svc : svc ,
40- counters : map [string ]* Counter {},
41- gauges : map [string ]* Gauge {},
42- histograms : map [string ]* Histogram {},
39+ counters : map [string ]* counter {},
40+ gauges : map [string ]* gauge {},
41+ histograms : map [string ]* histogram {},
4342 logger : logger ,
4443 }
4544}
4645
4746// NewCounter returns a counter. Observations are aggregated and emitted once
4847// per write invocation.
49- func (cw * CloudWatch ) NewCounter (name string ) * Counter {
50- c := NewCounter (name )
48+ func (cw * CloudWatch ) NewCounter (name string ) metrics.Counter {
5149 cw .mtx .Lock ()
50+ defer cw .mtx .Unlock ()
51+ c := & counter {c : generic .NewCounter (name )}
5252 cw .counters [name ] = c
53- cw .mtx .Unlock ()
5453 return c
5554}
5655
5756// NewGauge returns a gauge. Observations are aggregated and emitted once per
5857// write invocation.
59- func (cw * CloudWatch ) NewGauge (name string ) * Gauge {
60- g := NewGauge (name )
58+ func (cw * CloudWatch ) NewGauge (name string ) metrics.Gauge {
6159 cw .mtx .Lock ()
60+ defer cw .mtx .Unlock ()
61+ g := & gauge {g : generic .NewGauge (name )}
6262 cw .gauges [name ] = g
63- cw .mtx .Unlock ()
6463 return g
6564}
6665
6766// NewHistogram returns a histogram. Observations are aggregated and emitted as
6867// per-quantile gauges, once per write invocation. 50 is a good default value
6968// for buckets.
70- func (cw * CloudWatch ) NewHistogram (name string , buckets int ) * Histogram {
71- h := NewHistogram (name , buckets )
69+ func (cw * CloudWatch ) NewHistogram (name string , buckets int ) metrics.Histogram {
7270 cw .mtx .Lock ()
71+ defer cw .mtx .Unlock ()
72+ h := & histogram {h : generic .NewHistogram (name , buckets )}
7373 cw .histograms [name ] = h
74- cw .mtx .Unlock ()
7574 return h
7675}
7776
@@ -87,14 +86,14 @@ func (cw *CloudWatch) WriteLoop(c <-chan time.Time) {
8786 }
8887}
8988
90- // Send will fire an api request to CloudWatch with the latest stats for
91- // all metrics.
89+ // Send will fire an API request to CloudWatch with the latest stats for
90+ // all metrics. It is preferred that the WriteLoop method is used.
9291func (cw * CloudWatch ) Send () error {
9392 cw .mtx .RLock ()
9493 defer cw .mtx .RUnlock ()
9594 now := time .Now ()
9695
97- datums := []* cloudwatch.MetricDatum {}
96+ var datums []* cloudwatch.MetricDatum
9897
9998 for name , c := range cw .counters {
10099 datums = append (datums , & cloudwatch.MetricDatum {
@@ -140,77 +139,56 @@ func (cw *CloudWatch) Send() error {
140139 return err
141140}
142141
143- // Counter is a CloudWatch counter metric.
144- type Counter struct {
142+ // counter is a CloudWatch counter metric.
143+ type counter struct {
145144 c * generic.Counter
146145}
147146
148- // NewCounter returns a new usable counter metric.
149- func NewCounter (name string ) * Counter {
150- return & Counter {
151- c : generic .NewCounter (name ),
152- }
153- }
154- 155147// With implements counter
156- func (c * Counter ) With (labelValues ... string ) metrics.Counter {
148+ func (c * counter ) With (labelValues ... string ) metrics.Counter {
157149 c .c = c .c .With (labelValues ... ).(* generic.Counter )
158150 return c
159151}
160152
161153// Add implements counter.
162- func (c * Counter ) Add (delta float64 ) {
154+ func (c * counter ) Add (delta float64 ) {
163155 c .c .Add (delta )
164156}
165157
166- // Gauge is a CloudWatch gauge metric.
167- type Gauge struct {
158+ // gauge is a CloudWatch gauge metric.
159+ type gauge struct {
168160 g * generic.Gauge
169161}
170162
171- // NewGauge returns a new usable gauge metric
172- func NewGauge (name string ) * Gauge {
173- return & Gauge {
174- g : generic .NewGauge (name ),
175- }
176- }
177- 178163// With implements gauge
179- func (g * Gauge ) With (labelValues ... string ) metrics.Gauge {
164+ func (g * gauge ) With (labelValues ... string ) metrics.Gauge {
180165 g .g = g .g .With (labelValues ... ).(* generic.Gauge )
181166 return g
182167}
183168
184169// Set implements gauge
185- func (g * Gauge ) Set (value float64 ) {
170+ func (g * gauge ) Set (value float64 ) {
186171 g .g .Set (value )
187172}
188173
189174// Add implements gauge
190- func (g * Gauge ) Add (delta float64 ) {
175+ func (g * gauge ) Add (delta float64 ) {
191176 g .g .Add (delta )
192177}
193178
194- // Histogram is a CloudWatch histogram metric
195- type Histogram struct {
179+ // histogram is a CloudWatch histogram metric
180+ type histogram struct {
196181 h * generic.Histogram
197182}
198183
199- // NewHistogram returns a new usable histogram metric
200- func NewHistogram (name string , buckets int ) * Histogram {
201- return & Histogram {
202- h : generic .NewHistogram (name , buckets ),
203- }
204- }
205- 206184// With implements histogram
207- func (h * Histogram ) With (labelValues ... string ) metrics.Histogram {
185+ func (h * histogram ) With (labelValues ... string ) metrics.Histogram {
208186 h .h = h .h .With (labelValues ... ).(* generic.Histogram )
209187 return h
210188}
211189
212190// Observe implements histogram
213- func (h * Histogram ) Observe (value float64 ) {
191+ func (h * histogram ) Observe (value float64 ) {
214192 h .h .Observe (value )
215193}
216194
0 commit comments