skip to Main Content

Using the Opentelemetry exporter for azure monitor. I am running the hello world trace found here. It shows up as a ‘dependency’ in app insights instead of a trace. I have tried numerous implementations, but everything shows as a dependency, regardless of if it is a web request or local code execution. Any help is appreciated, there is clearly something I don’t understand. I cannot figure out how to create traces.

"""
An example to show an application using Opentelemetry tracing api and sdk. Custom dependencies are
tracked via spans and telemetry is exported to application insights with the AzureMonitorTraceExporter.
"""
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
tracer = trace.get_tracer(__name__)
# This is the exporter that sends data to Application Insights
exporter = AzureMonitorTraceExporter(
    connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
span_processor = BatchSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

with tracer.start_as_current_span("hello"):
    print("Hello, World!")

# Telemetry records are flushed automatically upon application exit
# If you would like to flush records manually yourself, you can call force_flush()
tracer_provider.force_flush()

2

Answers


  1. The code you have taken from Microsoft-Document clearly states that:

    an application using Opentelemetry tracing api and sdk. Custom dependencies are tracked via spans and telemetry is exported to application insights with the AzureMonitorTraceExporter.

    The code you have used will send data to dependencies only, in comments you can clearly see that you are creating custom dependencies that is why you will get something like this:

    enter image description here

    You cannot log anything in traces with just print statement, you should log using Logger and its handler like below and I have followed SO-Thread and Microsoft-Document modified code a bit:

    import logging
    from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
    from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter
    
    from opentelemetry._logs import (
        get_logger_provider,
        set_logger_provider,
    )
    from opentelemetry.sdk._logs import (
        LoggerProvider,
        LoggingHandler,
    )
    set_logger_provider(LoggerProvider())
    chotu_exporter = AzureMonitorLogExporter(
        connection_string="InstrumentationKey=4ad5eee20;IngestionEndpoint=https://centralindia-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralindia.livediagnostics.monitor.azure.com/"
    )
    get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(chotu_exporter))
    
    chotu_handler = LoggingHandler()
    chotu_logger = logging.getLogger(__name__)
    chotu_logger.addHandler(chotu_handler)
    chotu_logger.setLevel(logging.INFO)
    
    chotu_logger.info("Rithwik Bojja from Log INFO")
    chotu_logger.warning("Rithwik Bojja from Log WARNING ")
    chotu_logger.error("Rithwik Bojja from Log ERROR")
    

    Output:

    enter image description here

    Login or Signup to reply.
  2. Please read this document first to instrument with the OpenTelemetry Azure monitor exporter: https://learn.microsoft.com/en-us/python/api/overview/azure/monitor-opentelemetry-exporter-readme?view=azure-python-preview. Specifically, look at this section: https://learn.microsoft.com/en-us/python/api/overview/azure/monitor-opentelemetry-exporter-readme?view=azure-python-preview#logging-experimental-1 on how to create logs (traces table in AppInsights).

    Also see this table: https://learn.microsoft.com/en-us/previous-versions/azure/azure-monitor/app/opencensus-python#telemetry-type-mappings to see how OpenTelemetry pillars of observability map to Az

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search