Adding context to Azure log's properties with Python

91 Views Asked by At

I'm deploying a Python service as a service bus queue triggered Azure function, written in Python 3.11.7. The main service works, but I'm having difficulties including information from the service bus message into the logs. Specifically, I want this information stored under the Properties attribute of the log.

Following Microsoft's documentation, I've tried adding the context to a dictionary, and then passing that dictionary as the "custom_dimensions" value within the log's extra argument. Here is my code:

import azure.functions as func
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler

logger = logging.getLogger(__name__)
logger.addHandler(AzureLogHandler())

app = func.FunctionApp()

@app.service_bus_queue_trigger(
    arg_name="azservicebus",
    queue_name="%INPUT_QUEUE%",
    connection="CONNECTION",
)
def servicebus_queue_trigger(azservicebus: func.ServiceBusMessage):
    servicebusmessage = azservicebus.get_body().decode("utf-8")
    identifiers = {'prop__requestId': servicebusmessage['batchId'],
                   'prop__userId': servicebusmessage['userId']}
    properties = {"custom_dimensions": identifiers}
    logger.info("Message received", extra=properties)

I expected this to create a single log for each service bus message with "prop_requestId" and "prop__userId" included within the log's Properties. However, when I run the function, it actually ends up creating 2 logs in Azure's Log Analytics workspaces:

  1. The first log created has the expected message "Message received", and it includes the desired "prop__requestId" and "prop__userId" information under the log's Properties. However, other log fields (e.g., OperationName, OperationId, ParentId") are either blank or populated with all '0's.

  2. The second log created has the expected message "Message received", and other log fields are populated (e.g., OperationName = "servicebus_queue_trigger"). However, it does not include any of the context I am trying to provide.

1

There are 1 best solutions below

0
Ikhtesam Afrin On

However, when I run the function, it actually ends up creating 2 logs in Azure's Log Analytics workspaces.

  • Yes, because the initial log is being sent from function app automatically if you have linked the application insight to it while creating the instance and the later one, by opencensus which you have configured explicitly in the code. This is feasible if your function is getting invoked from function app.

  • I can see only one log when my function is running locally.

  • opencensus generated log will have below sdk type.

enter image description here

  • logs generated from function app automatically will have below sdk type.

enter image description here

  • As per the latest update, OpenCensus will be unsupported by 30 September 2024.

  • Instead you can use azure-monitor-opentelemetry library to add properties to customDimensions.