noamtm
noamtm

Reputation: 12953

AWS CloudWatch custom metrics best practice

I'm running my Python web app on an EC2, and I want it to report some custom (app-level) metrics to CloudWatch.

Sample metrics are (uplink) request duration and similar.

From what I understand, I have to either use boto3 or the AWS CLI in order to do that. However:

  1. My app doesn't use boto3 for functionality, so it seems like an overkill to use it just for reporting metrics
  2. I have to be authenticated - unlike with Lambda, just the fact I'm running inside an EC2 does not mean I'm automatically authenticated.

What's the best practice here? My app doesn't have to run on EC2 (can be run it on GCP, Azure, or a custom server), so I really don't want to import boto3 into the code.

Upvotes: 1

Views: 1988

Answers (2)

Dejan Peretin
Dejan Peretin

Reputation: 12089

You can look into CloudWatch Embedded Metrics Format (EMF).

You would need to install and configure CloudWatch Agent on your EC2 instance and then you can use python EMF library to publish metrics.

With this approach:

  • Your application is not calling CloudWatch APIs directly. CloudWatch agent does the publishing.
  • You get custom metrics and EMF log entries in CloudWatch Logs, which can then be used with CloudWatch Logs Insights and Contributor Insights.

But you still need to provide a way to CloudWatch agent to authenticate against CloudWatch APIs. On EC2 instances this is done via the role your instance assumes.

Upvotes: 3

Ivan Shumov
Ivan Shumov

Reputation: 636

  • You can attach role and have access to AWS cervices without credentials.
  • It's all depends on your metric. Probably you don't need to use CloudWatch Metrics, but X-Ray in some cases.
  • If your app doesn't use both3 it's not means what you need to use that, but it's easiest way to call AWS API
  • If you are using GCP or Azure, please, use their monitoring services.
  • If you need to collect some custom metrics from anywhere, please, use some metrics service or implement your own API

Upvotes: 2

Related Questions