Yes, you can integrate OpenTelemetry with Azure Monitor without changing all the logger instances by using the OpenTelemetry Logging Integration.
This sample example of how to use logging with open telemetry
- The code below uses OpenTelemetry to integrate with Azure Monitor to collect traces from your application.
- Code reference is taken from MSDOC and OpenTelemetry Django
# Import necessary libraries
import logging
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.instrumentation.django import DjangoInstrumentor
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configure OpenTelemetry to use Azure Monitor with the specified connection string
configure_azure_monitor(
connection_string="AzureApplication Insights connection_string",
)
# Get a tracer for the current module
tracer = trace.get_tracer(__name__)
# Instrument Django app
DjangoInstrumentor().instrument()
# Your Django app code goes here...
# Start a new span with the name "hello"
with tracer.start_as_current_span("hello"):
logger.info("Hello, World!")
# Wait for export to take place in the background
input()
Using the OpenTelemetry Logging Integration:
- The code below acts as a bridge between OpenTelemetry and spans your existing logging infrastructure. It captures span information and attaches it to the logs without modifying existing logging calls.
import logging
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configure OpenTelemetry to use Azure Monitor with the specified connection string
configure_azure_monitor(
connection_string="AzureApplication Insights connection_string",
)
# Get a tracer for the current module
tracer = trace.get_tracer(__name__)
# Instrument Django app
DjangoInstrumentor().instrument()
# Instrument logging to capture spans
LoggingInstrumentor().instrument()
# Your Django app code goes here...
# Start a new span with the name "hello"
with tracer.start_as_current_span("hello"):
logger.info("Hello, World!")
# Wait for export to take place in the background
input()


For exception in the Azure VM Resource Detector:
settings.py
import logging
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configure OpenTelemetry to use Azure Monitor with the specified connection string
configure_azure_monitor(
connection_string="AzureApplication Insights connection_string ",
resource_detectors=[],
)
# Get a tracer for the current module
tracer = trace.get_tracer(__name__)
# Instrument Django app
DjangoInstrumentor().instrument()
# Instrument logging to capture spans
LoggingInstrumentor().instrument()
if middleware present:
# settings.py
MIDDLEWARE = [
' Replace with MiddlewareName',
# Other middleware entries...
]
# views.py
from django.shortcuts import render
from opentelemetry import trace
import logging
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
tracer = trace.get_tracer(__name__)
def example_view(request):
with tracer.start_as_current_span("example_view"):
# Your view code...
logging.LoggerAdapter.info("Hello from views.py")
return render(request, 'example_template.html')
