Custom Metrics to AWS CloudWatch from ASP.NET Core
Tags: dotnetcore, aspnetcore, aws
Published: 2019-08-09 14:52:43

---
title: Custom Metrics to AWS CloudWatch from ASP.NET Core
published: true
tags: dotNETCore,ASP.NET Core,AWS
canonical_url: https://codeopinion.com/custom-metrics-to-aws-cloudwatch-from-asp-net-core/
---

![CloudWatch Custom Metrics](https://codeopinion.com/wp-content/uploads/2019/08/aws-cloudwatch-logo-png-transparent-265x300.png)

I was playing around with AWS CloudWatch and was curious to send custom metrics from ASP.NET Core. Specifically the execution time of an HTTP request.

## AWS SDK

I created a simple middleware that starts a StopWatch before calling the next middleware in the pipeline. When it returns, stop the StopWatch and send the data to CloudWatch.

First is to add the relevant NuGet packages.

{% gist https://gist.github.com/dcomartin/8a12b8a7092a522b0c9fe53c6c22baad %}

## Configuration

If you’ve never used the AWS SDK/Packages, I recommend checking out my post on [Configuring AWS SDK in ASP.NET Core](https://codeopinion.com/configuring-aws-sdk-in-asp-net-core/). It goes over creating a named profile to store your AWS credentials and using appSettings or Environment variables to pass them through to ASP.NET Core.

## Middleware

Next is to create a simple middleware using the SDK. The middleware is having the **IAmazonCloudwatch** injected into the constructor. In the Startup’s **ConfigureServices** is where this is configured.

We’re simply calling the **PutMetricDataAsync** with a list of one MetricDatum. It contains the metric name, value, unit, timestamp, and dimensions.

{% gist https://gist.github.com/dcomartin/7da98c1bb3c0e2654d035aa6d6f1c372 %}

## Startup

As mentioned, in order to use the AWS SDK and the **IAmazonCloudWatch** in the middleware, you can use the **AddDefaultAWSOptions** and **AddAWSService<IAmazonCloudWatch>** to **ConfigureServices** to register the appropriate types.

{% gist https://gist.github.com/dcomartin/3280bbdcc6797f0265ce777a9bb4bd90 %}

## Results

If you look in CloudWatch, you can now see the new custom metrics we’ve published.

![](https://codeopinion.com/wp-content/uploads/2019/08/image-1024x432.png)

## Next

Before you start using this in a production environment, there are a couple of glaring issues.

First, sending the metrics to AWS, although incredibly fast should be handled outside of the actual HTTP request. this can be done completely asynchronously from the actual request. There is no point in delaying the HTTP response to the client. To solve this, we’ll add a queue and do it in a background service.

Secondly, some of the cost associated with CloudWatch is based on the number of API calls. This is why there is a **List<MetricDatum>** in the **PutMetricDataRequest**. It allows you to send/batch multiple metrics together to limit the number of API calls.

This is a better solution is to collect **PutMetricDataRequest** separately and send them in batch in a background service.

More on that coming soon.

[Follow @codeopinion](https://twitter.com/codeopinion?ref_src=twsrc%5Etfw)

The post [Custom Metrics to AWS CloudWatch from ASP.NET Core](https://codeopinion.com/custom-metrics-to-aws-cloudwatch-from-asp-net-core/) appeared first on [CodeOpinion](https://codeopinion.com).