Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions and IHealthCheck

I need to implement the Health Check for Azure Functions.

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-3.0

But, in my case instead of using NETCORE 3.0 we need to implement it in NETCORE 2.2

Our main problem is the startup class which inherits from the FunctionsStartup which it is quite different from MVC API startup. Thus, the following code could not be implemented in the Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {            
            //Readiness check
            var port = int.Parse(Configuration["HealthManagementPort"]);
            app.UseHealthChecks("/ready", port, new HealthCheckOptions()
            {
                Predicate = (check) => check.Tags.Contains("ready"),
            });
            app.UseHealthChecks("/live", port, new HealthCheckOptions()
            {
                //Exclude all checks and return 200-OK
                Predicate = (_) => false,
            });

        }

Have any anyone faced something similar? How can I implement similar behavior?

Thanks.

like image 453
Gustavo Ferrero Avatar asked Oct 26 '19 19:10

Gustavo Ferrero


People also ask

What is the difference between Azure function and Web API?

Azure Functions are always static methodsActions of Web API, with the aid of using the way, don't have the static modifier. This outcome in a good-sized architectural extrude at some stage in the migration, specifically with dependency injection (DI).

What is the difference between Azure functions and App Service?

Azure App Service and Azure Functions considerations for multitenancy. Azure App Service is a powerful web application hosting platform. Azure Functions, built on top of the App Service infrastructure, enables you to easily build serverless and event-driven compute workloads.

What is Azure functions used for?

Azure Functions is a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Instead of worrying about deploying and maintaining servers, the cloud infrastructure provides all the up-to-date resources needed to keep your applications running.


2 Answers

app.UseHealthChecks("/ready", port, new HealthCheckOptions()
          {
                Predicate = (check) => check.Tags.Contains("ready"),
          });

UseHealthChecks is an extension method that registers HealthCheckMiddleware which invoke a method called CheckHealthAsync in HealthCheckService class

In order to acheive that in Azure functions you need to do the following:

  1. Add Microsoft.Extensions.Diagnostics.HealthCheck nuget package.
  2. On your Startup.cs class register your health checks
   services.AddHealthChecks()
                .AddCheck<SampleHealthCheck>("Sample")
                .AddCheck<Sample2HealthCheck>("Sample2");
  1. Create Http trigger function and inject HealthCheckService class
public class GetReady
    {
        private readonly HealthCheckService _healthCheckService;

        public GetReady(HealthCheckService healthCheckService)
        {
            _healthCheckService = healthCheckService;
        }

        [FunctionName(nameof(GetReady))]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "ready")]
            HttpRequest req,
            CancellationToken cancellationToken)
        {
            var result = await _healthCheckService.CheckHealthAsync((check) => check.Tags.Contains("ready"), cancellationToken);
            return new OkObjectResult(result);
        }
    }

And your endpoint should work same as aspnet core.

like image 152
Mahmoud Salah Avatar answered Sep 22 '22 02:09

Mahmoud Salah


You can use Health Monitor Feature of Azure function if you are using Consumption based plan.

The Host Health Monitor feature of the Functions Runtime monitors various VM sandbox imposed performance counters. The goal is to temporarily stop the host from doing more work when thresholds for any of the counters are about to be exceeded. This allows the host to avoid hitting hard sandbox limits which could cause a hard shutdown, and also allows the host to gracefully complete in-progress work while waiting for the counters to return to normal limits.

You can read more here in detail:

https://github.com/Azure/azure-functions-host/wiki/Host-Health-Monitor

Alternatively you can use application insight for doing health check.You can check these blogs for detailed instruction:

https://zimmergren.net/azure-functions-scheduled-trigger-not-firing-application-insights-monitoring/

How can you create an API Health check in azure?

Hope it helps.

like image 26
Mohit Verma Avatar answered Sep 20 '22 02:09

Mohit Verma