How can you create an API Health check in azure?

北城以北 提交于 2020-08-08 05:38:06

问题


What is the best way to test API endpoints within azure? I am looking to get alerted if an endpoint is not working.


回答1:


Take a look at webtest feature in Application Insights.

https://docs.microsoft.com/en-us/azure/application-insights/app-insights-monitor-web-app-availability




回答2:


I suggest you create a WebJob to test your API endpoints. In your WebJob, you could use a TimerTrigger to run the test function timely(For example, every 2 minutes).

To use TimerTrigger, you need to install Microsoft.Azure.WebJobs.Extensions package using NuGet. After that, you could configure the WebJob to use the timer extension using following code.

static void Main()
{
    var config = new JobHostConfiguration();
    //Configure WebJob to use TimerTrigger
    config.UseTimers();
    var host = new JobHost(config);
    // The following code ensures that the WebJob will be running continuously
    host.RunAndBlock();
}

In the function, you could send a request to your Web API. If you can't get the response from the server or the response status is not equal to 200 OK, it means that the Web API is not useable.

public static void StartupJob([TimerTrigger("0 */2 * * * *", RunOnStartup = true)] TimerInfo timerInfo)
{
    WebRequest request = WebRequest.Create("URL of your api");
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    if (response == null || response.StatusCode != HttpStatusCode.OK)
    {
        //API is not useable
    }
}



回答3:


you can write a custom Azure Function to report Telemetry to Application Insight. See: https://github.com/rbickel/Azure.Function.AppInsightAvailabilityTest



来源:https://stackoverflow.com/questions/43720824/how-can-you-create-an-api-health-check-in-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!