Logico_jp explains how to send distributed traces from Micronaut native image Java applications to Azure Monitor, covering dependencies, build configuration, and trace verification, including practical troubleshooting tips.

Send Traces from Micronaut Native Image Applications to Azure Monitor

Author: Logico_jp

Overview

This guide walks through instrumenting Micronaut applications (compiled as GraalVM native images) so that they can export distributed trace data to Azure Monitor (Application Insights). It includes prerequisites, dependency setup, configuration, build, and verification using both Java and native image artifacts.

Prerequisites

  • Maven: 3.9.10
  • JDK: version 21
  • Micronaut: 4.9.0 or later

Reference guide:

Project Setup

You can use Micronaut CLI or Micronaut Launch. For YAML configuration, add the “yaml” feature.

Example CLI:

mn create-app \
  --build=maven \
  --jdk=21 \
  --lang=java \
  --test=junit \
  --features=tracing-opentelemetry-http,validation,graalvm,azure-tracing,http-client,yaml \
  dev.logicojp.micronaut.azuremonitor-metric

Micronaut Launch: Select features – tracing-opentelemetry-http, validation, graalvm, azure-tracing, http-client, yaml.

Dependencies

Core for tracing to Azure Monitor:

  • io.micronaut.azure:micronaut-azure-tracing
  • com.azure:azure-monitor-opentelemetry-autoconfigure
  • io.micronaut:micronaut-inject
  • io.micronaut.tracing:micronaut-tracing-opentelemetry
  • io.micronaut.tracing:micronaut-tracing-opentelemetry-http

Add GraalVM reachability metadata support:

<dependency>
  <groupId>org.graalvm.buildtools</groupId>
  <artifactId>graalvm-reachability-metadata</artifactId>
  <version>0.11.0</version>
</dependency>

GraalVM Maven plugin:

<plugin>
  <groupId>org.graalvm.buildtools</groupId>
  <artifactId>native-maven-plugin</artifactId>
  <configuration>
    <metadataRepository>
      <enabled>true</enabled>
    </metadataRepository>
    <buildArgs combine.children="append">
      <buildArg>-Ob</buildArg>
    </buildArgs>
    <quickBuild>true</quickBuild>
  </configuration>
</plugin>

Configuration

Set the Application Insights connection string (different from metric settings):

  • In application.yml:

    azure:
      tracing:
        connection-string: InstrumentationKey=...
    
  • Or as an environment variable: azure.tracing.connection-string="InstrumentationKey=..."

Handling Dependency Conflicts

Micronaut lets you substitute dependencies (e.g., use Undertow instead of Netty, JSON-P/JSON-B instead of Jackson) to resolve conflicts commonly encountered with Azure SDK and GraalVM native image compilation.

Build and Test (Java Application)

  • Build: mvn clean package
  • Start app and verify traces are sent to Azure Monitor (Application Insights)

Generate config for native image:

# Collect reflect-config, etc.

$JAVA_HOME/bin/java \ \
  -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/{groupId}/{artifactId}/ \
  -jar ./target/{artifactId}-{version}.jar

# Generate trace file

$JAVA_HOME/bin/java \ \
  -agentlib:native-image-agent=trace-output=/path/to/trace-file.json \
  -jar ./target/{artifactId}-{version}.jar

# Generate reachability metadata

native-image-configure generate \
  --trace-input=/path/to/trace-file.json \
  --output-dir=/path/to/config-dir/

See Configure Native Image with the Tracing Agent

Generated files go in src/main/resources/META-INF/native-image/{groupId}/{artifactId}.

Native Image Build

  • Build native image: mvn package -Dpackaging=native-image
  • For faster local development, enable Quick Build and use -Ob optimization (Maven plugin docs)

Running and Verifying

  • Deploy the application, hit endpoints (e.g., inventory check with curl), and verify responses:

    curl https://<app-url>/store/inventory/desktop
    # Response: {"warehouse":7,"item":"desktop","store":2}
    
  • Place an order:

    curl -X POST "https://<app-url>/store/order" \
      -H 'Content-Type: application/json; charset=utf-8' \
      -d '{"item":"desktop", "count":5}'
    curl https://<app-url>/store/inventory/desktop
    
  • View traces and application map in Azure Monitor to confirm correct telemetry.

Troubleshooting

References

This post appeared first on “Microsoft Tech Community”. Read the entire article here