To demonstrate how to enable telemetry delivery to Azure Application Insights with a containerized Java application. This document demonstrates the creation of an assembly file to instruct Maven to build a zip file with the necessary project file, modify the pom.xml file with the required publish settings and create the Dockerfile essential to the creation of the container image. Finally, to tie everything together, I included an explanation of how to run and test the process locally.
To get this example to work, you will need the following:
The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive. For example, assume that a Maven project defines a single JAR artifact that contains both a console application and a Swing application. Such a project could define two “assemblies” that bundle the application with a different set of supporting scripts and dependency sets. One assembly would be the assembly for the console application, and the other could be a Swing application bundled with a slightly different set of dependencies. This assembly aims to package the project-generated JAR file and the AppInsights JAR file into a single Zip file.
Pom file Additions
Two plug-ins need to be added to the POM file. The first one will copy the applicationinsights-agent-X.X.X.jar into the resource directory, the second is to create the zip file based on the zip-assembly file.
Adding Four goals to the Docker file base on the openjdk:17-alpine image:
- Install the zip package into the container
- Copy the AppInsightsAgentAssembly.zip into the container
- Unzip the file
- Add a command line argument to refer to the jar file on startup
Bringing it all together
Create an instance of Application Insights in Azure and retrieve the connection string
Retrieve the code from here:
Open the code in your favorite editor and run the Maven Package action. The zip file should now be in your target folder.
Create a file named .env and add the Application Insights connection string as an environment variable.
Create the container image: docker build . -t myimage:latest
Start the container and reference the environment file in the docker run command
docker run -it -p 8080:8080 –env-file=.env myimage:latest
Call the API with the following URL:
Telemetry should be available in your instance of Application Insights: