2

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)
Abishek Kumaresan
  • 107
  • 1
  • 4
  • 15
  • If you are actually using _Azure Functions_ as a tag suggests, this may help: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=azurecli-linux%2Capplication-level#log-custom-telemetry – Anatoli Beliaev Jan 10 '22 at 07:21
  • @AnatoliBeliaev Thanks, I saw that documentation, created PYTHON_ENABLE_WORKER_EXTENSIONS environment variable in azure function configuration but still I could get the trace_id being logged Moreover in that example I need to create a span, is it possible to just add trace_id and query using customDimensions['trace_id'] == I am appending the trace_id to the response header for tracing for tracing purpose, – Abishek Kumaresan Jan 10 '22 at 09:35
  • I see an other possibility but I am not able to implement using python If I could just access the operationId of the particular request and use that to trace the request but operationId is not expose, invocationId is exposed in the context will invocationId be a part of all log entries corresponding to that request ? If so I can use that – Abishek Kumaresan Jan 10 '22 at 09:38

1 Answers1

0

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

enter image description here

Refer here Doc 1 & Doc 2

OpenCensus and OpenTracing have currently merged into OpenTelemetry. You can have a look here

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15
  • As pointed out by @AnatoliBeliaev learn.microsoft.com/en-us/azure/azure-functions/… should be used for Azure functions, Refer https://github.com/census-ecosystem/opencensus-python-extensions-azure/issues/8 for details of why I faced the issue. – Abishek Kumaresan Jan 13 '22 at 10:27