/
cloudwatch.go
59 lines (50 loc) 路 1.46 KB
/
cloudwatch.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
//nolint:deadcode,unused,varcheck
package aws
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
)
const statAvg = types.StatisticAverage
const statSum = types.StatisticSum
const unitCount = types.StandardUnitCount
func cloudwatchNewClient(ctx context.Context, region string) (*cloudwatch.Client, error) {
cfg, err := getConfig(ctx, region)
if err != nil {
return nil, err
}
return cloudwatch.NewFromConfig(cfg), nil
}
type statsRequest struct {
region string
namespace string
metric string
dimensions map[string]string
statistic types.Statistic
unit types.StandardUnit
}
func cloudwatchGetMonthlyStats(ctx context.Context, req statsRequest) (*cloudwatch.GetMetricStatisticsOutput, error) {
client, err := cloudwatchNewClient(ctx, req.region)
if err != nil {
return nil, err
}
dim := make([]types.Dimension, 0, len(req.dimensions))
for k, v := range req.dimensions {
dim = append(dim, types.Dimension{
Name: strPtr(k),
Value: strPtr(v),
})
}
return client.GetMetricStatistics(ctx, &cloudwatch.GetMetricStatisticsInput{
Namespace: strPtr(req.namespace),
MetricName: strPtr(req.metric),
StartTime: aws.Time(time.Now().Add(-timeMonth)),
EndTime: aws.Time(time.Now()),
Period: int32Ptr(60 * 60 * 24 * 30),
Statistics: []types.Statistic{req.statistic},
Unit: req.unit,
Dimensions: dim,
})
}