I am trying to send Exceptions from my Python application running in Azure App service to the designated Azure Application Insights instance. I am using OpenCensus python library for this purpose. The basic logging and exception are successfully reaching to App Insight.
In addition to this i would like to know if there is a way where I can configure the Exception attributes like: problemId
or any other attributes explicitly to reflect specific value for easier alerting (like send email to specific group based on problemId).
Any suggestion/pointers would be super helpful
You can add custom properties to your log messages (not only exception, but all other log types too like trace, event etc.) in the extra keyword argument by using the custom_dimensions
field. These properties appear as key-value pairs in customDimensions
in Azure Monitor. Then you can query, see or configure alert based on that.
For this feature to work, you need to pass a dictionary to the custom_dimensions
field. If you pass arguments of any other type, the logger ignores them.
NOTE: OpenCensus Python doesn't automatically track and send exception telemetry. They're sent through AzureLogHandler by using exceptions through the Python logging library. You can add custom properties just like with normal logging.
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
logger = logging.getLogger(__name__)
# TODO: replace the all-zero GUID with your instrumentation key.
logger.addHandler(AzureLogHandler(
connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000')
)
properties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}
# Use properties in exception logs
try:
result = 1 / 0 # generate a ZeroDivisionError
except Exception:
logger.exception('Captured an exception.', extra=properties)
You can view customDimentions in Azure Portal like below (just example, not actual as per above code):
You can see the same in Kusto Query like below:
And you can query on those and configure alerts based on that (below image shows traces
, for exceptions, it would be exceptions table
):