Setting up Datadog APM on ECS, Fargate(Spring Boot)
Tags: datadog, apm, ecs, fargate
Published: 2022-06-07 01:35:53

Hello this is Roy, and today I want to write a short blog post about enabling Datadog APM at Spring Boot application running on Fargate, along with ECS.

APM helps you deep dive into your application, allowing you to see all the metrics and what kind of tasks were executed(also in which order) to handle a request.

For example, if you were to use APM on server applications, which is also in this case, you can see how much time it took to execute queries to database, how long it took to process associated codes, and also the latency time, which is a time indicating how long it took for the server to process the request made from a client.

> For container metrics, you can reference this nice [documentation](https://docs.datadoghq.com/integrations/ecs_fargate/?tab=fluentbitandfirelens) from Datadog itself.

As I was using Amazon ECS with Fargate, I was confused if I had to see APM configuration for [ECS](https://docs.datadoghq.com/agent/amazon_ecs/apm/?tab=ec2metadataendpoint) or for [Fargate](https://docs.datadoghq.com/integrations/ecs_fargate/?tab=logdriver).

After reading all the relevant docs, I ended up noticing that there were three essential steps to enable Datadog APM on my environment.

1. Download `dd-java-agent.jar` 
2. Assign this downloaded jar file to `-javaagent` flag when running the application jar file.
3. Set appropriate environment variables for Datadog.

> For step 3, you can configure it in your task definition file.

In my case, my previous Dockerfile for containerizing my Spring Boot application looked as below.

```Dockerfile
FROM openjdk:11-jdk AS builder
WORKDIR application
ARG JAR_FILE=build/libs/MY_APPLICATION.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM openjdk:11-jdk
WORKDIR application
COPY --from=builder application/dependencies/ ./
RUN true
COPY --from=builder application/spring-boot-loader/ ./
RUN true
COPY --from=builder application/snapshot-dependencies/ ./
RUN true
COPY --from=builder application/application/ ./
RUN true
ENV TZ Asia/Seoul
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
```

As I was wondering where I was supposed to download the `dd-java-agent.jar` file and assign it to `-javaagent` flag, I found that Dockerfile was the appropriate place in my case.

So my Dockerfile changed as below, and as you can see it downloads `dd-java-agent.jar` file, and assigns it to `-javaagent` flag when running my application's jar file.

```Dockerfile
FROM openjdk:11-jdk AS builder
WORKDIR application
ARG JAR_FILE=build/libs/MY_APPLICATION.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM openjdk:11-jdk
WORKDIR application
RUN  apt-get update \
  && apt-get install -y wget \
  && rm -rf /var/lib/apt/lists/*
RUN wget -O dd-java-agent.jar 'https://dtdg.co/latest-java-tracer'

COPY --from=builder application/application.jar ./application.jar
RUN true
COPY --from=builder application/dependencies/ ./
RUN true
COPY --from=builder application/spring-boot-loader/ ./
RUN true
COPY --from=builder application/snapshot-dependencies/ ./
RUN true
COPY --from=builder application/application/ ./
RUN true

ENV TZ Asia/Seoul

ENTRYPOINT ["java", "-javaagent:dd-java-agent.jar", "-Ddd.profiling.enabled=true", "-XX:FlightRecorderOptions=stackdepth=256", "org.springframework.boot.loader.JarLauncher"]
```

And that's it! After deploying new tasks, I was able to see my application on Datadog APM tab, and was able to see all the events made, all the details and process time of it, how long it took to process a request and much more.

I spent around 3 days configuring this, and I am hoping that this blog will help you if you are in the same situation as I am.

Thank you, and any feedbacks or comments are always appreciated.