Letās start with āwhyā should one even bother with setting up Datadog on the local machine. I can share my motivation to set it up
My strong āwhyā:trying to make sense of the flaky tests:
We had an extensive end-to-end test suite with high coverage. But sadly, the tests grew flakier and flakier over time. To my dismay, when the tests were run in isolation, theyād pass. When we ran the entire test suite, many tests would start failing. The application logs corresponding to the tests should help us debug. But I couldnāt even understand where the logs for a particular test started and ended, let alone make sense of them.
Datadog to the rescue š¦ø!
We have been usingĀ
Our application is written in Java and my local machine runs on MacOS. You could tweak the below steps to suit your need.
What do you need to set up:
- Sign up for DatadogĀ
here . Our scope here is only local debugging, so aĀfree trial Ā should suffice. - Datadog Agent is theĀ software that monitors the host on which it runs.Ā Settings like service name, environment, etc. can be made in the agent.
dd-trace-java , Datadog's APM (App Performance Monitoring) client Java library.Ādd-trace-java
Ā is a jar that contains APIs to automatically or manuallyĀtrace Ā andĀprofile Ā Java applications.
Step 1 ā Sign up process:
-
After giving your email id and other details, you would be prompted to choose your tech stack on the next page.
-
after you click āNextā, comes the most important part. You would be given the āagent setupā instructions. My local machine is MacOSX, so I chose it appropriately. Please note down theĀ DD_API_KEYĀ andĀ DD_SITEĀ values, as they will be used in the subsequent steps.
Step 2ā Datadog Agent Installation:
For MacOS, you could install viaĀ
-
Customize theĀ
datadog-agent
Ā as shown below
-
After the above step is completed, youāll be asked to āFinishā in the Datadog-web-UI.
- Youāll be prompted to add anyĀ
integrations
. You could addĀJava
,ĀDocker
Ā etc (as per your project needs).
Step 3 ā Install Datadog tracer:
-
Download theĀ
dd-java-agent
Ā jar fromĀhere  ² -
For your Java application, make sure youāre using a version of Java that comes with an in-builtĀ FlightRecorder. This is needed for tracing. OpenJdK8 comes with an inbuiltĀ FlightRecorder. If youāre using Oracle Java, you need a commercial license to useĀ **FlightRecorder.**³
Running your application:
- add the VM options for our java application:
-javaagent:"/Users/home/Downloads/dd-java-agent.jar" -Ddd.profiling.enabled=true -Ddd.logs.injection=true -Ddd.agent.port=8126 -Ddd.service=ecommerce-service -Ddd.env=local -XX:FlightRecorderOptions=stackdepth=256
- If you are using aĀ
gradle
Ā task, you can include theĀjvmArgs
Ā as below:
jvmArgs = ["-javaagent:/Users/home/Downloads/dd-java-agent.jar", "-XX:FlightRecorderOptions=stackdepth=256", "-Ddd.profiling.enabled=true", "-Ddd.logs.injection=true", "-Ddd.agent.port=8126", "-Ddd.service=ecommerce-service", "-Ddd.env=local"]
- Now start your Java application. If the tracing is happening correctly, you should see the below logs:
Logging in Datadog:
- To enable logs to be streamed to Datadog, follow the instructions in the Datadog Web UI. ā“
- ForĀ
macOS
ā Config directory isĀ~/.datadog-agent/conf.d/java.d
Ā and the file is present atĀ~/.datadog-agent/conf.d/java.d/conf.yaml
-
and now, theĀ
logs
should start flowing in Datadog.
Some Tips:
-
Add appropriateĀ
debug
logs at the beginning and end of each method in your application. Itās very useful while debugging. -
Add a mechanism to injectĀ
span id
andĀtrace id
. Itās an amazing lifesaver and puts the logs in proper context.āµ
Conclusion:
Once I had the Datadog agent up and running locally, I was able to view all the requests contextually.
And voila! All of my end-to-end tests are passing now š Happy logging!
Additional Info:
-
¹Alternatively, you could also download theĀ
dmg package Ā and install it. This link can also be obtained in the above āAgent Setupā step (refer to screenshot) -
²Direct download theĀ
dd-trace-java Ā jar. -
³ Starting from JDK 11, we may useĀ
FlightRecorder
without activating anything. More details can be foundĀhere . -
ā“More details areĀ
here . -
āµIf your application is Java, you could you this libraryĀ
spring-cloud-sleuth Ā for addingĀspan_id
Ā andĀtrace_id
Ā to the request.