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

Choose Copy to save the code locally.

Create the file listing_metrics.go.

Import the packages used in the example.

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

Get the metric name, namespace, and dimensions from the command line.

if len(os.Args) != 4 { fmt.Println("You must supply a metric name, namespace, and dimensions") os.Exit(1) } metric := os.Args[1] namespace := os.Args[2] dimensions := os.Args[3]

Initialize a session that the SDK will use to load credentials from the shared credentials file ~/.aws/credentials, load your configuration from the shared configuration file ~/.aws/config, and create a CloudWatch client.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create CloudWatch client svc := cloudwatch.New(sess)

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

result, err := svc.ListMetrics(&cloudwatch.ListMetricsInput{ MetricName: aws.String(metric), Namespace: aws.String(namespace), Dimensions: []*cloudwatch.DimensionFilter{ &cloudwatch.DimensionFilter{ Name: aws.String(name), }, }, }) fmt.Println("Metrics", result.Metrics)

See the complete example on GitHub.

Submit Custom Metrics

Choose Copy to save the code locally.

Create the file custom_metrics.go.

Import the packages used in the example.

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

Get the metric name, namespace, and dimensions from the command line.

if len(os.Args) != 4 { fmt.Println("You must supply a metric name, dimension, value, and namespace") os.Exit(1) } metric := os.Args[1] dimensions := os.Args[3] namespace := os.Args[2]

Initialize a session that the SDK will use to load credentials from the shared credentials file ~/.aws/credentials, load your configuration from the shared configuration file ~/.aws/config, and create a CloudWatch client.

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.

result, err := svc.PutMetricData(&cloudwatch.PutMetricDataInput{ MetricData: []*cloudwatch.MetricDatum{ &cloudwatch.MetricDatum{ MetricName: aws.String(metric), Unit: aws.String(cloudwatch.StandardUnitNone), Value: aws.Float64(1.0), Dimensions: []*cloudwatch.Dimension{ &cloudwatch.Dimension{ Name: aws.String(dimension), Value: aws.String(value), }, }, }, }, Namespace: aws.String(namespace), }) fmt.Println("Success", result)

See the complete example on GitHub.