简体   繁体   中英

How to override a Trace provider in Azure Function (Python)

I have Azure function (Python), for which i need calculate the entire time of execution of function and some part of execution ie few lines of function

exporter = AzureMonitorTraceExporter.from_connection_string("<ConnectionString>")
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
span_processor = BatchSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
with tracer.start_as_current_span(name="TotalFunctionConnection"):
       #Some Code starts 
       with tracer.start_as_current_span(name="CodeConnection"):
           #SomeCode Starts

if I use this, its throwing error as, you cant overwrite trace provider.

Can anyone help me on this?

"you can't overwrite trace provider."

If you are using a nested Trace provider span , you need to provide the alias for each specific span . If you did not specify the alias in a nested span, you will get the conflict from the Python interpreter like you cannot overwrite the trace provider .

Instead of the below line

#without using Alias
with tracer.start_as_current_span(name="TotalFunctionConnection"):

You can use like this below to access the specific span/nested span

# using Alias
with tracer.start_as_current_span(name="TotalFunctionConnection")as TotalFunCon:

Please follow the below workaround to fix the issue

Here I am using the HTTP trigger to create an Opentelemetry trace to calculate the time of execution

from  azure.core.settings  import  settings
from  azure.core.tracing.ext.opentelemetry_span  import  OpenTelemetrySpan
settings.tracing_implementation = OpenTelemetrySpan

from opentelemetry import  trace
from  opentelemetry.sdk.trace  import  TracerProvider
from  opentelemetry.sdk.trace.export  import  BatchSpanProcessor
from  azure.monitor.opentelemetry.exporter  import  AzureMonitorTraceExporter

exporter = AzureMonitorTraceExporter.from_connection_string("<Applicaiton Insights Connection String>")

def  main(req: func.HttpRequest) -> func.HttpResponse:

    logging.info('Python HTTP trigger function processed a request.')
      
    trace.set_tracer_provider(TracerProvider())
    tracer = trace.get_tracer(__name__)
    span_processor = BatchSpanProcessor(exporter)
    trace.get_tracer_provider().add_span_processor(span_processor)
    
    with  tracer.start_as_current_span(name="parent") as  span:
        logging.info("parent span context Before seting Attribute:"+ str(span.get_span_context()))
        span.set_attribute("parent span test","Parent span")

        with  tracer.start_as_current_span(name="Child") as  childspan:
            logging.info("child span context Before seting Attribute:"+ str(span.get_span_context()))
            childspan.set_attribute("child span test","Child span")

    logging.info("Parent span context:" + str(span.get_span_context()))
    logging.info("Child span context:"+ str(childspan.get_span_context()))

    return  func.HttpResponse(
        "This HTTP triggered function executed successfully.\n Parent_Span:)" + str(span.get_span_context())+ "\n child_Span:" + str(childspan.get_span_context()) + "\n Pass a name in the query string or in the request body for a personalized response.",
        status_code=200
    )

Result

在此处输入图像描述

Refer Creating Span in an Opentelemetry .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM