3

We send up custom metrics to AWS using Python (see existing code below) and separately use the AWS CloudWatch Agent to send up metrics for our EC2 machine. However, we'd like to stop sending the custom metrics through a boto client and instead send them up using the AWS CloudWatch agent.

I've found details on how to send up custom metrics from StatsD and collectd, but it's unclear how to send up your own custom metrics. I'm guessing we'll have to export our metrics in a similar data format to one of these, but it's unclear how to do that. In summary, we need to:

  • Export the metric in Python to a log file in the right format
  • Update the AWS CloudWatch Agent to read from those log files and upload the metric

Does anyone have an example that covers that?

Existing Code

import boto3
cloudwatch = boto3.client(
    service_name="cloudwatch",
    region_name=env["AWS_DEPLOYED_REGION"],
    api_version="2010-08-01",
)
cloudwatch.put_metric_data(
    Namespace="myNameSpace",
    MetricData=[
        {
            "MetricName": "someName",
            "Dimensions": [
                {"Name": "Stage", "Value": "..."},
                {"Name": "Purpose", "Value": "..."},
            ],
            "Values": values,
            "StorageResolution": 60,
            "Unit": "someUnit",
        },
    ],
)
pir
  • 5,513
  • 12
  • 63
  • 101
  • 1
    It looks a little complex: [New – How to better monitor your custom application metrics using Amazon CloudWatch Agent | AWS DevOps Blog](https://aws.amazon.com/blogs/devops/new-how-to-better-monitor-your-custom-application-metrics-using-amazon-cloudwatch-agent/) – John Rotenstein May 31 '20 at 23:38

2 Answers2

3

CloudWatch Agent supports StatsD or CollectD for collecting custom metrics. There is no support for using the AWS CloudWatch SDK and pointing it to the CW Agent.

To use StatsD or CollectD, you just follow the documentation for that specific tool. Then CloudWatch provide an adapter for both that interface to the CloudWatch Agent as I linked above. This is generally useful for people who already use StatsD or CollectD for custom and application metrics however its clearly painful in your case as you will have to onboard to either or in order to achieve you desired effect.

mickzer
  • 5,958
  • 5
  • 34
  • 57
2

You can create CloudWatch agent config files in /etc/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d/ directory.

The config file should be like,

{
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "path_to_log_file/app1.log",
                        "log_group_name": "/app/custom.log",
                        "log_stream_name": "{instance_id}"
                    }
                ]
            }
        }
    }
}

Restarting the cw agent will consider this configuration automatically.

One more way is to attach config files manually using the command,

/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a append-config -m ec2 -s -c file:/path_to_json/custom_log.json

This log group will be available in the CloudWatch Logs console.

gowthz
  • 400
  • 2
  • 8
  • 22