Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function should log or throw exception on error?

I have Azure Function (Http trigger) that can fail. I also have Application Insights configured for it.

In case of error (Which is better):

  1. Catch exception, wrap with more info and rethrow it. (Sends 500 response) OR
  2. Catch exception, log it, wrap it, rethrow it to caller. (Sends 500 response) OR
  3. Catch exception, log it (Don't throw it), Manual send 500 response.

Application Insight is able log exceptions. I don't really see point in logging error and throwing exception at the same time.

What are the guidelines? What is good practise?

like image 772
Hooch Avatar asked Nov 12 '19 13:11

Hooch


People also ask

Where do azure function logs go?

In the path of Storage Account > File Shares (under Data Storage) > Your Function App > LogFiles > Application > Functions > Function > Your Function Name > You can see the console result (Application Logs - Execution results/failed result/error data) of the function.

How do I check Azure error logs?

To view streaming logs in the portal, select the Platform features tab in your function app. Then, under Monitoring, choose Log streaming. This connects your app to the log streaming service and application logs are displayed in the window. You can toggle between Application logs and Web server logs.

Do Azure Functions have a timeout?

The Azure Function Timeout is difference depending on which hosting method / pricing tier is used to host an Azure Function App. While in the Consumption plan, the default timeout is 5 minutes, there is a different default and maximum timeouts for the Premium and Dedicated pricing tiers.

What is HTTP trigger in Azure function?

The HTTP trigger lets you invoke a function with an HTTP request. You can use an HTTP trigger to build serverless APIs and respond to webhooks. The default return value for an HTTP-triggered function is: HTTP 204 No Content with an empty body in Functions 2.

How do I log exceptions in Azure Functions?

You need to be able to tell when your Azure Functions apps cause problems. And in order to monitor for errors, you need to be able to log them! In a web server-hosted process, you would normally connect some kind of global logger to catch and log exceptions. You would use that same logger to log from your code.

How do I monitor for errors in Azure Functions?

You need to be able to tell when your Azure Functions apps cause problems. And in order to monitor for errors, you need to be able to log them! In a web server-hosted process, you would normally connect some kind of global logger to catch and log exceptions.

How do I send exceptions to Raygun from Azure Functions apps?

1) Add the Raygun client package to your function code. 2) Add the code to create a client. 3) Send the exception. This is the most basic way to send exceptions to Raygun from your Azure Functions apps. Of course, you can take things up a few levels from here. Let’s take a look at some things you can do to improve on this approach. 1.

Why is error handling important in Azure Functions?

Thank you. Handling errors in Azure Functions is important to avoid lost data, missed events, and to monitor the health of your application. This article describes general strategies for error handling along with links to binding-specific errors.


1 Answers

From my experience which is building an HTTP triggered azure function with Python, I noticed that: When handling exceptions and then manually return something to the client you do have control over the function returned value but the framework marks the function's job as 'Succeeded', which then makes it more difficult to be tracked & monitored in the Application Insights.

For your question, I ended up with a catch clause that mixes 1 & 3 i.e a hybrid approach where I try to identify the origin of the exception, then for exceptions from my code I'm wrapping it and return 500, and for other exceptions that originated from code that I'm using I re-throw them and let the azure function framework return a 500 and to mark the function's job as 'Failed'.

like image 123
ben cohen Avatar answered Oct 18 '22 01:10

ben cohen