# Tracing in native image - Datadog

> **TL;DR**
> Restore Datadog trace observability in a Spring Boot GraalVM native image by embedding the Datadog Java agent into the native build using `-J-javaagent`, then running with `DD_SERVICE` set.

After successfully launching the spring context, in [Road to Native Image Spring Boot 3](road-to-native-image-spring-boot-3.html), I've sense-checked the API and added more reflection data from the agent, mainly POJOs that the native image couldn't deserialize.

This article aims to restore observability via Datadog, particularly for traces.

Datadog relies on the Java agent that gets passed to the JVM at runtime to instrument the app for traces. However, we are no longer running on the JVM. Let's see how we can build a native image that supports Datadog's agent instrumentation:

## Setup

What you'll need are 3 elements:

### 1. The Datadog agent:

Terminology is a bit loose here. This is not the Java agent. It's an agent provided by Datadog as well that forwards your traces to the Datadog endpoints:

Install:

> **Tip:**
> References
>
>
>
> [Datadog Agent Documentation](https://docs.datadoghq.com/agent/?tab=Linux)
>
>
>
> [Datadog Site Information](https://docs.datadoghq.com/getting_started/site/)

Configure via the `datadog.yaml` file:

* site: value from your Datadog Site Information.

* api_key: your DD API key.

```YAML
site: <site>

api_key: <api_key>

apm_config:
  enabled: true
  receiver_port: <port, defaults to 8126>
```

Check the agent status in the Datadog Agent Manager on [Datadog Agent Manager](http://localhost:5002)

You should be able to see this:

```LOG
Endpoints
<datadog endpoint> - API Key ending with:
  - <last 5 characters of your API key>
```

### 2. The Java Jar agent:

Download the jar provided by DD:

> **Tip:**
> References
>
>
>
> [Java Jar Agent Documentation](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/java/?tab=wget)

### 3. The Datadog libraries for custom instrumentation (optional):

Spring Boot is fully supported through spring-web/spring-webflux.

> **Tip:**
> References
>
>
>
> Check compatibility on
>
>
>
> [Java Compatibility Documentation](https://docs.datadoghq.com/tracing/trace_collection/compatibility/java/?tab=graalvm)

## Build Native Image with Datadog Agent:

At the properties level:

```YAML
management:
  datadog:
    metrics:
      export:
        apiKey: '<api_key>'
        application-key: '<app_key>'
        enabled: true
        uri: '<datadog_site_endpoint>'
```

Pass it to the native image plugin arguments:

```XML
<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <configuration>
        <buildArgs>
            <arg>-Ob</arg>
            <arg>-J-javaagent:/path/to/agent/dd-java-agent.jar</arg>
            ...
</plugin>
```

Rebuild your native image:

```SHELL
mvn -Pnative package -DskipTests
```

Run with a custom service identifier for instance.

```SHELL
spring_profiles_active=local DD_SERVICE=native_traces ./target/executable-name.exe
```

## Traces

Check if your traces are being sent to the DD Agent on the Agent Manager:

![traces_received.png](img/traces_received.png)

On the DD APM UI, check that you are receiving traces. Here is a metric push example that was captured.

![captured_trace_metric.png](img/captured_trace_metric.png)

