When using Python (3.8) in Azure Functions, is there a way to send structured logs to Application Insights? More specifically, I'm trying to send custom dimensions with a log message. All I could find about logging is this very brief section.
3 Answers
Update 0127:
It's solved as per this github issue. And here is the sample code:
# Change Instrumentation Key and Ingestion Endpoint before you run this function app
import logging
import azure.functions as func
from opencensus.ext.azure.log_exporter import AzureLogHandler
logger_opencensus = logging.getLogger('opencensus')
logger_opencensus.addHandler(
AzureLogHandler(
connection_string='InstrumentationKey=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee;IngestionEndpoint=https://eastus-6.in.applicationinsights.azure.com/'
)
)
def main(req: func.HttpRequest) -> func.HttpResponse:
properties = {
'custom_dimensions': {
'key_1': 'value_1',
'key_2': 'value_2'
}
}
logger_opencensus.info('logger_opencensus.info Custom Dimension', extra=properties)
logger_opencensus.info('logger_opencensus.info Statement')
return func.HttpResponse("OK")
Please try OpenCensus Python SDK.
The example code is in the Logs section, step 5:
Description: You can also add custom properties to your log messages in the extra keyword argument by using the custom_dimensions field. These properties appear as key-value pairs in customDimensions in Azure Monitor.
The sample:
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 logging statements
logger.warning('action', extra=properties)
7 Comments
Note that the accept answer suggests using the OpenCensus Python SDK which has been retired as of July 2023.
Instead, Microsoft suggest switching across to their OpenTelemetry offering and provide the following migration guidance as well as a guide for how to get started with azure-monitor-opentelemetry .
Comments
I'm not sure if this is what you are asking about, but in my case I wanted to be able to always send a custom_dimension value depending on some initialization configuration, without having to use the extra parameter on each log request. For example to know from what cron job the telemetry was coming from.
I solved it with a filter:
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
logger = logging.getLogger()
handler = AzureLogHandler(connection_string='InstrumentationKey=...')
logger.addHandler(handler)
def add_custom_dimension(e):
e.custom_dimensions = {'job_name': '123'}
return True
logger.addFilter(add_custom_dimension)
logger.error("test")