Hello @Kanav Sharma
To integrate your Python application hosted on Azure Container Apps with Azure Application Insights, you can follow these steps:
- Create an Application Insights resource in your Azure subscription if you haven't already done so.
- Install the
opencensus-ext-azure
package in your Python application using pip. This package provides an exporter for sending telemetry data to Azure Application Insights. - Import the necessary modules in your Python application:
from opencensus.ext.azure.log_exporter import AzureLogHandler
import logging
- Configure the logger to use the
AzureLogHandler
:
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
# Assumes the environment variable APPLICATIONINSIGHTS_CONNECTION_STRING is already set
logger.addHandler(AzureLogHandler())
- Add logging statements to your Python application:
logger.warning("I will be sent to Application Insights")
- Deploy your Python application to Azure Container Apps.
- In the Azure portal, navigate to your Application Insights resource and select "Logs" from the left-hand menu.
- Run a query to view your Python application's logs:
traces | where message == "I will be sent to Application Insights"
This query will return all log entries with the message "I will be sent to Application Insights". - You can also view metrics for your Python application in Azure Application Insights by using the
opencensus-ext-azure
package to export metrics data. You can find more information on how to do this in the official documentation.