Custom Metrics to AWS CloudWatch from ASP.NET Core

CloudWatch Custom Metrics

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.

Configuration

If you’ve never used the AWS SDK/Packages, I recommend checking out my post on 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.

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.

Results

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

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 on Twitter

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Roundup #50: HttpRepl, ReferenceAssemblies, IAsyncEnumerable, Searching Nuget, Resilience

Here are the things that caught my eye recently in .NET.  I’d love to hear what you found most interesting this week.  Let me know in the comments or on Twitter.

Follow @CodeOpinion on Twitter

HttpRepl: A command-line tool for interacting with RESTful HTTP services

The ASP.NET team has built a command-line tool called HttpRepl. It lets you browse and invoke HTTP services in a similar way to working with files and folders. You give it a starting point (a base URL) and then you can execute commands like “dir” and “cd” to navigate your way around the API.

Link: https://devblogs.microsoft.com/aspnet/httprepl-a-command-line-tool-for-interacting-with-restful-http-services/

Using the ReferenceAssemblies NuGet package to build .NET Framework libraries on Linux, without installing Mono

In this post I show how you can build .NET projects that target .NET Framework versions on Linux, withoutusing Mono. By using the new Microsoft.NETFramework.ReferenceAssemblies NuGet packages from Microsoft you don’t need to install anything more than the .NET Core SDK!

Link: https://andrewlock.net/using-reference-assemblies-to-build-net-framework-libararies-on-linux-without-mono/

What’s the big deal with IAsyncEnumerable<T> in .NET Core 3.0

One of the most exciting features of .NET Core 3.0 and C# 8.0 has been the addition of IAsyncEnumerable<T> (aka async streams). But what’s so special about it? What can we do now that wasn’t possible before?

In this article, we’ll look at what challenges IAsyncEnumerable<T> is intended to solve, how to implement it in our own applications, and why IAsyncEnumerable<T> will replace Task<IEnumerable<T>> in many situations.

Link: https://dev.to/dotnet/what-s-the-big-deal-with-iasyncenumerable-t-in-net-core-3-1eii

Indexing and searching NuGet.org with Azure Functions and Search

In an application I’m writing, I need to deserialize some JSON. I know the class to use is JsonConvert, but which NuGet package was that type in again?

Granted, that’s an obvious one. Yet, there are many uses for a “NuGet reverse package search”that helps finding the correct NuGet package based on a public type.

While ReSharper and Rider initially added this nice feature and Visual Studio has added this a few years later as well, I wanted to see if I could build an indexer and search engine that collects public type information from NuGet packages and makes them searchable. And along the way, I discovered that this would be an ideal use case for Azure Functions.

Link: https://blog.maartenballiauw.be/post/2019/07/30/indexing-searching-nuget-with-azure-functions-and-search.html

System Stable: Robust connected applications with Polly, the .NET Resilience Framework – Bryan Hogan

In this session I will show you how with just a few lines of code you can make your applications much more resilient and reliable. With Polly, the .NET resilience framework, your application can easily tolerate transient faults and longer outages in remote systems or infrastructure.

At the end of this hour you will know how to use all the features of Polly to make your application a rock solid piece of work. We’ll cover the reactive and proactive resilience strategies, starting with simple but very powerful retries and finishing with bulkhead isolation.

Oh, and did I mention Polly is a .NET Standard library so it will work on any application you can think of!

Join me for an hour, and your applications will never be the same.

Link: https://www.youtube.com/watch?v=esEDPtBJz40

Follow @CodeOpinion on Twitter

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Roundup #47: Elastic APM, Miguel de Icaza, Microsoft.FeatureManagement, .NET Core 3 Progress

Here are the things that caught my eye recently in .NET.  I’d love to hear what you found most interesting this week.  Let me know in the comments or on Twitter.

Follow @CodeOpinion on Twitter

Elastic APM .NET Agent beta Released

We are proud to announce the beta release of the Elastic APM .NET agent! The version number of this release is 1.0.0-beta1.

Back in February we announced the alpha release of the .NET APM agent. We received very positive feedback from the community, and we managed to attract lots of users already in this very early stage. Our base NuGet package reached around 20,000 downloads.

Link: https://www.elastic.co/blog/elastic-apm-dot-net-agent-beta-released

Careers Behind the Code: Building Careers and Companies with Open Source with Miguel de Icaza

In this episode, we talk to Miguel de Icaza, Distinguished Engineer about his career building open source software, founding companies, and building communities.

Link: https://www.youtube.com/watch?v=ApLBy0AJBEw

Microsoft.FeatureManagement

In a recent .NET Community Standup, a new library was introduced that’s being built by the Azure team – Microsoft.FeatureManagement. In this post, I give a brief introduction to the library and how to use it in an ASP.NET Core app. This post just covers the basics – in later posts I’ll show some of the ASP.NET Core-specific features, as well as how to create custom feature filters.

Link: https://andrewlock.net/introducing-the-microsoft-featuremanagement-library-adding-feature-flags-to-an-asp-net-core-app-part-1/

.NET Core 3.0 Progress

Link: https://twitter.com/ziki_cz/status/1146553003610202112

Follow @CodeOpinion on Twitter

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.