0

I am trying to setup AI similar to how it is being done here

I am using Python 3.9.13 and following packages: opencensus==0.11.0, opencensus-ext-azure==1.1.7, opencensus-context==0.1.3

My code looks something like this:

import logging
import time
from opencensus.ext.azure.log_exporter import AzureLogHandler


# create the logger
app_insights_logger = logging.getLogger(__name__)

# set the handler 
app_insights_logger.addHandler(AzureLogHandler(
    connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000')
)

# set the logging level
app_insights_logger.setLevel(logging.INFO)

# this prints 'logging level = 20'
print('logging level = ',app_insights_logger.getEffectiveLevel())

# try to log an exception
try:
    result = 1 / 0  
except Exception:
    app_insights_logger.exception('Captured a math exception.')
    app_insights_logger.handlers[0].flush()
    time.sleep(5)

However the exception does not get logged, I tried adding the explicit flush as mentioned in this post

Additionally, I tried adding the instrumentation key as mentioned in the docs, when that didn't work I tried with the entire connection string(the one with the ingestion key)

So,

  1. How can I debug if my app is indeed sending requests to Azure ?
  2. How can I check on the Azure portal if it is a permission issue ?
CCP
  • 1
  • 1

1 Answers1

0

You can set the severity level before logging any kind of telemetry information to Application Insights.

Note: By default, root logger can be configured with warning severity. if you want to add other severity information you have to set like (logger.setLevel(logging.INFO)).

I am using below code to log the telemetry information in Application Insights

import  logging
from  logging  import  Logger
from  opencensus.ext.azure.log_exporter  import  AzureLogHandler  

AI_conn_string= '<Your AI Connection string>'

handler = AzureLogHandler(connection_string=AI_conn_string)  
logger = logging.getLogger()
logger.addHandler(handler) 

#by default root logger can be configured with warning severity.
logger.warning('python console app warning log in AI ')  

# setting severity for information level logging.
logger.setLevel(logging.INFO)
logger.info('Test Information log')
logger.info('python console app information log in AI')
    
try:
    logger.warning('python console app Try block warning log in AI')
    result = 1 / 0
except  Exception:
    logger.setLevel(logging.ERROR)
    logger.exception('python console app error log in AI')

Results

Warning and Information log enter image description here

Error log in AI enter image description here

  1. How can I debug if my app is indeed sending requests to Azure ?

We cannot debug the information whether the telemetry data send to Application Insights or not. But we can see the process like below enter image description here

  1. How can I check on the Azure portal if it is a permission issue ?

Instrumentation key and connection string has the permission to access the Application Insights resource.

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15