-
Notifications
You must be signed in to change notification settings - Fork 0
/
cws.go
166 lines (138 loc) · 3.9 KB
/
cws.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
package tune
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
// CWStatter represents all the elements necessary to meet the Statter struct
// used by the tune statting client
type CWStatter struct {
prefix string
svc *cloudwatch.CloudWatch
}
// NewCWStatter takes a prefix and returns a configured statter to point at
// cloudwatch
func NewCWStatter(prefix, region string) (*Client, error) {
sess, err := session.NewSession(&aws.Config{Region: ®ion})
if err != nil {
return nil, fmt.Errorf("failed to create new session: %v", err.Error())
}
svc := cloudwatch.New(sess)
c := &Client{
&CWStatter{
prefix: prefix,
svc: svc,
},
}
return c, nil
}
// NewCWStatterWithClient takes a prefix and http client and returns a
// configured statter to point at cloudwatch
func NewCWStatterWithClient(prefix, region string, client *http.Client) (*Client, error) {
sess, err := session.NewSession(&aws.Config{
Region: ®ion,
HTTPClient: client,
})
if err != nil {
return nil, fmt.Errorf("unable to create session for AWS client: %v", err.Error())
}
svc := cloudwatch.New(sess)
c := &Client{
&CWStatter{
prefix: prefix,
svc: svc,
},
}
return c, nil
}
func metricNames(bucket string) (string, string, string) {
s := strings.Split(bucket, ".")
switch {
case len(s) == 2:
return s[0], strings.Join(s[1:], "."), strings.Join(s[1:], ".")
case len(s) >= 3:
return s[0], strings.Join(s[1:len(s)-1], "."), s[len(s)-1]
default:
return s[0], s[0], s[0]
}
}
// Counter meets the Statter interface
func (cws *CWStatter) Counter(sampleRate float32, bucket string, n ...int) {
dname, dvalue, mname := metricNames(bucket)
unit := "Count"
dimension := []*cloudwatch.Dimension{&cloudwatch.Dimension{
Name: &dname,
Value: &dvalue,
}}
datums := make([]*cloudwatch.MetricDatum, 0, len(n))
for _, v := range n {
d := &cloudwatch.MetricDatum{
MetricName: &mname,
Unit: &unit,
StorageResolution: aws.Int64(int64(sampleRate)),
Value: aws.Float64(float64(v)),
Dimensions: dimension,
}
datums = append(datums, d)
}
go cws.svc.PutMetricData(&cloudwatch.PutMetricDataInput{ //nolint,errcheck
Namespace: &cws.prefix,
MetricData: datums,
})
}
// Timing meets the Statter interface
func (cws *CWStatter) Timing(sampleRate float32, bucket string, d ...time.Duration) {
dname, dvalue, mname := metricNames(bucket)
unit := "Milliseconds"
dimension := []*cloudwatch.Dimension{&cloudwatch.Dimension{
Name: &dname,
Value: &dvalue,
}}
datums := make([]*cloudwatch.MetricDatum, 0, len(d))
for _, t := range d {
d := &cloudwatch.MetricDatum{
MetricName: &mname,
Unit: &unit,
StorageResolution: aws.Int64(int64(sampleRate)),
Value: aws.Float64(float64(time.Millisecond * t)),
Dimensions: dimension,
}
datums = append(datums, d)
}
go cws.svc.PutMetricData(&cloudwatch.PutMetricDataInput{ //nolint,errcheck
Namespace: &cws.prefix,
MetricData: datums,
})
}
// Gauge meets the Statter interface
func (cws *CWStatter) Gauge(sampleRate float32, bucket string, value ...string) {
dname, dvalue, mname := metricNames(bucket)
unit := "Count"
dimension := []*cloudwatch.Dimension{&cloudwatch.Dimension{
Name: &dname,
Value: &dvalue,
}}
datums := make([]*cloudwatch.MetricDatum, 0, len(value))
for _, v := range value {
f, err := strconv.ParseFloat(v, 64)
if err == nil {
d := &cloudwatch.MetricDatum{
MetricName: &mname,
Unit: &unit,
StorageResolution: aws.Int64(int64(sampleRate)),
Value: &f,
Dimensions: dimension,
}
datums = append(datums, d)
}
}
go cws.svc.PutMetricData(&cloudwatch.PutMetricDataInput{ //nolint,errcheck
Namespace: &cws.prefix,
MetricData: datums,
})
}