djangoazure-application-insightsopencensus

Export Django logs to Azure AppInsights with OpenCensus


I'm following this guidance for Django and Azure. I'm able to get dependancies and requests, but not traces.

I added this to middleware:

'opencensus.ext.django.middleware.OpencensusMiddleware'

Here is the LOGGING and OPENCENSUS portions of settings.py

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'default': {
        'format': '%(asctime)s - %(levelname)s - %(processName)s - %(name)s\n%(message)s',
    },
},
"handlers": {
    "azure": {
        "level": "DEBUG",
    "class": "opencensus.ext.azure.log_exporter.AzureLogHandler",
        "instrumentation_key": assert_env('APPINSIGHTS_INSTRUMENTATIONKEY'),
     },
    "console": {
        "level": "DEBUG",
        "class": "logging.StreamHandler",
        "formatter": "default",
     },
  },
"loggers": {
    "logger_name": {"handlers": ["azure", "console"]},
},
    # For some reason, this is needed or logging doesn't show up in the
    # celery log file.
'skyforge.tasks': {
    'handlers': ['azure','console'],
    'level': assert_env('DJANGO_LOG_LEVEL'),
},

}

OPENCENSUS = {
    'TRACE': {
        'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1)',
        'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter(
            service_name='skyforge'
        )'''
        #Assumes Environmental Variable 'APPINSIGHTS_INSTRUMENTATIONKEY'
    }
}

Any guidance on where to look for why no trace logs. The django-critical and django-tasks are still going to the console.


Solution

  • This is the part that is wrong:

    "loggers": {
    "logger_name": {"handlers": ["azure", "console"]},
    

    The "logger_name" needed to be populated with the App name so that logger = logging.getLogger(__name__) worked properly.

    Also a level is needed in the logger.

    Here is what worked for me.

    LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s - %(levelname)s - %(processName)s - %(name)s\n%(message)s',
        },
    },
    'handlers': {
         'azure': {
            'class': 'opencensus.ext.azure.log_exporter.AzureLogHandler',
            'formatter':'default'
            # https://pypi.org/project/opencensus-ext-azure/
            #Assumed ENV APPLICATIONINSIGHTS_CONNECTION_STRING
          },
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'default',
    
        },
      },
    'loggers': {
        'polls': {
            'handlers': ['azure', 'console'],
            'level':'DEBUG'
            },
    }
    

    }

    The following options did not make a difference: 'level' parameter in handler 'filter':'[]' in handler 'stream':sys.stdout in handler.

    I put the Appinsights connection string in an environmental variable, but the reference shows how to place in settings also. Also the original key in settings seems to work also.

    APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentaionKey=<key uuid>