Send Custom Metrics to CloudWatch using Embedded Metric Format (EMF)

Aparna Rathore
2 min readAug 24, 2023

--

Sending custom metrics to Amazon CloudWatch using the Embedded Metric Format (EMF) involves a few steps. EMF is a way to send high-cardinality, fine-grained metrics to CloudWatch. It’s especially useful when you have a large number of individual metrics that you want to aggregate.

Here’s a general outline of how you can achieve this:

1. **Include EMF Libraries:**
Include the EMF libraries in your application. These libraries provide functions to generate and send metrics in the EMF format.

2. **Generate Metrics:**
In your application code, you’ll create instances of EMF metrics and add data to them. Each metric can have multiple dimensions and values.

3. **Create EMF Batch:**
EMF metrics are grouped into batches before sending to CloudWatch. You’ll need to create a batch and add your metrics to it.

4. **Configure AWS Credentials:**
Ensure your application has the necessary AWS credentials to send metrics to CloudWatch. This can be done using environment variables, AWS configuration files, or IAM roles if running in an AWS environment.

5. **Send Metrics:**
After adding metrics to the batch, you’ll send the batch to CloudWatch using the AWS SDK. The SDK handles the communication with CloudWatch for you.

6. **Set Up CloudWatch Namespace:**
In the AWS CloudWatch console, make sure you’ve created a custom namespace where you want to store your metrics.

Here’s a simple example using Python and the Boto3 AWS SDK:

```python
import boto3
from aws_embedded_metrics import metric_scope

# Create a CloudWatch client
cloudwatch = boto3.client(‘cloudwatch’)

# Define your metrics
@metric_scope
def main(metrics):
metric = metrics.create_metric(name=”CustomMetric”, unit=”None”)
metric.set_value(42)
metric.add_property(“PropertyKey”, “PropertyValue”)
metrics.add_metric(metric)

# Send metrics to CloudWatch
metrics.flush()

if __name__ == “__main__”:
main()
```

Remember to replace `”CustomMetric”`, `”PropertyKey”`, and `”PropertyValue”` with appropriate values for your use case.

Please note that the actual implementation might vary based on the programming language you’re using and the specific use case. Be sure to refer to the official documentation for the AWS SDK and the EMF libraries you’re using for more detailed instructions and examples.

--

--

Aparna Rathore
Aparna Rathore

Written by Aparna Rathore

A Tiny Girl with not so many tiny dreams.

No responses yet