AWS CLOUDWATCH METRICS

Implementing custom telemetry using AWS CloudWatch SDK.

Add metric addition capabilities to your application using CloudWatch SDK.

Shubham Deshmukh
AWS Tip
Published in
2 min readJun 24, 2022

--

Hello Friends, Today we will be creating custom telemetry using AWS Cloudwatch SDK. This will use Cloudwatch to store the data. To understand more about Cloudwatch metrics please visit this LINK.

Prerequisites:

Now that we know what we are doing let's start the implementation.

  • Create a golang project in vs code. add the below code to main.go and import all the necessary libraries. Please create an IAM role to add SECRET_KEY, ACCESS_KEY. Also, add the region to the placeholder.
  • the below code will create a namespace in AWS Cloudwatch with the name MyAppTelemetry and add a custom metric with the name CustomMetric. this metric will spit out random values when you run the application.
package mainimport (
"math/rand"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"fmt"
)
func main() {
// Initialize a session that the SDK uses to load
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{
// ...
Region: aws.String("<MY_REGION>"),
Credentials: credentials.NewStaticCredentials(
"MY_ACCESS_KEY",
"MY_SECRET_KEY",
"",
),
},
}))
// Create new cloudwatch client.
svc := cloudwatch.New(sess)
_, err := svc.PutMetricData(&cloudwatch.PutMetricDataInput{
Namespace: aws.String("MyAppTelemetry"),
MetricData: []*cloudwatch.MetricDatum{
&cloudwatch.MetricDatum{
MetricName: aws.String("CustomMetric"),
Unit: aws.String("Count"),
Value: aws.Float64(rand.Float64() * 10),
Dimensions: []*cloudwatch.Dimension{
&cloudwatch.Dimension{
Name: aws.String("mymobileapp"),
Value: aws.String("abc.xyz"),
},
},
},
},
})
if err != nil {
fmt.Println("Error adding metrics:", err.Error())
return
}
// Get information about metrics
result, err := svc.ListMetrics(&cloudwatch.ListMetricsInput{
Namespace: aws.String("MyAppTelemetry"),
})
if err != nil {
fmt.Println("Error getting metrics:", err.Error())
return
}
for _, metric := range result.Metrics {
fmt.Println(*metric.MetricName)
for _, dim := range metric.Dimensions {
fmt.Println(*dim.Name+":", *dim.Value)
fmt.Println()
}
}
}
  • Once all the changes are made please run the application with the below command.
go run main.go

if you got no error that means the application execution was successful and data values are being sent to Cloudwatch.

  • You can visit the Cloudwatch metric section of AWS to visualize the telemetry.

Thanks for reading. I hope this story was helpful. If you are interested,

check out my other articles.

you can also visit shubhamdeshmukh.com.

GitHub: https://github.com/sd2995/aws/tree/main/CloudwatchMetric

--

--

Writer for

Software Developer | Cloud infrastructure | IaC | AWS | DevOps | Hit the follow button for Cloud-related content 😄