Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Overview
This tutorial walks you through the steps for enabling tracing on a sample Java application installed on a host. In this scenario, you install a Datadog Agent on the same host as the application.
For other scenarios, including applications in containers or on cloud infrastructure, Agent in a container, and applications written in different languages, see the other Enabling Tracing tutorials.
A physical or virtual Linux host with root access when using sudo
Java 11-compatible JDK (not just a JRE) on the host. In this tutorial, you’re building on and deploying to the same machine.
Install the Agent
If you haven’t installed a Datadog Agent on your machine, go to Integrations > Agent and select your operating system. For example, on most Linux platforms, you can install the Agent by running the following script, replacing <YOUR_API_KEY> with your Datadog API key:
To send data to a Datadog site other than datadoghq.com, replace the DD_SITE environment variable with your Datadog site.
Verify that the Agent is running and sending data to Datadog by going to Events > Explorer, optionally filtering by the Datadog Source facet, and looking for an event that confirms the Agent installation on the host:
If after a few minutes you don't see your host in Datadog (under Infrastructure > Host map), ensure you used the correct API key for your organization, available at Organization Settings > API Keys.
Install and run a sample Java application
Next, install a sample application to trace. The code sample for this tutorial can be found at github.com/DataDog/apm-tutorial-java-host. Clone the git repository by running:
Build the sample app using either Maven or Gradle, whichever you are more comfortable with. Navigate to the notes directory within apm-tutorial-java-host and run one of the following:
./mvnw clean package
./gradlew clean bootJar
This uses the Spring Boot Jar plugin to create a single Jar file that contains all the necessary files to run the Java application.
Start the application by running:
java -jar target/notes-0.0.1-SNAPSHOT.jar
java -jar build/libs/notes-0.0.1-SNAPSHOT.jar
Alternatively, if your operating system supports it, you can build and run the application using the following scripts provided in the scripts directory:
sh ./scripts/mvn_run.sh
sh ./scripts/gradle_run.sh
The sample notes_app application is a basic REST API that stores data in an in-memory database. Open another terminal and use curl to send a few API requests:
curl localhost:8080/notes
Returns [] because there is nothing in the database yet
curl -X POST 'localhost:8080/notes?desc=hello'
Adds a note with the description hello and an ID value of 1. Returns {"id":1,"description":"hello"}.
curl localhost:8080/notes/1
Returns the note with id value of 1: {"id":1,"description":"hello"}
curl -X POST 'localhost:8080/notes?desc=otherNote'
Adds a note with the description otherNote and an ID value of 2. Returns {"id":2,"description":"otherNote"}
curl localhost:8080/notes
Returns the contents of the database: [{"id":1,"description":"hello"},{"id";2,"description":"otherNote"}]
Run more API calls to see the application in action. When you’re done, type Ctrl+C to stop the application.
Install Datadog tracing
Next, download the Java tracing library (sometimes called the Java Agent). From your apm-tutorial-java-host directory, run:
If your operating system does not support curl, you can go directly to 'https://dtdg.co/latest-java-tracer' to download the dd-java-agent.jar file.
Launch the Java application with automatic instrumentation
To start generating and collecting traces, restart the sample application with additional flags that cause tracing data to be sent to Datadog.
Note: The flags on these sample commands, particularly the sample rate, are not necessarily appropriate for environments outside this tutorial. For information about what to use in your real environment, read Tracing configuration.
Use curl to again send requests to the application:
curl localhost:8080/notes
[]
curl -X POST 'localhost:8080/notes?desc=hello'
{"id":1,"description":"hello"}
curl localhost:8080/notes/1
{"id":1,"description":"hello"}
curl localhost:8080/notes
[{"id":1,"description":"hello"}]
Wait a few moments, and take a look at your Datadog UI. Navigate to APM > Traces. The Traces list shows something like this:
The h2 is the embedded in-memory database for this tutorial, and notes is the Spring Boot application. The traces list shows all the spans, when they started, what resource was tracked with the span, and how long it took.
If you don’t see traces, clear any filter in the Traces Search field (sometimes it filters on an environment variable such as ENV that you aren’t using).
Examine a trace
On the Traces page, click on a POST /notes trace, and you’ll see a flame graph that shows how long each span took and what other spans occurred before a span completed. The bar at the top of the graph is the span you selected on the previous screen (in this case, the initial entry point into the notes application).
The width of a bar indicates how long it took to complete. A bar at a lower depth represents a span that completes during the lifetime of a bar at a higher depth.
The flame graph for a POST trace looks something like this:
A GET /notes trace looks something like this:
Tracing configuration
The Java tracing library uses Java’s built-in agent and monitoring support. The flag -javaagent:../dd-java-agent.jar tells the JVM where to find the Java tracing library so it can run as a Java Agent. Learn more about Java Agents at https://www.baeldung.com/java-instrumentation.
In addition to the javaagent flag, which enables the Java Agent, the launch commands specify three Unified Service Tagging settings to uniquely identify your application within Datadog. Always specify env, service, and version tags for every monitored application.
And finally, the dd.trace.sample.rate flag sets the sample rate for this application. The launch commands above set its value to 1, which means that 100% of all requests to the notes service are sent to the Datadog backend for analysis and display. For a low-volume test application, this is fine. Do not do this in production or in any high-volume environment, because this results in a very large volume of data. Instead, sample some of your requests. Pick a value between 0 and 1. For example, -Ddd.trace.sample.rate=0.1 sends traces for 10% of your requests to Datadog. Read more about tracing configuration settings and sampling mechanisms.
Notice that the flags in the commands appear before the -jar flag. That’s because these are parameters for the Java Virtual Machine, not your application. Make sure that when you add the Java Agent to your application, you specify the flags in the right location.
Add manual instrumentation to the Java application
Automatic instrumentation is convenient, but sometimes you want more fine-grained spans. Datadog’s Java DD Trace API allows you to specify spans within your code using annotations or code.
The following steps walk you through adding annotations to the code to trace some sample methods.
Open /notes/src/main/java/com/datadog/example/notes/NotesHelper.java. This example already contains commented-out code that demonstrates the different ways to set up custom tracing on the code.
Uncomment the lines that import libraries to support manual tracing:
Uncomment the lines that manually trace the two public processes. These demonstrate the use of @Trace annotations to specify aspects such as operationName and resourceName in a trace:
You can also create a separate span for a specific code block in the application. Within the span, add service and resource name tags and error handling tags. These tags result in a flame graph showing the span and metrics in Datadog visualizations. Uncomment the lines that manually trace the private method:
Tracertracer=GlobalTracer.get();// Tags can be set when creating the spanSpanspan=tracer.buildSpan("manualSpan1").withTag(DDTags.SERVICE_NAME,"NotesHelper").withTag(DDTags.RESOURCE_NAME,"privateMethod1").start();try(Scopescope=tracer.activateSpan(span)){// Tags can also be set after creationspan.setTag("postCreationTag",1);Thread.sleep(30);Log.info("Hello from the custom privateMethod1");
And also the lines that set tags on errors:
}catch(Exceptione){// Set error on spanspan.setTag(Tags.ERROR,true);span.setTag(DDTags.ERROR_MSG,e.getMessage());span.setTag(DDTags.ERROR_TYPE,e.getClass().getName());finalStringWritererrorString=newStringWriter();e.printStackTrace(newPrintWriter(errorString));span.setTag(DDTags.ERROR_STACK,errorString.toString());Log.info(errorString.toString());}finally{span.finish();}
Update your build script configuration, and build the application:
a. Open notes/pom.xml and uncomment the lines configuring dependencies for manual tracing. The dd-trace-api library is used for the @Trace annotations, and opentracing-util and opentracing-api are used for manual span creation.
a. Open notes/build.gradle and uncomment the lines configuring dependencies for manual tracing. The dd-trace-api library is used for the @Trace annotations, and opentracing-util and opentracing-api are used for manual span creation.
Resend some HTTP requests, specifically some GET requests.
On the Trace Explorer, click on one of the new GET requests, and see a flame graph like this:
Note the higher level of detail in the stack trace now that the getAll function has custom tracing.
The privateMethod around which you created a manual span now shows up as a separate block from the other calls and is highlighted by a different color. The other methods where you used the @Trace annotation show under the same service and color as the GET request, which is the notes application. Custom instrumentation is valuable when there are key parts of the code that need to be highlighted and monitored.
Add a second application to see distributed traces
Tracing a single application is a great start, but the real value in tracing is seeing how requests flow through your services. This is called distributed tracing.
The sample project includes a second application called calendar that returns a random date whenever it is invoked. The POST endpoint in the Notes application has a second query parameter named add_date. When it is set to y, Notes calls the calendar application to get a date to add to the note.
Navigate to the /calendar directory in the sample repo and build and run the calendar app:
curl -X POST 'localhost:8080/notes?desc=hello_again&add_date=y'
{"id":1,"description":"hello_again with date 2022-11-06"}
In the Trace Explorer, click this latest notes trace to see a distributed trace between the two services:
Note that you didn’t change anything in the notes application. Datadog automatically instruments both the okHttp library used to make the HTTP call from notes to calendar, and the Jetty library used to listen for HTTP requests in notes and calendar. This allows the trace information to be passed from one application to the other, capturing a distributed trace.
Troubleshooting
If you’re not receiving traces as expected, set up debug mode for the Java tracer. Read Enable debug mode to find out more.
Further reading
Documentation, liens et articles supplémentaires utiles: