0

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)

1 Answer 1

0

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

8
  • I just add this to the django settings.py but I'm getting "Exception in detector <opentelemetry.resource.detector.azure.vm.AzureVMResourceDetector object at 0xffff7b643700>, ignoring" this error. Additionally, when I call logger from another python file (i.e. src/apps/product/views.py) logger = logging.getLogger(__name__) and logger.info("hello"), I'm unable to see in azure. @Sampath
    – SerkanB
    Nov 21 at 12:58
  • added code for exception in the Azure VM Resource Detector please check
    – Sampath
    Nov 21 at 14:02
  • Still getting error. When I checked ur view.py, I saw tracer, do I have to add it? If I have to as I mentioned in my problem I have to change it for all views. @sampath
    – SerkanB
    Nov 21 at 14:34
  • Yes need to add view.py
    – Sampath
    Nov 21 at 14:38
  • Is there any development for the future because in opencensus I'm able to do it by adding 'azure':{ 'level': 'INFO', 'filters': ['require_debug_false'], 'class': 'opencensus.ext.azure.log_exporter.AzureLogHandler', 'formatter': 'azure_verbose' }, to the LOGGING dict on settings.py? @sampath
    – SerkanB
    Nov 21 at 14:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.