2
votes

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",
        },
    ],
)
1

1 Answers

2
votes

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.