dotnet / dotnet/aspnetcore

Health Checks - Open Up for Use In Azure Functions

Open
#26,680 5 comments 0 reactions 0 assignees View on GitHub
affected-few area-healthchecks enhancement severity-major
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is your feature request related to a problem? Please describe.

I like the health checks framework and all of the existing health check probes and would like to leverage it within the Azure Function Apps that we build at my company.

Unfortunately, when the ```AddHealthChecks()``` extension method is called on the ```IServiceCollection``` of the ```IFunctionsHostBuilder```, it is not only registering the ```DefaultHealthCheckService```, but it is also registering an ```IHostedService``` called ```HealthCheckPublisherHostedService```.

This is of course to support the health check publishers, but Azure Functions currently does not support that service and proactively fails on startup. (screenshot below)

![image](https://user-images.githubusercontent.com/177508/95395546-e3046a00-08cc-11eb-9276-b65a0232dfc8.png)

I would still like to leverage the probes without publishers, but the framework does not have a way to toggle that when adding the health checks.

### Describe the solution you'd like
I have created a sample project [here](https://github.com/toddmeinershagen/Demo.AzFuncWithHealthChecks) that shows how the health checks framework can be used along with a simple ```HealthCheckFunction``` class.

In order to solve this, I was forced to not only rewrite the ```HealthCheckServiceCollectionExtensions```, but also duplicate all of the internal classes (```DefaultHealthCheckService```, ```HealthCheckLogScope```, ```HealthChecksBuilder```) because they were not accessible outside of the asp.net core framework.

A few options to consider:

1. Make the ```DefaultHealthCheckService``` and ```HealthChecksBuilder``` classes public so that at least only one extensions method is needed.
2. Create an overload of the ```AddHealthChecks()``` method that allows for an optional parameter to specify whether to add the ```HealthCheckPublisherHostedService``.

```csharp
public static IHealthChecksBuilder AddHealthChecks(this IServiceCollection services, bool useHealthCheckPublishers = true)
```
3. Create an additional extension method ```AddHealthChecksWithoutPublishers()``` that does a similar behavior.

```csharp
public static IHealthChecksBuilder AddHealthChecksWithoutPublishers(this IServiceCollection services)
```
4. Perhaps work with the Azure Functions team to make the ```IHostedService``` available for assignment. Then we could leverage the health checks framework out-of-the-box. (There may be good reason for this not to exist - especially for consumption-based functions)

Some discussion about making ```IHostedService``` available in Azure Functions can be found [here](https://stackoverflow.com/questions/59947132/ihostedservice-usable-in-azure-functions-app) and [here](https://github.com/Azure/azure-functions-host/issues/5447#issuecomment-575368316).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.