Package list golang-github-go-kit-kit / 3a00cf8
Add gauge instrument. Cameron Stitt 4 years ago
1 changed file(s) with 53 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1717 prefix string
1818 svc *cloudwatch.CloudWatch
1919 counters map[string]*Counter
20 gauges map[string]*Gauge
2021 logger log.Logger
2122 }
2223
2627 prefix: prefix,
2728 svc: svc,
2829 counters: map[string]*Counter{},
30 gauges: map[string]*Gauge{},
2931 logger: logger,
3032 }
3133 }
3234
3335 func (cw *CloudWatch) NewCounter(name string) *Counter {
3436 c := NewCounter(name)
37 cw.mtx.Lock()
38 cw.counters[cw.prefix+name] = c
39 cw.mtx.Unlock()
40 return c
41 }
42
43 func (cw *CloudWatch) NewGauge(name string) *Gauge {
44 c := NewGauge(name)
3545 cw.mtx.Lock()
3646 cw.counters[cw.prefix+name] = c
3747 cw.mtx.Unlock()
5666 MetricName: aws.String(name),
5767 Dimensions: makeDimensions(c.c.LabelValues()...),
5868 Value: aws.Float64(c.c.Value()),
69 Timestamp: aws.Time(now),
70 },
71 },
72 })
73 if err != nil {
74 cw.logger.Log("during", "WriteLoop", "err", err)
75 }
76 }
77
78 for name, g := range cw.gauges {
79 _, err := cw.svc.PutMetricData(&cloudwatch.PutMetricDataInput{
80 Namespace: aws.String(cw.prefix),
81 MetricData: []*cloudwatch.MetricDatum{
82 {
83 MetricName: aws.String(name),
84 Dimensions: makeDimensions(g.g.LabelValues()...),
85 Value: aws.Float64(g.g.Value()),
5986 Timestamp: aws.Time(now),
6087 },
6188 },
89116 c.c.Add(delta)
90117 }
91118
119 // Gauge is a CloudWatch gauge metric.
120 type Gauge struct {
121 g *generic.Gauge
122 }
123
124 // NewGauge returns a new usable gauge metric
125 func NewGauge(name string) *Gauge {
126 return &Gauge{
127 g: generic.NewGauge(name),
128 }
129 }
130
131 func (g *Gauge) With(labelValues ...string) metrics.Gauge {
132 return &Gauge{
133 g: g.g.With(labelValues...),
134 }
135 }
136
137 func (g *Gauge) Set(value float64) {
138 g.g.Set(value)
139 }
140
141 func (g *Gauge) Add(delta float64) {
142 g.g.Add(delta)
143 }
144
92145 func makeDimensions(labelValues ...string) []*cloudwatch.Dimension {
93146 dimensions := make([]*cloudwatch.Dimension, len(labelValues)/2)
94147 for i, j := 0, 0; i < len(labelValues); i, j := i+2, j+1 {