azure-functions-python-worker
azure-functions-python-worker copied to clipboard
logging.exception does not create entry in exceptions table
When logging.exception("Simple Exception") then it should show up in the exceptions
table in App Insights. It only shows up when it is explicitly raised.
If we want to show 403,404 as an "exception from the app's point of view" and correlate with requests etc currently the app insights will only show failures that explicitly raised an exception. If a valid HttpResponse is sent with 403, it will be shown as successful request because the function executed successfully. But from the app's point of view, if it needs a simple correlation between requests/traces and exceptions, the workaround is to use logging.exception and assign it a error for ease of querying.
But the logging.exception when used outside of a try/catch context in Python functions does not log the exception inside exceptions
table in app insights.
Here is a sample code that can be used to test this:
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse: logging.exception("Before exception logging") try: raise ValueError("Oops. something went wrong") except ValueError as err: logger.exception("Simple exception logging:" + str(err)) #raise #HTTP 500 kill -> failed.
In this sample "Before exception logging" statement does not show up in the exceptions table traces | where message contains "Before"
exceptions | where message contains "Before"
Overall this would have been an easier stopgap method to correlate exceptions in Python as it does not have a provision to override the custom telemetry model offered by App Insights.
It seems that logging exceptions has never been fully implemented...
https://github.com/Azure/azure-functions-python-worker/blob/dev/azure_functions_worker/dispatcher.py#L211-L215
Seems to have been reported previously https://github.com/Azure/azure-functions-python-worker/issues/795.
The application insights integration really would be a lot better if exceptions and stack traces were properly reported to application insights. Without support in the Python worker, we'd have to implement our own logging integration with App Insights to ensure exceptions are correctly reported.
Bumping this up, also an ask from https://github.com/Azure-Samples/azure-monitor-opencensus-python/pull/7/files
@stefanushinardi The code I pointed to seems to be the root problem. Other language runtimes don't have this problem. Is there anyway we can accelerate a fix? Should I open a support ticket? Not being able to log exceptions is a really big miss.
This issue is still relevant and painful. A straights forward non-azure way to fix this is to use " opencensus.ext.azure.log_exporter import AzureLogHandler", but this do create double logging(one trace + one exception)...
But the logging.exception when used outside of a try/catch context in Python functions does not log the exception inside exceptions table in app insights.
Does it create exceptions table entry when invoked inside catch block? Just tried and even that does not work and V4 code has no traces for supporting logging.exception