Implementing custom telemetry using AWS CloudWatch SDK.

Implementing custom telemetry using AWS CloudWatch SDK.

Table of contents

No heading

No headings in the article.

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:

AWS Account.

GO setup on the machine.

VS code.

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 main

import (
    "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

Screenshot 2022-06-23 at 11.41.45 PM.png

  • 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.

Screenshot 2022-06-23 at 11.44.00 PM.png

Thanks for reading. I hope this story was helpful. If you are interested, check out my other articles.

you can also visit shubhamdeshmukh.com

Did you find this article valuable?

Support Direct Dev by becoming a sponsor. Any amount is appreciated!