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
import json
import logging
from opencensus.extension.azure.functions import OpenCensusExtension
from opencensus.trace import config_integration
from opencensus.trace.samplers import AlwaysOnSampler
from opencensus.trace.tracer import Tracer
logger = logging.getLogger('HttpTriggerLogger')
OpenCensusExtension.configure()
config_integration.trace_integrations(['logging'])
logging.basicConfig(format='%(asctime)s traceId=%(traceId)s spanId=%(spanId)s %(message)s')
tracer = Tracer(sampler=AlwaysOnSampler())
logger = logging.getLogger(__name__)
logger.warning('Before the span')
with tracer.span(name='hello'):
logger.warning('In the span')
logger.warning('After the span')
def main(req, context):
logger.info('Executing HttpTrigger with OpenCensus extension')
# You must use context.tracer to create spans
with context.tracer.span("parent"):
logger.info('Message from HttpTrigger')
return json.dumps({
'method': req.method,
'ctx_func_name': context.function_name,
'ctx_func_dir': context.function_directory,
'ctx_invocation_id': context.invocation_id,
'ctx_trace_context_Traceparent': context.trace_context.Traceparent,
'ctx_trace_context_Tracestate': context.trace_context.Tracestate,
})
I can see the metrics in Azure monitor
OpenCensus and OpenTracing have currently merged into OpenTelemetry. You can have a look here