How to do health checks for Azure functions and Web Apps.
With Azure Monitor and IaaS via C#.
There were two reasons to write this article, the first one is that basic health checks usually not added in the early stages of projects and not deployed with infrastructure. The second one is the awesome Microsoft Extensions.Diagnostics.HealthChecks is not supported by Azure Functions for a good reason, but there is a native health monitor configuration and walkaround from the community on GitHub.
So I decided to share several approaches that can be used together or independently, but all approaches are super easy to repeat for any project(Azure Portal setup is not an easily repeatable solution). This article is a part of the #AzureSpringClean #AzureFamily community event.
TL;DR; I will cover Azure Monitor health checks that can be deployed along the main infrastructure written via C#(Pulumi), then provide anoverview of Azure Functions health monitor configuration which I`m using and provide usage example of Health checks package for WebApps and link to workaround for Azure Functions
Azure Monitor health checks with infrastructure.
Let’s start with a simple approach that is not related to changes in a solution code, but still needs to be written with C# :).
For this purpose, we can use the Pulumi framework, which I started to adopt recently to increase the adoption of the IaaS approach within developers teams. Writing JSON and YAML file is not fun and usually meet a lot of backpressure from developer teams g the classic response is “we will ask DevOps engineer to do a task”. And the ability to do this task with strongly typed language is usually met with curiosity.
Short introduction Pulumi/C# video below, if you prefer video format.
The first step is to install Pulumi, generate a new project, apply your C# infrastructure code and deploy it to Azure.
- Install Pulumi from https://www.pulumi.com/docs/get-started/install/
- Run in the CMD az login command to get an auth token for a subscription.
- Run pulumi login (you will be prompted to register a new account)
- Run pulumi new azure-csharp to create the new C# app for your infrastructure.
- You can open this project both in VS Code or Visual Studio 2019, I prefer the latter for better IntelliSense support :).
Now let's have a look at our project, Program.cs is a classic entry point for application start, and Stack.cs will contain our infrastructure.
Let's write some infrastructure, I cannot express how cool it is to write a C# code for my Azure infrastructure. Let's examine basic infrastructure with my example.
There a few things to note, at the moment Pulumi has two providers for Azure, the old one based on Terraform and the new one based on Azure Resource Manager API. I will use the new one - Pulumi.AzureNative v0.7 and version 1.0 are due at the end of April.
And another important thing is that Pulumi is not free for teams, but the great news that it is free for personal usage and only 50$ per month for small teams. Which is cheaper than dedicated DevOps engineer hours :). I will also not do the deep dive into why “desired state configuration” is great.
The infrastructure code below contains Azure Functions, Azure SQL.
The only thing is needed to open cmd and run command pulumi up.
There is an option to avoid the command line and make deployments with a C# code, also Pulumi can be integrated with Azure DevOps or GitHub, the command line in this article used for the sake of brevity.
And after selection of the yes option, my resources are online.
And now to the fun part, let's add a basic availability test. While most of the infrastructure is covered by Pulumi, some exotic cases still require XML stuff to work, such as fields in this availability test. And moreover, current documentation sometimes doesn't reflect all needed changes for exotic cases like this alert. But they will improve WebTestPropertiesConfigurationArgs class to handle all configuration fields.
Let's add this test to our infrastructure.
But before I will share the successful deployment screen, let me share what will happen if you misspell something in the XML string above. Not very informative.
But in other cases, linking internal fields of Pulumi objects inside concatenated string can produce meaningful errors :).
So after a few fixes, I deployed the alert and then updated it.
One of many benefits of writing infrastructure via Pulumi/C# is
Azure Functions host health monitor.
In this section, I will cover the magic of the Azure Functions host.json file. If there is a need to configure ingest, scalability of monitoring is a place to go. So configuring the application this way is also a repeatable option, though it can be changed at any time.
I will not repeat official documentation in this article but provide my configuration and few important takeaways from usage. You need to monitor additional parameters of function app host like connection limits and track these errors in connected Application Insights Instance. The Azure Functions runtime in Azure is not aware of any additional logic like HealthCheck middleware outside of functions execution context.
So in order to have proper health checks, you should create a special alert rule(with IaaS code) in Application Insights so it can be triggered with a specific type of event, that contains “Host thresholds exceeded - message”
This solution is only for applications deployed to Consumption Plan.
Host Health Monitor · Azure/azure-functions-host Wiki (github.com)
An alternative workaround(or if you use Premium functions plan) is to use a guide and code from Keith Smith, his approach will use classic NuGet packages from Microsoft along with a custom code wrapper and Azure Function to expose health checkpoint of application.
Adding Health Checks to your Azure Functions (keithmsmith.com)
Azure Web Apps health monitoring.
.NET Core is providing middleware for health checks and reporting of the health of a variety of standard components such as Entity Core Framework DB context, underlying databases, and its super easy to customize health checks for any of your components.
Let's cover this functionality briefly, everything starts with the standard Startup class and health monitoring of standard components. Take for example package:
All you need is to add a health check to services and configure the monitoring endpoint, so you can visit it via the application.
One important notice, this health endpoint can be also configured from the App Service via Azure Portal since August 2020.
Sometimes basic scenarios are enough and if you can create them via easily configurable code and repeating of this code for all you project can be a first major milestone.
But there is more, you might need to monitor the health of your database in Azure, receive additional alerts via different Azure Monitor alerts. In order to write less code you can create a set of alerts for different types in Azure Portal, then export the ARM template and convert them to C# Pulumi via an online converter https://www.pulumi.com/arm2pulumi/.
Or you can embrace the native .NET Core approach via code provided by Microsoft. My take is to go with the infrastructure approach for cloud component and try to delegate this work to developers(not DevOps engineers)
That’s it, thanks for reading. Cheers!
- Adding Health Checks to your Azure Functions (keithmsmith.com)
- Monitor availability and responsiveness of any web site — Azure Monitor | Microsoft Docs
- Health checks in ASP.NET Core | Microsoft Docs
- azure-native.insights.WebTest | Pulumi
- AspNetCore.Docs/aspnetcore/host-and-deploy/health-checks/samples/3.x/HealthChecksSample at main · dotnet/AspNetCore.Docs (github.com)
- Send health pings from your Azure Function applications to ensure Scheduled triggers work as expected (zimmergren.net)
- pulumi/examples: Infrastructure, containers, and serverless apps to AWS, Azure, GCP, and Kubernetes… all deployed with Pulumi (github.com)