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.
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).
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.
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.
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:
services.AddHealthChecks()
.AddCheck<SampleHealthCheck>("Sample")
.AddCheck<Sample2HealthCheck>("Sample2");
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With