pythonamazon-web-servicesamazon-cloudwatchamazon-cloudwatch-metrics

Logging custom metrics using AWS Cloudwatch Agent and Python


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:

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

Solution

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