I am trying to add traceId and spanId to logs in azure functions in python, following https://learn.microsoft.com/en-us/azure/azure-monitor/app/correlation#log-correlation in Azure documentation
traceId and spanId is added to log statements in local development using VS Code but I am not able to see the same traceId and spanId in azure monitor,
I followed https://learn.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python#logs section of the documentation to add AzureLogHandler but still things don't seem to work
I want to be able to query the logs in Azure Application insights using the traceId and spanId What is missing in my code so traceId and spanId is not logged in azure monitor
Below is my code to configure logs in python
def logger_and_tracer(name):
config_integration.trace_integrations(["logging", "requests"])
tracer = Tracer(sampler=AlwaysOnSampler())
formatter = logging.Formatter(
"fileName=%(filename)s functionName=%(funcName)s traceId=%(traceId)s spanId=%(spanId)s %(message)s"
)
logger = logging.getLogger(name)
azure_logger = AzureLogHandler()
syslog = logging.StreamHandler()
azure_logger.addFilter(CustomDimensionsFilter(default_log_items))
syslog.addFilter(CustomDimensionsFilter(default_log_items))
azure_logger.setFormatter(formatter)
syslog.setFormatter(formatter)
logger.setLevel(logging.DEBUG)
logger.addHandler(syslog)
logger.addHandler(azure_logger)
return (logger, tracer)
Please follow the below steps to add the Trace ID and span ID in your azure function to view the value of those in Azure monitor.
Add the necessary packages of Opencenus in azure function root folder.
pip install opencensus-extension-azure-functions
pip install opencensus-ext-logging
I can see the metrics in Azure monitor
Refer here Doc 1 & Doc 2
OpenCensus and OpenTracing have currently merged into OpenTelemetry. You can have a look here