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
The code you have taken from Microsoft-Document clearly states that:
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:
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:
Output:
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