DEV Community

Roy Ra for AWS Community Builders

Posted on

5 3

Setting up Datadog APM on ECS, Fargate(Spring Boot)

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 from Datadog itself.

As I was using Amazon ECS with Fargate, I was confused if I had to see APM configuration for ECS or for Fargate.

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.

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"]
Enter fullscreen mode Exit fullscreen mode

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.

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"]
Enter fullscreen mode Exit fullscreen mode

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.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (2)

Collapse
 
jjthompson profile image
jjthompson •

Hi thanks for the instructions, what does you task definition file look like?

Collapse
 
jjthompson profile image
jjthompson •

Do you have the agent also included there?

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post