Configure Application Insights to receive monitoring information from your applications
You’ve set up monitoring for the Azure Container Apps environment that hosts your application. However, you also want to get information from the applications themselves—both to determine how well they’re running and to potentially detect anomalous activity. To track this application-specific monitoring data, you‘ll use Application Insights, a feature of Azure Monitor.
In the following steps, you’ll create an Application Insights resource and enable application monitoring for each of your applications. You won’t have to change anything in the applications themselves, since you can use the Java autoinstrumentation feature of Application Insights, which applies an agent-based approach to collect the monitoring data.
However, to add the required monitoring agents to your apps, you’ll need to update your deployed applications to include an Application Insights JAR file. This will involve modifying your containers by using an updated Dockerfile and then redeploying your apps. To make this happen, you will:
- Create the Dockerfile for containerizing your applications.
- Add the Application Insights JAR file to your Dockerfile.
- Add an environment variable to your applications with the connection string info for your Application Insights instance.
- Define a role-name for each of the applications in the cluster. This tells Application Insights which of your applications the monitoring data is coming from, allowing it to create a proper Application map from the recorded data.
For additional information on how this works, review:
- Enable Azure Monitor OpenTelemetry for .NET, Node.js, Python, and Java applications
- Spring Boot via Docker entry point
- Workspace-based Application Insights resources
Step-by-step guidance
If you’ve already successfully run the
create-azure-resources.sh
script discussed in the installation instructions, skip to step 2.
-
Your first step is to create an Application Insights resource. Execute this statement in your command-line environment:
APP_INSIGHTS_NAME=app-insights-$APPNAME-$UNIQUEID az monitor app-insights component create \ --resource-group $RESOURCE_GROUP \ --app $APP_INSIGHTS_NAME \ --location $LOCATION \ --kind web \ --workspace $WORKSPACE_ID
-
Store the Application Insights connection string as an environment variable. (You’ll need this later in the module.)
APP_INSIGHTS_CONN=$(az monitor app-insights component show --app $APP_INSIGHTS_NAME -g $RESOURCE_GROUP --query connectionString --output tsv)
In the previous lab’s Deploy app section, you already built the target image with the application insights agent included, so you won’t need to modify that.
-
Update the api-gateway container app to enable Application Insights Agent.
Notice how you’re setting the environment variables JAVA_TOOL_OPTIONS, APPLICATIONINSIGHTS_CONNECTION_STRING, and APPLICATIONINSIGHTS_CONFIGURATION_CONTENT.
APP_NAME="api-gateway" az containerapp update \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --set-env-vars JAVA_TOOL_OPTIONS='-javaagent:/applicationinsights-agent.jar' APPLICATIONINSIGHTS_CONNECTION_STRING="$APP_INSIGHTS_CONN" APPLICATIONINSIGHTS_CONFIGURATION_CONTENT='{"role": {"name": "'$APP_NAME'"}}'
-
After the app
api-gateway
deployment succeeds, you’ll need to execute the same statements on the other applications:customers-service
,vets-service
, andvisits-service
.To save time, you can use the preconfigured script ../tools/update-apps-appinsights.sh to deploy all of the apps in a batch:
export RESOURCE_GROUP APP_INSIGHTS_CONN ../tools/update-apps-appinsights.sh
The script ../tools/update-apps-appinsights.sh includes all the steps to update the target apps, and it runs the commands in parallel to save time. If you’d like to explore the details, open the file and review its contents.
(Optional) Alternatively, you can manually build each app individually by running the following commands:
APP_NAME="customers-service" az containerapp update \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --set-env-vars JAVA_TOOL_OPTIONS='-javaagent:/applicationinsights-agent.jar' APPLICATIONINSIGHTS_CONNECTION_STRING="$APP_INSIGHTS_CONN" APPLICATIONINSIGHTS_CONFIGURATION_CONTENT='{"role": {"name": "'$APP_NAME'"}}' APP_NAME="vets-service" az containerapp update \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --set-env-vars JAVA_TOOL_OPTIONS='-javaagent:/applicationinsights-agent.jar' APPLICATIONINSIGHTS_CONNECTION_STRING="$APP_INSIGHTS_CONN" APPLICATIONINSIGHTS_CONFIGURATION_CONTENT='{"role": {"name": "'$APP_NAME'"}}' APP_NAME="visits-service" az containerapp update \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --set-env-vars JAVA_TOOL_OPTIONS='-javaagent:/applicationinsights-agent.jar' APPLICATIONINSIGHTS_CONNECTION_STRING="$APP_INSIGHTS_CONN" APPLICATIONINSIGHTS_CONFIGURATION_CONTENT='{"role": {"name": "'$APP_NAME'"}}'
Notice that for each of the applications, we indicate a different role-name. Application Insights uses this role-name in its Application map feature to show the communication between your applications.
After redeploying, you’ll need to make sure everything is back up and running as expected. Verify that your applications are operational, and check the console log to determine whether any application is in a failed state.