Menu
AWS SDK for Go
Developer Guide

Getting Metrics from CloudWatch

These Go examples show you how to retrieve a list of published CloudWatch metrics and publish data points to CloudWatch metrics with the AWS SDK for Go, as follows:

  • Listing metrics

  • Submitting custom metrics

You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.

Scenario

Metrics are data about the performance of your systems. You can enable detailed monitoring of some resources, such as your Amazon EC2 instances, or your own application metrics.

In this example, Go code is used to get metrics from CloudWatch and to send events to CloudWatch Events. The code uses the AWS SDK for Go to get metrics from CloudWatch by using these methods of the CloudWatch type:

Prerequisites

List Metrics

Create a new Go file named listing_metrics.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

Copy
package main import ( "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatch" )

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials, and create a new Amazon EC2 service client.

Copy
func main() { // Load session from shared config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create new cloudwatch client. svc := cloudwatch.New(sess)

Call ListMetrics, supplying the metric name, namespace, and dimensions. Print the metrics returned in the result.

Copy
result, err := svc.ListMetrics(&cloudwatch.ListMetricsInput{ MetricName: aws.String("IncomingLogEvents"), Namespace: aws.String("AWS/Logs"), Dimensions: []*cloudwatch.DimensionFilter{ &cloudwatch.DimensionFilter{ Name: aws.String("LogGroupName"), }, }, }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Metrics", result.Metrics) }

Submit Custom Metrics

Create a new Go file named custom_metrics.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

Copy
package main import ( "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatch" )

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials, and create a new Amazon EC2 service client.

Copy
func main() { // Load session from shared config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create new cloudwatch client. svc := cloudwatch.New(sess)

Call PutMetricData, suppylying the metric name, unit, value, and dimensions. Print any errors, or a success message.

Copy
result, err := svc.PutMetricData(&cloudwatch.PutMetricDataInput{ MetricData: []*cloudwatch.MetricDatum{ &cloudwatch.MetricDatum{ MetricName: aws.String("PAGES_VISITED"), Unit: aws.String(cloudwatch.StandardUnitNone), Value: aws.Float64(1.0), Dimensions: []*cloudwatch.Dimension{ &cloudwatch.Dimension{ Name: aws.String("UNIQUE_PAGES"), Value: aws.String("URLS"), }, }, }, }, Namespace: aws.String("SITE/TRAFFIC"), }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", result) }