Configure Application Insights to receive monitoring information from your applications
You now know how to set up monitoring for your AKS cluster, however, you would also like to get monitoring info on how your applications run in the cluster. To track Application specific monitoring data, you can use Application Insights. In this next step you will need to create an Application Insights resource and enable application monitoring for each of your microservices. For enabling this, you will not have to change anything in your microservices themselves, you can make use of the Java auto-instrumentation feature of Azure Monitor. The following steps are needed:
- Add the Application Insights jar file to your docker file.
- Add an environment variable to your microservices with the connection string info for your Application Insights instance.
- To get a proper application map in Application Insights, you will also have to define a different role for each of the microservices in the cluster.
You can follow the below guidance to do so.
- Azure Monitor OpenTelemetry-based auto-instrumentation for Java applications
- Spring Boot via Docker entry point
- Workspace-based Application Insights resources
Step by step guidance
-
As a first step, you will need to create an Application Insights resource. Execute the below statement in your bash shell.
AINAME=ai-$APPNAME-$UNIQUEID az extension add -n application-insights az monitor app-insights component create \ --app $AINAME \ --location $LOCATION \ --kind web \ -g $RESOURCE_GROUP \ --workspace $WORKSPACEID
-
Once your Application Insights resource got created, you will need to update the docker file you are using to deploy the different microservices to include the application insights jar file. As a first step, navigate to the
staging-acr
folder and download the latest application insights agent. We are renaming the jar file toai.jar
to have an easier name in the next steps.cd ../staging-acr wget https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.4.12/applicationinsights-agent-3.4.12.jar cp applicationinsights-agent-3.4.12.jar ai.jar
-
Make sure that the latest jar files for each microservice exist in the
staging-acr
folder.cp ../spring-petclinic-api-gateway/target/spring-petclinic-api-gateway-$VERSION.jar spring-petclinic-api-gateway-$VERSION.jar cp ../spring-petclinic-admin-server/target/spring-petclinic-admin-server-$VERSION.jar spring-petclinic-admin-server-$VERSION.jar cp ../spring-petclinic-customers-service/target/spring-petclinic-customers-service-$VERSION.jar spring-petclinic-customers-service-$VERSION.jar cp ../spring-petclinic-visits-service/target/spring-petclinic-visits-service-$VERSION.jar spring-petclinic-visits-service-$VERSION.jar cp ../spring-petclinic-vets-service/target/spring-petclinic-vets-service-$VERSION.jar spring-petclinic-vets-service-$VERSION.jar cp ../spring-petclinic-config-server/target/spring-petclinic-config-server-$VERSION.jar spring-petclinic-config-server-$VERSION.jar cp ../spring-petclinic-discovery-server/target/spring-petclinic-discovery-server-$VERSION.jar spring-petclinic-discovery-server-$VERSION.jar
-
Update the
Dockerfile
in thestaging-acr
directory to also copy theai.jar
file into the container. The resultingDockerfile
should look like this. Changes were made to the following lines:- line 5: Extra argument for the Application Insights jar file.
- line 11: Setting environment variable for the Application Insights jar file.
- line 16 and 17: Copy the Application Insights jar file into the container.
- line 20: Added an extra
-javaagent
argument to the run command for theai.jar
file.
FROM mcr.microsoft.com/openjdk/jdk:17-mariner ARG ARTIFACT_NAME ARG APP_PORT ARG AI_JAR EXPOSE ${APP_PORT} 8778 9779 # The application's jar file ARG JAR_FILE=${ARTIFACT_NAME} ARG APP_INSIGHTS_JAR=${AI_JAR} # Add the application's jar to the container ADD ${JAR_FILE} app.jar # Add App Insights jar to the container ADD ${APP_INSIGHTS_JAR} ai.jar # Run the jar file ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-javaagent:/ai.jar","-jar","/app.jar"]
-
Rebuild all of the containers, using docker build and push them to your Azure Container Registry. This will update the containers in your Azure Container Registry with a new version including the Application Insights jar file.
docker build -t $MYACR.azurecr.io/spring-petclinic-api-gateway:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-api-gateway-$VERSION.jar \ --build-arg APP_PORT=8080 \ --build-arg AI_JAR=ai.jar \ . docker push $MYACR.azurecr.io/spring-petclinic-api-gateway:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-admin-server:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-admin-server-$VERSION.jar \ --build-arg APP_PORT=8080 \ --build-arg AI_JAR=ai.jar \ . docker push $MYACR.azurecr.io/spring-petclinic-admin-server:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-customers-service:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-customers-service-$VERSION.jar \ --build-arg APP_PORT=8080 \ --build-arg AI_JAR=ai.jar \ . docker push $MYACR.azurecr.io/spring-petclinic-customers-service:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-visits-service:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-visits-service-$VERSION.jar \ --build-arg APP_PORT=8080 \ --build-arg AI_JAR=ai.jar \ . docker push $MYACR.azurecr.io/spring-petclinic-visits-service:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-vets-service:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-vets-service-$VERSION.jar \ --build-arg APP_PORT=8080 \ --build-arg AI_JAR=ai.jar \ . docker push $MYACR.azurecr.io/spring-petclinic-vets-service:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-config-server:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-config-server-$VERSION.jar \ --build-arg APP_PORT=8888 \ --build-arg AI_JAR=ai.jar \ . docker push $MYACR.azurecr.io/spring-petclinic-config-server:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-discovery-server:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-discovery-server-$VERSION.jar \ --build-arg APP_PORT=8761 \ --build-arg AI_JAR=ai.jar \ . docker push $MYACR.azurecr.io/spring-petclinic-discovery-server:$VERSION
-
You will now have to update the config map with an extra environment variable with the connection string info to your Application Insights instance. First, get the connection string info.
AI_CONNECTIONSTRING=$(az monitor app-insights component show --app $AINAME -g $RESOURCE_GROUP --query connectionString)
-
Navigate to the
kubernetes
directory and update yourconfig-map.yml
file to include an extra environment variable for the Application Insights connection string info.cd ~/workspaces/java-microservices-aks-lab/src/kubernetes
apiVersion: v1 kind: ConfigMap metadata: name: config-server data: # property-like keys; each key maps to a simple value CONFIG_SERVER_URL: "http://config-server:8888" APPLICATIONINSIGHTS_CONNECTION_STRING: "Copy here the value of $AI_CONNECTIONSTRING"
-
re-apply the
config-map.yml
file.kubectl replace -f config-map.yml --namespace spring-petclinic
-
Additionally each of the application specific YAML files in this folder will need 2 additional environment variables, 1 for the
APPLICATION_INSIGHTS_CONNECTIONSTRING
and one for theAPPLICATIONINSIGHTS_CONFIGURATION_CONTENT
. These should be added after line 25 and before the imagePullPolicy. You can curl the updates for these files and then fill out again the correct container registry name.curl -o spring-petclinic-api-gateway.yml https://raw.githubusercontent.com/Azure-Samples/java-microservices-aks-lab/main/docs/03_lab_monitor/spring-petclinic-api-gateway.yml IMAGE=${MYACR}.azurecr.io/spring-petclinic-api-gateway:$VERSION sed -i "s|#image#|$IMAGE|g" spring-petclinic-api-gateway.yml curl -o spring-petclinic-admin-server.yml https://raw.githubusercontent.com/Azure-Samples/java-microservices-aks-lab/main/docs/03_lab_monitor/spring-petclinic-admin-server.yml IMAGE=${MYACR}.azurecr.io/spring-petclinic-admin-server:$VERSION sed -i "s|#image#|$IMAGE|g" spring-petclinic-admin-server.yml curl -o spring-petclinic-customers-service.yml https://raw.githubusercontent.com/Azure-Samples/java-microservices-aks-lab/main/docs/03_lab_monitor/spring-petclinic-customers-service.yml IMAGE=${MYACR}.azurecr.io/spring-petclinic-customers-service:$VERSION sed -i "s|#image#|$IMAGE|g" spring-petclinic-customers-service.yml curl -o spring-petclinic-visits-service.yml https://raw.githubusercontent.com/Azure-Samples/java-microservices-aks-lab/main/docs/03_lab_monitor/spring-petclinic-visits-service.yml IMAGE=${MYACR}.azurecr.io/spring-petclinic-visits-service:$VERSION sed -i "s|#image#|$IMAGE|g" spring-petclinic-visits-service.yml curl -o spring-petclinic-vets-service.yml https://raw.githubusercontent.com/Azure-Samples/java-microservices-aks-lab/main/docs/03_lab_monitor/spring-petclinic-vets-service.yml IMAGE=${MYACR}.azurecr.io/spring-petclinic-vets-service:$VERSION sed -i "s|#image#|$IMAGE|g" spring-petclinic-vets-service.yml curl -o spring-petclinic-config-server.yml https://raw.githubusercontent.com/Azure-Samples/java-microservices-aks-lab/main/docs/03_lab_monitor/spring-petclinic-config-server.yml IMAGE=${MYACR}.azurecr.io/spring-petclinic-config-server:$VERSION sed -i "s|#image#|$IMAGE|g" spring-petclinic-config-server.yml curl -o spring-petclinic-discovery-server.yml https://raw.githubusercontent.com/Azure-Samples/java-microservices-aks-lab/main/docs/03_lab_monitor/spring-petclinic-discovery-server.yml IMAGE=${MYACR}.azurecr.io/spring-petclinic-discovery-server:$VERSION sed -i "s|#image#|$IMAGE|g" spring-petclinic-discovery-server.yml
-
Inspect the changes in these files as opposed to their previous version. For the
spring-petclinic-api-gateway.yml
file you will notice the following addition:- name: "APPLICATIONINSIGHTS_CONNECTION_STRING" valueFrom: configMapKeyRef: name: config-server key: APPLICATIONINSIGHTS_CONNECTION_STRING - name: "APPLICATIONINSIGHTS_CONFIGURATION_CONTENT" value: >- { "role": { "name": "api-gateway" } }
Notice that for each of the microservices, we indicate a different role-name. This role-name will be used in the Application Insights Application Map to properly show the communication between your microservices.
-
Re-apply all of the YAML files, starting with the config server.
kubectl apply -f spring-petclinic-config-server.yml kubectl get pods -w
-
Once the config-server is properly up and running, escape out of the pod watch statement with
Ctrl+Q
. Now in the same way deploy thediscovery-server
.kubectl apply -f spring-petclinic-discovery-server.yml kubectl get pods -w
-
Once the discovery-server is properly up and running, escape out of the pod watch statement with
Ctrl+Q
. Now in the same way deploy the rest of the microservices.kubectl apply -f spring-petclinic-customers-service.yml kubectl apply -f spring-petclinic-visits-service.yml kubectl apply -f spring-petclinic-vets-service.yml kubectl apply -f spring-petclinic-api-gateway.yml kubectl apply -f spring-petclinic-admin-server.yml
To make sure everything is back up and running as expected, you may want to double check if all your services are back up and running by using a
kubectl get pods -w
statement. If a pod is showing any problems in starting up, take a look at the logs to check what might be going on with akubectl logs <name of the pod>
.