upgrade aws sdk v2
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
Mark Sagi-Kazar
2 years ago
7 | 7 | github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a |
8 | 8 | github.com/aws/aws-lambda-go v1.13.3 |
9 | 9 | github.com/aws/aws-sdk-go v1.38.65 |
10 | github.com/aws/aws-sdk-go-v2 v0.18.0 | |
10 | github.com/aws/aws-sdk-go-v2 v1.6.0 | |
11 | github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.4.1 | |
11 | 12 | github.com/casbin/casbin/v2 v2.1.2 |
12 | 13 | github.com/cenkalti/backoff v2.2.1+incompatible // indirect |
13 | 14 | github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec // indirect |
24 | 24 | github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU= |
25 | 25 | github.com/aws/aws-sdk-go v1.38.65 h1:umGu5gjIOKxzhi34T0DIA1TWupUDjV2aAW5vK6154Gg= |
26 | 26 | github.com/aws/aws-sdk-go v1.38.65/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= |
27 | github.com/aws/aws-sdk-go-v2 v0.18.0 h1:qZ+woO4SamnH/eEbjM2IDLhRNwIwND/RQyVlBLp3Jqg= | |
28 | github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= | |
27 | github.com/aws/aws-sdk-go-v2 v1.6.0 h1:r20hdhm8wZmKkClREfacXrKfX0Y7/s0aOoeraFbf/sY= | |
28 | github.com/aws/aws-sdk-go-v2 v1.6.0/go.mod h1:tI4KhsR5VkzlUa2DZAdwx7wCAYGwkZZ1H31PYrBFx1w= | |
29 | github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.4.1 h1:Mt2+LnGKQQyncULtRrx+oJkIwnrfy5XKb96Rvsml30U= | |
30 | github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.4.1/go.mod h1:dWC4cWmhO3NI8sDqWSPU587mlTLhxCFdS/o3ym/QgMU= | |
31 | github.com/aws/smithy-go v1.4.0 h1:3rsQpgRe+OoQgJhEwGNpIkosl0fJLdmQqF4gSFRjg+4= | |
32 | github.com/aws/smithy-go v1.4.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= | |
29 | 33 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= |
30 | 34 | github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= |
31 | 35 | github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= |
82 | 86 | github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= |
83 | 87 | github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih4= |
84 | 88 | github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= |
85 | github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= | |
86 | 89 | github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= |
87 | 90 | github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= |
88 | 91 | github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= |
170 | 173 | github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= |
171 | 174 | github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d h1:/WZQPMZNsjZ7IlCpsLGdQBINg5bxKQ1K1sh6awxLtkA= |
172 | 175 | github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= |
173 | github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= | |
174 | 176 | github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= |
175 | 177 | github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= |
176 | 178 | github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= |
432 | 434 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= |
433 | 435 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
434 | 436 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= |
435 | google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= | |
436 | 437 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= |
437 | 438 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= |
438 | 439 | google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= |
9 | 9 | |
10 | 10 | "github.com/aws/aws-sdk-go-v2/aws" |
11 | 11 | "github.com/aws/aws-sdk-go-v2/service/cloudwatch" |
12 | "github.com/aws/aws-sdk-go-v2/service/cloudwatch/cloudwatchiface" | |
12 | "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" | |
13 | 13 | "golang.org/x/sync/errgroup" |
14 | 14 | |
15 | 15 | "github.com/go-kit/kit/log" |
21 | 21 | const ( |
22 | 22 | maxConcurrentRequests = 20 |
23 | 23 | ) |
24 | ||
25 | // CloudWatchAPI is an interface that defines the set of Amazon CloudWatch API operations required by CloudWatch. | |
26 | type CloudWatchAPI interface { | |
27 | PutMetricData(ctx context.Context, params *cloudwatch.PutMetricDataInput, optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error) | |
28 | } | |
24 | 29 | |
25 | 30 | // CloudWatch receives metrics observations and forwards them to CloudWatch. |
26 | 31 | // Create a CloudWatch object, use it to create metrics, and pass those metrics as |
31 | 36 | mtx sync.RWMutex |
32 | 37 | sem chan struct{} |
33 | 38 | namespace string |
34 | svc cloudwatchiface.ClientAPI | |
39 | svc CloudWatchAPI | |
35 | 40 | counters *lv.Space |
36 | 41 | logger log.Logger |
37 | 42 | numConcurrentRequests int |
65 | 70 | // Namespace is applied to all created metrics and maps to the CloudWatch namespace. |
66 | 71 | // Callers must ensure that regular calls to Send are performed, either |
67 | 72 | // manually or with one of the helper methods. |
68 | func New(namespace string, svc cloudwatchiface.ClientAPI, options ...Option) *CloudWatch { | |
73 | func New(namespace string, svc CloudWatchAPI, options ...Option) *CloudWatch { | |
69 | 74 | cw := &CloudWatch{ |
70 | 75 | namespace: namespace, |
71 | 76 | svc: svc, |
130 | 135 | defer cw.mtx.RUnlock() |
131 | 136 | now := time.Now() |
132 | 137 | |
133 | var datums []cloudwatch.MetricDatum | |
138 | var datums []types.MetricDatum | |
134 | 139 | |
135 | 140 | cw.counters.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool { |
136 | datums = append(datums, cloudwatch.MetricDatum{ | |
141 | datums = append(datums, types.MetricDatum{ | |
137 | 142 | MetricName: aws.String(name), |
138 | 143 | Dimensions: makeDimensions(lvs...), |
139 | 144 | StatisticValues: stats(values), |
142 | 147 | return true |
143 | 148 | }) |
144 | 149 | |
145 | var batches [][]cloudwatch.MetricDatum | |
150 | var batches [][]types.MetricDatum | |
146 | 151 | for len(datums) > 0 { |
147 | var batch []cloudwatch.MetricDatum | |
152 | var batch []types.MetricDatum | |
148 | 153 | lim := len(datums) |
149 | 154 | if lim > maxConcurrentRequests { |
150 | 155 | lim = maxConcurrentRequests |
161 | 166 | defer func() { |
162 | 167 | <-cw.sem |
163 | 168 | }() |
164 | req := cw.svc.PutMetricDataRequest(&cloudwatch.PutMetricDataInput{ | |
169 | _, err := cw.svc.PutMetricData(context.TODO(), &cloudwatch.PutMetricDataInput{ | |
165 | 170 | Namespace: aws.String(cw.namespace), |
166 | 171 | MetricData: batch, |
167 | 172 | }) |
168 | _, err := req.Send(context.TODO()) | |
169 | 173 | return err |
170 | 174 | }) |
171 | 175 | } |
176 | 180 | |
177 | 181 | // Just build this once to reduce construction costs whenever |
178 | 182 | // someone does a Send with no aggregated values. |
179 | var zeros = cloudwatch.StatisticSet{ | |
183 | var zeros = types.StatisticSet{ | |
180 | 184 | Maximum: &zero, |
181 | 185 | Minimum: &zero, |
182 | 186 | Sum: &zero, |
183 | 187 | SampleCount: &zero, |
184 | 188 | } |
185 | 189 | |
186 | func stats(a []float64) *cloudwatch.StatisticSet { | |
190 | func stats(a []float64) *types.StatisticSet { | |
187 | 191 | count := float64(len(a)) |
188 | 192 | if count == 0 { |
189 | 193 | return &zeros |
202 | 206 | } |
203 | 207 | } |
204 | 208 | |
205 | return &cloudwatch.StatisticSet{ | |
209 | return &types.StatisticSet{ | |
206 | 210 | Maximum: &max, |
207 | 211 | Minimum: &min, |
208 | 212 | Sum: &sum, |
210 | 214 | } |
211 | 215 | } |
212 | 216 | |
213 | func makeDimensions(labelValues ...string) []cloudwatch.Dimension { | |
214 | dimensions := make([]cloudwatch.Dimension, len(labelValues)/2) | |
217 | func makeDimensions(labelValues ...string) []types.Dimension { | |
218 | dimensions := make([]types.Dimension, len(labelValues)/2) | |
215 | 219 | for i, j := 0, 0; i < len(labelValues); i, j = i+2, j+1 { |
216 | dimensions[j] = cloudwatch.Dimension{ | |
220 | dimensions[j] = types.Dimension{ | |
217 | 221 | Name: aws.String(labelValues[i]), |
218 | 222 | Value: aws.String(labelValues[i+1]), |
219 | 223 | } |
0 | 0 | package cloudwatch2 |
1 | 1 | |
2 | 2 | import ( |
3 | "net/http" | |
3 | "context" | |
4 | 4 | "strings" |
5 | 5 | "testing" |
6 | 6 | |
7 | "github.com/aws/aws-sdk-go-v2/aws" | |
8 | 7 | "github.com/aws/aws-sdk-go-v2/service/cloudwatch" |
9 | "github.com/aws/aws-sdk-go-v2/service/cloudwatch/cloudwatchiface" | |
8 | "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" | |
10 | 9 | ) |
11 | 10 | |
12 | 11 | func TestStats(t *testing.T) { |
72 | 71 | } |
73 | 72 | |
74 | 73 | type mockCloudWatch struct { |
75 | cloudwatchiface.ClientAPI | |
74 | CloudWatchAPI | |
76 | 75 | latestName string |
77 | latestData []cloudwatch.MetricDatum | |
76 | latestData []types.MetricDatum | |
78 | 77 | } |
79 | 78 | |
80 | func (mcw *mockCloudWatch) PutMetricDataRequest(in *cloudwatch.PutMetricDataInput) cloudwatch.PutMetricDataRequest { | |
81 | mcw.latestName = *in.Namespace | |
82 | mcw.latestData = in.MetricData | |
83 | return cloudwatch.PutMetricDataRequest{ | |
84 | // To mock the V2 API, most of the functions spit | |
85 | // out structs that you need to call Send() on. | |
86 | // The non-intuitive thing is that to get the Send() to avoid actually | |
87 | // going across the wire, you just create a dumb aws.Request with either | |
88 | // aws.Request.Data defined (for succes) or with aws.Request.Error | |
89 | // to simulate an Error. | |
90 | Request: &aws.Request{ | |
91 | HTTPRequest: &http.Request{Method: "PUT"}, | |
92 | Data: &cloudwatch.PutMetricDataOutput{}, | |
93 | }, | |
94 | Input: in, | |
95 | } | |
79 | func (mcw *mockCloudWatch) PutMetricData(ctx context.Context, params *cloudwatch.PutMetricDataInput, optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error) { | |
80 | mcw.latestName = *params.Namespace | |
81 | mcw.latestData = params.MetricData | |
82 | ||
83 | return nil, nil | |
96 | 84 | } |
97 | 85 | |
98 | 86 | func TestSend(t *testing.T) { |