Naman Bhargava
Naman Bhargava

Reputation: 59

Custom metrics using the CloudWatch agent with the StatsD protocol

I have a web application running in EC2 instance. It has different API endpoints. I want to count the number of times each API is called. The web application is in Java.

Can anyone suggest to me some articles where I can find proper Java implementation for integration of statsD with CloudWatch?

Upvotes: 4

Views: 2672

Answers (2)

Koncervator
Koncervator

Reputation: 34

  1. Install CloudWatch Agent on your EC2 instance

  2. Locate and open CW Agent config file

  3. Add statsd section into the config file (JSON format)

    {
      ....,
      "statsd": {
        "metrics_aggregation_interval": 60,
        "metrics_collection_interval": 10,
        "service_address": ":8125"
      }
    }
    
  4. AWS CloudWatch agent is smart enough to understand custom tags helping you to correctly split statistics gathered from different API methods ("correctly" here means splitting API methods stats by dimension name, not by metric name). So you need a Java client lib supporting tags. For example, DataDog client

  5. Configure the client instance as explained in the package documentation and that's it. Now you can do thing like this at the beginning of your each REST API operation:

    statsd.incrementCounter(“InvocationCount”, 1, “host:YOUR-EC2-INSTANCE-NAME”, “operation:YOUR-REST-OPERATION-NAME”);
    
  6. CloudWatch will handle everything else automatically. You will be able to see you metrics data flowing in the AWS CloudWatch Console under "CWAgent" namespace. Please be aware that average delay between statds client call and the data visibility in CW Console is about 10-15 minutes.

  7. Manually writing statsd calls in each REST API operation may not be a good idea. Decorators will help you to automatically instrument it with just a several lines of code.

Upvotes: 0

Madhan S
Madhan S

Reputation: 937

Refer their doc page https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-custom-metrics-statsd.html, They have mentioned about publishing the metrics in the same page, for your client side you can refer https://github.com/etsy/statsd/wiki#client-implementations.

Usually I follow a simple approach without using statsd, Log the events in the file and sync the file to the Cloudwatch, In cloudwatch you can configure filters and based on filters, you can increment the custom metrics.

Upvotes: 1

Related Questions