-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
ListMetricsv2.go
69 lines (56 loc) · 2.02 KB
/
ListMetricsv2.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
// snippet-start:[cloudwatch.go-v2.ListMetrics]
package main
import (
"context"
"fmt"
"strconv"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
)
// CWListMetricsAPI defines the interface for the ListMetrics function.
// We use this interface to test the function using a mocked service.
type CWListMetricsAPI interface {
ListMetrics(ctx context.Context,
params *cloudwatch.ListMetricsInput,
optFns ...func(*cloudwatch.Options)) (*cloudwatch.ListMetricsOutput, error)
}
// GetMetrics gets the name, namespace, and dimension name of your Amazon CloudWatch metrics
// Inputs:
// c is the context of the method call, which includes the Region
// api is the interface that defines the method call
// input defines the input arguments to the service call.
// Output:
// If success, a ListMetricsOutput object containing the result of the service call and nil
// Otherwise, nil and an error from the call to ListMetrics
func GetMetrics(c context.Context, api CWListMetricsAPI, input *cloudwatch.ListMetricsInput) (*cloudwatch.ListMetricsOutput, error) {
return api.ListMetrics(c, input)
}
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic("configuration error, " + err.Error())
}
client := cloudwatch.NewFromConfig(cfg)
input := &cloudwatch.ListMetricsInput{}
result, err := GetMetrics(context.TODO(), client, input)
if err != nil {
fmt.Println("Could not get metrics")
return
}
fmt.Println("Metrics:")
numMetrics := 0
for _, m := range result.Metrics {
fmt.Println(" Metric Name: " + *m.MetricName)
fmt.Println(" Namespace: " + *m.Namespace)
fmt.Println(" Dimensions:")
for _, d := range m.Dimensions {
fmt.Println(" " + *d.Name + ": " + *d.Value)
}
fmt.Println("")
numMetrics++
}
fmt.Println("Found " + strconv.Itoa(numMetrics) + " metrics")
}
// snippet-end:[cloudwatch.go-v2.ListMetrics]