-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
ListMetrics.go
76 lines (63 loc) · 2.09 KB
/
ListMetrics.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[cloudwatch.go.list_metrice]
package main
// snippet-start:[cloudwatch.go.list_metrice.imports]
import (
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
// snippet-end:[cloudwatch.go.list_metrice.imports]
// GetMetrics gets the name, namespace, and dimension name of your Amazon CloudWatch metrics
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
//
// Output:
//
// If successful, the metrics and nil
// Otherwise, nil and an error from a call to ListMetrics
func GetMetrics(sess *session.Session) (*cloudwatch.ListMetricsOutput, error) {
// snippet-start:[cloudwatch.go.list_metrice.call]
// Create CloudWatch client
svc := cloudwatch.New(sess)
result, err := svc.ListMetrics(nil)
// snippet-end:[cloudwatch.go.list_metrice.call]
if err != nil {
return nil, err
}
return result, nil
}
func main() {
// Initialize a session that the SDK uses to load
// credentials from the shared credentials file ~/.aws/credentials
// and configuration from the shared configuration file ~/.aws/config.
// snippet-start:[cloudwatch.go.list_metrice.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[cloudwatch.go.list_metrice.session]
result, err := GetMetrics(sess)
if err != nil {
fmt.Println("Could not get metrics")
return
}
// snippet-start:[cloudwatch.go.list_metrice.print]
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.list_metrice.print]
}
// snippet-end:[cloudwatch.go.list_metrice]