question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django server not responding with Azure Application Insights connector

See original GitHub issue

Describe your environment.

Running on WSL2 Ubuntu 20.04.

Django = "^4.0.5"
opencensus-ext-azure = "^1.1.4"

Steps to reproduce.

  1. Create a new Django project and app
django-admin startproject '<project_name>'
django-admin startapp 'myapp'
  1. Create an Application Insights resource on Azure and add the logging configuration to settings.py:
CONNECTION_STRING = 'InstrumentationKey=xxxx-xxx-xx-xx....'

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "azure": {
            "level": "DEBUG",
            "class": "opencensus.ext.azure.log_exporter.AzureLogHandler",
            "connection_string": CONNECTION_STRING
        },
        "console": {
            "class": "logging.StreamHandler",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console", "azure"],
        },
    },
}

What is the expected behavior?

Server should respond to requests on 8000 and logs should be sent to Insights.

What is the actual behavior?

Server will not respond when started: python manage.py runserver. Logs are not sent.

June 20, 2022 - 13:39:47
Django version 4.0.5, using settings 'insights.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

# After the server is up
$ telnet 127.0.0.1 8000
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

Workaround 'disable_existing_loggers': True doesn’t work. The HTTP server responds, but Insights is not updated with metrics.

Additional context.

This issue was talk about in #904 but it was not resolved. I’m opening this issue to find what the cause really is.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
epomatticommented, Jun 21, 2022

@dsv-mckinsey

You need to create a custom logger. Here is what worked for me:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "azure": {
            "level": "DEBUG",
            "class": "opencensus.ext.azure.log_exporter.AzureLogHandler",
        },
        "console": {
            "class": "logging.StreamHandler",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console"],
        },
        # Create a custom logger right here
        'azure': {
            'handlers': ['azure'],
            'level': 'INFO',
        },
    },
}
0reactions
lzchencommented, Jun 21, 2022

@epomatti It is recommended usage to not use the root logger because the azure exporters themselves log messages when an exception occurs. All of the examples that send log data use custom loggers. This is already in the official docs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django example not working · Issue #94623 - GitHub
When I remove only the "azure" handler, the HTTP port starts responding again, but then logs are not sent to insights. "loggers": {...
Read more >
Django + Azure Application Insights - Stack Overflow
i'm trying to embed Azure's Application Insights in a dockerized Django/AngularJS environment to cover the data flow from end to end.
Read more >
Diagnose exceptions in web apps with Application Insights
Open the app solution in Visual Studio. Run the app, either on your server or on your development machine by using F5 ....
Read more >
Track incoming requests with OpenCensus Python
Monitor request calls for your Python apps via OpenCensus Python. ... The data is then sent to Application Insights under Azure Monitor as ......
Read more >
Troubleshoot Bad Gateway errors - Azure Application Gateway
Solution · Check UDR associated with the application gateway subnet. Ensure that the UDR isn't directing traffic away from the backend subnet.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found