'Java - Azure Application Insights Dynamic Tracing not working with docker images
Azure Functions has dynamic tracing functionality for Java with App Insights. Where it collects metrics against several libraries. Full list here.
As per this, for Linux, adding the config ApplicationInsightsAgent_EXTENSION_VERSION=~3
should make it work. It works as expected if the azure function is deployed as code runtime.
But it doesn't work when the azure function is deployed against docker!
Solution 1:[1]
As of Feb 2022, dynamic tracing functionality (for Java) automatically works against code runtime.
To make it work with a docker image, this is what I did (May be just a matter of time if it starts functioning automatically against docker images as well).
To make it work in the meantime:
mvn clean package
- Update your Dockerfile:
ARG JAVA_VERSION=8
# This image additionally contains function core tools – useful when using custom extensions
#FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION-core-tools AS installer-env
FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION-build AS installer-env
COPY . /src/java-function-app
RUN cd /src/java-function-app && \
mkdir -p /home/site/wwwroot && \
cd ./target/azure-functions/ && \
cd $(ls -d */|head -n 1) && \
cp -a . /home/site/wwwroot && \
cd /home/site/wwwroot && \
curl -SL --output applicationinsights-agent-3.2.6.jar https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.2.6/applicationinsights-agent-3.2.6.jar
# This image is ssh enabled
FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION-appservice
# This image isn't ssh enabled
#FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]
The above assumes that the mvn clean package
has been done before the docker image is created.
- Finally, add the path to the agent as
JAVA_OPTS
. This is how mypom.xml
looks:
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>${azure.functions.maven.plugin.version}</version>
<configuration>
<appName>${functionAppName}</appName>
<resourceGroup>xxxxx</resourceGroup>
<appServicePlanName>dockertemp2-appservice</appServicePlanName>
<region>centralus</region>
<pricingTier>P1V2</pricingTier>
<runtime>
<os>docker</os>
<image>xxxxx.azurecr.io/dockertemp:v0.0.2</image>
<serverId>SERVER_ID</serverId>
<registryUrl>xxxxx.azurecr.io</registryUrl>
</runtime>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~3</value>
</property>
<property>
<name>JAVA_OPTS</name>
<value>-javaagent:/home/site/wwwroot/applicationinsights-agent-3.2.6.jar</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
Finally, push the docker image and deploy the azure function.
mvn clean install package -DskipTests
docker build --tag xxxxx.azurecr.io/dockertemp:v0.0.2 .
docker push xxxxx.azurecr.io/dockertemp:v0.0.2
mvn azure-functions:deploy
Finally, able to see dynamic tracing in action:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |