Python Application Hosted On Docker(Azure Container Apps) Intergration with App Insights

Kanav Sharma 25 Reputation points
2025-01-10T15:10:17.9833333+00:00

I have hosted my python application on azure container app . I want to get metrics and logs in azure app insights. How can I achieve that?

Azure Container Apps
Azure Container Apps
An Azure service that provides a general-purpose, serverless container platform.
550 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Pinaki Ghatak 5,575 Reputation points Microsoft Employee
    2025-01-13T10:04:10.3666667+00:00

    Hello @Kanav Sharma

    To integrate your Python application hosted on Azure Container Apps with Azure Application Insights, you can follow these steps:

    1. Create an Application Insights resource in your Azure subscription if you haven't already done so.
    2. 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.
    3. Import the necessary modules in your Python application:
    from opencensus.ext.azure.log_exporter import AzureLogHandler
    import logging
    
    1. 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())
    
    1. Add logging statements to your Python application:
    logger.warning("I will be sent to Application Insights")
    
    1. Deploy your Python application to Azure Container Apps.
    2. In the Azure portal, navigate to your Application Insights resource and select "Logs" from the left-hand menu.
    3. 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".
    4. 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.
    0 comments No comments

  2. Shireesha Eeraboina 1,720 Reputation points Microsoft Vendor
    2025-01-16T05:23:13.6433333+00:00

    Hi @Kanav Sharma ,

    Thanks for posting your question in Microsoft Q&A, apologize for any inconvenience caused on this.

    To send logs from your xyz.log file inside a Docker container to Azure Application Insights, you can follow these steps:

    In the Azure portal, click on "Create a resource" and search for "Application Insights" and click "Create" and fill in the required details such as the resource name, subscription, resource group, and region then click "Review + create" and then "Create", Also ensure your Docker container is set up to send logs to Application Insights and You can use the opencensus-ext-azure package for Python applications, as mentioned earlier.

    • In the Azure portal, search for "Log Analytics workspaces" and create a new workspace and link your Application Insights resource to this workspace by navigating to your Application Insights resource, selecting "Properties", and then selecting the Log Analytics workspace you created then navigate to your Docker container's resource in the Azure portal under "Monitoring", select Diagnostic settings and click on "Add diagnostic setting" and choose the logs you want to send to Application Insights then select your Application Insights resource as the destination.
    • At last, navigate to your Application Insights resource and select "Logs" from the left-hand menu to query and view your logs, for your reference use the following query to view your logs:
       

    traces

    | where message contains "your log message"

    Also, you are trying to fetch live metrics from docker container/azure container app to App Insights so for these please refer the below documents:

    https://learn.microsoft.com/en-us/azure/container-apps/metrics

    https://learn.microsoft.com/en-us/azure/azure-monitor/containers/container-insights-livedata-overview

    I hope the above provided information might help you, if you have any further queries, please feel free to reach out to us.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.