Codebase list golang-github-go-kit-kit / ead1f1c
Fixes from PR. Cameron Stitt 6 years ago
2 changed file(s) with 40 addition(s) and 63 deletion(s). Raw diff Collapse all Expand all
00 package cloudwatch
11
22 import (
3 "fmt"
34 "sync"
4
55 "time"
6
7 "fmt"
86
97 "github.com/aws/aws-sdk-go/aws"
108 "github.com/aws/aws-sdk-go/service/cloudwatch"
119 "github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
10
1211 "github.com/go-kit/kit/log"
1312 "github.com/go-kit/kit/metrics"
1413 "github.com/go-kit/kit/metrics/generic"
2322 mtx sync.RWMutex
2423 namespace string
2524 svc cloudwatchiface.CloudWatchAPI
26 counters map[string]*Counter
27 gauges map[string]*Gauge
28 histograms map[string]*Histogram
25 counters map[string]*counter
26 gauges map[string]*gauge
27 histograms map[string]*histogram
2928 logger log.Logger
3029 }
3130
3231 // New returns a CloudWatch object that may be used to create metrics. Namespace is
3332 // applied to all created metrics and maps to the CloudWatch namespace.
3433 // Callers must ensure that regular calls to Send are performed, either manually or with one of the helper methods.
35 func New(namespace string, logger log.Logger, svc cloudwatchiface.CloudWatchAPI) *CloudWatch {
34 func New(namespace string, svc cloudwatchiface.CloudWatchAPI, logger log.Logger) *CloudWatch {
3635 return &CloudWatch{
3736 namespace: namespace,
3837 svc: svc,
39 counters: map[string]*Counter{},
40 gauges: map[string]*Gauge{},
41 histograms: map[string]*Histogram{},
38 counters: map[string]*counter{},
39 gauges: map[string]*gauge{},
40 histograms: map[string]*histogram{},
4241 logger: logger,
4342 }
4443 }
4544
4645 // NewCounter returns a counter. Observations are aggregated and emitted once
4746 // per write invocation.
48 func (cw *CloudWatch) NewCounter(name string) *Counter {
49 c := NewCounter(name)
47 func (cw *CloudWatch) NewCounter(name string) metrics.Counter {
5048 cw.mtx.Lock()
49 defer cw.mtx.Unlock()
50 c := &counter{c: generic.NewCounter(name)}
5151 cw.counters[name] = c
52 cw.mtx.Unlock()
5352 return c
5453 }
5554
5655 // NewGauge returns a gauge. Observations are aggregated and emitted once per
5756 // write invocation.
58 func (cw *CloudWatch) NewGauge(name string) *Gauge {
59 g := NewGauge(name)
57 func (cw *CloudWatch) NewGauge(name string) metrics.Gauge {
6058 cw.mtx.Lock()
59 defer cw.mtx.Unlock()
60 g := &gauge{g: generic.NewGauge(name)}
6161 cw.gauges[name] = g
62 cw.mtx.Unlock()
6362 return g
6463 }
6564
6665 // NewHistogram returns a histogram. Observations are aggregated and emitted as
6766 // per-quantile gauges, once per write invocation. 50 is a good default value
6867 // for buckets.
69 func (cw *CloudWatch) NewHistogram(name string, buckets int) *Histogram {
70 h := NewHistogram(name, buckets)
68 func (cw *CloudWatch) NewHistogram(name string, buckets int) metrics.Histogram {
7169 cw.mtx.Lock()
70 defer cw.mtx.Unlock()
71 h := &histogram{h: generic.NewHistogram(name, buckets)}
7272 cw.histograms[name] = h
73 cw.mtx.Unlock()
7473 return h
7574 }
7675
8685 }
8786 }
8887
89 // Send will fire an api request to CloudWatch with the latest stats for
90 // all metrics.
88 // Send will fire an API request to CloudWatch with the latest stats for
89 // all metrics. It is preferred that the WriteLoop method is used.
9190 func (cw *CloudWatch) Send() error {
9291 cw.mtx.RLock()
9392 defer cw.mtx.RUnlock()
9493 now := time.Now()
9594
96 datums := []*cloudwatch.MetricDatum{}
95 var datums []*cloudwatch.MetricDatum
9796
9897 for name, c := range cw.counters {
9998 datums = append(datums, &cloudwatch.MetricDatum{
139138 return err
140139 }
141140
142 // Counter is a CloudWatch counter metric.
143 type Counter struct {
141 // counter is a CloudWatch counter metric.
142 type counter struct {
144143 c *generic.Counter
145144 }
146145
147 // NewCounter returns a new usable counter metric.
148 func NewCounter(name string) *Counter {
149 return &Counter{
150 c: generic.NewCounter(name),
151 }
152 }
153
154146 // With implements counter
155 func (c *Counter) With(labelValues ...string) metrics.Counter {
147 func (c *counter) With(labelValues ...string) metrics.Counter {
156148 c.c = c.c.With(labelValues...).(*generic.Counter)
157149 return c
158150 }
159151
160152 // Add implements counter.
161 func (c *Counter) Add(delta float64) {
153 func (c *counter) Add(delta float64) {
162154 c.c.Add(delta)
163155 }
164156
165 // Gauge is a CloudWatch gauge metric.
166 type Gauge struct {
157 // gauge is a CloudWatch gauge metric.
158 type gauge struct {
167159 g *generic.Gauge
168160 }
169161
170 // NewGauge returns a new usable gauge metric
171 func NewGauge(name string) *Gauge {
172 return &Gauge{
173 g: generic.NewGauge(name),
174 }
175 }
176
177162 // With implements gauge
178 func (g *Gauge) With(labelValues ...string) metrics.Gauge {
163 func (g *gauge) With(labelValues ...string) metrics.Gauge {
179164 g.g = g.g.With(labelValues...).(*generic.Gauge)
180165 return g
181166 }
182167
183168 // Set implements gauge
184 func (g *Gauge) Set(value float64) {
169 func (g *gauge) Set(value float64) {
185170 g.g.Set(value)
186171 }
187172
188173 // Add implements gauge
189 func (g *Gauge) Add(delta float64) {
174 func (g *gauge) Add(delta float64) {
190175 g.g.Add(delta)
191176 }
192177
193 // Histogram is a CloudWatch histogram metric
194 type Histogram struct {
178 // histogram is a CloudWatch histogram metric
179 type histogram struct {
195180 h *generic.Histogram
196181 }
197182
198 // NewHistogram returns a new usable histogram metric
199 func NewHistogram(name string, buckets int) *Histogram {
200 return &Histogram{
201 h: generic.NewHistogram(name, buckets),
202 }
203 }
204
205183 // With implements histogram
206 func (h *Histogram) With(labelValues ...string) metrics.Histogram {
184 func (h *histogram) With(labelValues ...string) metrics.Histogram {
207185 h.h = h.h.With(labelValues...).(*generic.Histogram)
208186 return h
209187 }
210188
211189 // Observe implements histogram
212 func (h *Histogram) Observe(value float64) {
190 func (h *histogram) Observe(value float64) {
213191 h.h.Observe(value)
214192 }
215193
11
22 import (
33 "errors"
4 "fmt"
5 "sync"
46 "testing"
5
6 "sync"
7
8 "fmt"
97
108 "github.com/aws/aws-sdk-go/service/cloudwatch"
119 "github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
10
1211 "github.com/go-kit/kit/log"
1312 "github.com/go-kit/kit/metrics/teststat"
1413 )
6463 namespace, name := "abc", "def"
6564 label, value := "label", "value"
6665 svc := newMockCloudWatch()
67 cw := New(namespace, log.NewNopLogger(), svc)
66 cw := New(namespace, svc, log.NewNopLogger())
6867 counter := cw.NewCounter(name).With(label, value)
6968 valuef := func() float64 {
7069 err := cw.Send()
8786 namespace, name := "abc", "def"
8887 label, value := "label", "value"
8988 svc := newMockCloudWatch()
90 cw := New(namespace, log.NewNopLogger(), svc)
89 cw := New(namespace, svc, log.NewNopLogger())
9190 gauge := cw.NewGauge(name).With(label, value)
9291 valuef := func() float64 {
9392 err := cw.Send()
110109 namespace, name := "abc", "def"
111110 label, value := "label", "value"
112111 svc := newMockCloudWatch()
113 cw := New(namespace, log.NewNopLogger(), svc)
112 cw := New(namespace, svc, log.NewNopLogger())
114113 histogram := cw.NewHistogram(name, 50).With(label, value)
115114 n50 := fmt.Sprintf("%s_50", name)
116115 n90 := fmt.Sprintf("%s_90", name)