Tutorial: Configure the Application Performance Management (APM) Java agent with init containers in Azure Container Apps
In this tutorial, you configure the Application Performance Management (APM) Java agent with init containers in Azure Container Apps. APM helps power observability for your container apps. You can package the APM plugin in the same image or Dockerfile with your app, but it binds together the management concerns, like release and Common Vulnerabilities and Exposures (CVE) mitigation. Rather than binding the concerns together, you can use the Java agent and init containers in Azure Container Apps to inject APM solutions without modifying your app image.
In this tutorial, you:
- Prepare an image to set up the Java agent and push it to an Azure Container Registry.
- Create a Container Apps environment and a container app as the target Java app.
- Configure init containers and volume mounts to set up Application Insights integration.
Prerequisites
- An Azure subscription. Create one for free..
- An instance of Application Insights.
- An instance of Azure Container Registry or another container image registry.
- Docker, to build an image.
- The latest version of the Azure CLI.
Set up the environment
Use the following steps to define environment variables and ensure your Container Apps extension is up to date:
Define environment variables by using the following commands:
export SUBSCRIPTION_ID="<SUBSCRIPTION_ID>" # Replace with your own Azure subscription ID export APP_INSIGHTS_RESOURCE_ID="/subscriptions/$SUBSCRIPTION_ID/resourceGroups/my-resource-group/providers/microsoft.insights/components/my-app-insights" export CONTAINER_REGISTRY_NAME="myacr" export RESOURCE_GROUP="my-resource-group" export ENVIRONMENT_NAME="my-environment" export CONTAINER_APP_NAME="my-container-app" export LOCATION="eastus"
Sign in to the Azure CLI by using the following commands:
az login az account set --subscription $SUBSCRIPTION_ID
Use the following commands to ensure that you have the latest version of the Azure CLI extensions for Container Apps and Application Insights:
az extension add --name containerapp --upgrade az extension add --name application-insights --upgrade
Retrieve the connection string for your Application Insights instance by using the following commands:
CONNECTION_STRING=$(az monitor app-insights component show \ --ids $APP_INSIGHTS_RESOURCE_ID \ --query connectionString)
Prepare the container image
To build a setup image for the Application Insights Java agent, use the following steps in the same directory:
Create a Dockerfile with the following contents:
FROM mcr.microsoft.com/cbl-mariner/base/core:2.0 ARG version="3.5.4" RUN tdnf update -y && tdnf install -y curl ca-certificates RUN curl -L "https://github.com/microsoft/ApplicationInsights-Java/releases/download/${version}/applicationinsights-agent-${version}.jar" > agent.jar ADD setup.sh /setup.sh ENTRYPOINT ["/bin/sh", "setup.sh"]
Create a setup.sh file with the following contents:
#!/bin/sh if [[ -z "$CONNECTION_STRING" ]]; then echo "Environment variable CONNECTION_STRING is not found. Exiting..." exit 1 else echo "{\"connectionString\": \"$CONNECTION_STRING\"}" > /java-agent/applicationinsights.json cp agent.jar /java-agent/agent.jar fi
Create an image by using the following command:
docker build . --tag "$CONTAINER_REGISTRY_NAME.azurecr.io/samples/java-agent-setup:1.0.0"
Push the image to Azure Container Registry or another container image registry by using the following commands:
az acr login --name $CONTAINER_REGISTRY_NAME docker push "$CONTAINER_REGISTRY_NAME.azurecr.io/samples/java-agent-setup:1.0.0"
Tip
You can find code relevant to this step in the azure-container-apps-java-samples GitHub repo.
Create a Container Apps environment and a container app as the target Java app
To create a Container Apps environment and a container app as the target Java app, use the following steps:
Create a Container Apps environment by using the following command:
az containerapp env create \ --name $ENVIRONMENT_NAME \ --resource-group $RESOURCE_GROUP \ --location "$LOCATION" \ --query "properties.provisioningState"
After you successfully create the Container Apps environment, the command line returns a
Succeeded
message.Create a container app for further configuration by using the following command:
az containerapp create \ --name $CONTAINER_APP_NAME \ --environment $ENVIRONMENT_NAME \ --resource-group $RESOURCE_GROUP \ --query "properties.provisioningState"
After you create the container app, the command line returns a
Succeeded
message.
Configure init container, secrets, environment variables, and volumes to set up Application Insights integration
Use the following steps to configure your init container with secrets, environment variables, and volumes. This configuration allows you to use the stored information with your Application Insights instance.
Write the current configuration of the running container app to an app.yaml file in the current directory, by using the following command:
az containerapp show \ --resource-group $RESOURCE_GROUP \ --name $CONTAINER_APP_NAME \ --output yaml \ > app.yaml
Use the following steps to edit your app.yaml file. The edits add secrets, ephemeral storage, and an init container to the file, and update the app container.
Add a secret for the Application Insights connection string by using the following example. Replace
$CONNECTION_STRING
with your Application Insights connection string.properties: configuration: secrets: - name: app-insights-connection-string value: $CONNECTION_STRING
Add an ephemeral storage volume for Java agent files by using the following example:
properties: template: volumes: - name: java-agent-volume storageType: EmptyDir
Add an init container with volume mounts and environment variables by using the following example. Replace
<CONTAINER_REGISTRY_NAME>
with your Azure Container Registry name.properties: template: initContainers: - image: <CONTAINER_REGISTRY_NAME>.azurecr.io/samples/java-agent-setup:1.0.0 name: java-agent-setup resources: cpu: 0.25 memory: 0.5Gi env: - name: CONNECTION_STRING secretRef: app-insights-connection-string volumeMounts: - mountPath: /java-agent volumeName: java-agent-volume
Update the app container with volume mounts and environment variables by using the following example:
properties: template: containers: - name: test-java-app image: mcr.microsoft.com/azurespringapps/samples/hello-world:0.0.1 resources: cpu: 0.5 memory: 1Gi env: - name: JAVA_TOOL_OPTIONS value: -javaagent:/java-agent/agent.jar volumeMounts: - mountPath: /java-agent volumeName: java-agent-volume
Update the container app with the modified app.yaml file by using the following command:
az containerapp update \ --resource-group $RESOURCE_GROUP \ --name $CONTAINER_APP_NAME \ --yaml app.yaml \ --query "properties.provisioningState"
After you update the container app, the command returns a
Succeeded
message. Now you can verify that your container app is connected, by viewing your Application Insights instance in the Azure portal.
Clean up resources
The resources you created in this tutorial contribute to your Azure bill. If you don't need them long term, use the following command to remove the resource group and its resources:
az group delete --resource-group $RESOURCE_GROUP
Related content
Other than Azure Application Insights, there are other popular APM solutions in the community. If you want to integrate your Azure Container App with other APM providers, just replace the Java agent JAR and related config files.