Search code examples
djangoazureazure-application-insightsopen-telemetry

How can I use Azure AppInsights with OpenTelemetry in Django


Hi I have a Django app and I wanted to connect my traditional logs with Azure AppInsights.

I'm using logging lib for my logs and this piece of code occurs in 100 times in the project.

import logging 
logger = logging.getLogger(__name__) 

When I checked the open telemetry Azure docs I found the following example but to use this one I have to change all the logger instances because we need to add handler for open telemetry and an exporter for Azure. Is there any way to do it without changing all the logger occurrences?

logger_provider = LoggerProvider()
set_logger_provider(logger_provider)
exporter = AzureMonitorLogExporter.from_connection_string(
    os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter, schedule_delay_millis=60000))

# Attach LoggingHandler to namespaced logger
handler = LoggingHandler()
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)


Solution

  • 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()
    
    

    enter image description here

    enter image description here

    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')
    

    enter image description here