3

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 3

3

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)
Sign up to request clarification or add additional context in comments.

7 Comments

thanks, I know that part (and am using it in another, non-Function app. But ideally I don't want to add another logger, since there is already the logger available in the Function. I'm afraid doing so, I would probably lose any correlation
this Github discussion actually produced a working example which is not too far away from yours. If you want to amend your answer, I'll mark it as accepted github.com/Azure/azure-functions-python-worker/issues/…
@silent, included it in the answer. If it's helpful, please accept it as answer:).
@ivan-young sorry, I was happy a bit too early... it turns out the correlation doesnt work properly with that code snippet. I'm currently in contact with support/PG to sort that out
@silent thanks for pointing this out. Let's wait for the response from support team:).
|
1

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

0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.