[example] update dropwizard example

This commit is contained in:
Emanuele Palazzetti 2017-08-02 16:41:45 +02:00
parent 4086d8d7a0
commit 10f70132d7
No known key found for this signature in database
GPG Key ID: F4D8F69FEF18A502
5 changed files with 48 additions and 26 deletions

View File

@ -12,15 +12,20 @@ We also demonstrate user cross-process tracing through the `TracedClient` exampl
#### Prerequisites #### Prerequisites
1. Make sure that you have a local mongo database running (hostname: `localhost`, port: `27017`). Be sure to build the project so that the latest version of ``dd-trace-java`` components are used. You can build
2. No process holding the 8080 and the 8081 port, they are used by the Dropwizard server. all libraries and examples launching from the ``dd-trace-java`` root folder:
If you're using Docker, you can run a mongo instance as follow:
```bash ```bash
docker run -it --rm -p 27017:27017 --name mongo -d mongo ./gradlew clean shadowJar
``` ```
Then you can start all services via Docker:
```bash
cd dd-trace-examples/dropwizard-mongo-client
DD_API_KEY=<your_datadog_api_key> docker-compose up -d
```
A valid ``DD_API_KEY`` is required to post collected traces to the Datadog backend.
#### Run the application #### Run the application
If you want to enable tracing you have to launch the application with the Datadog java agent. If you want to enable tracing you have to launch the application with the Datadog java agent.
@ -33,7 +38,6 @@ First, get the latest version of the dd-java-agent:
wget -O dd-java-agent.jar 'https://search.maven.org/remote_content?g=com.datadoghq&a=dd-java-agent&v=LATEST' wget -O dd-java-agent.jar 'https://search.maven.org/remote_content?g=com.datadoghq&a=dd-java-agent&v=LATEST'
``` ```
Then, build the app add the agent to the JVM. That can be done as follow: Then, build the app add the agent to the JVM. That can be done as follow:
``` ```
cd path/to/dd-trace-examples/dropwizard-mongo-client cd path/to/dd-trace-examples/dropwizard-mongo-client
@ -42,14 +46,15 @@ java -javaagent:/path/to/dd-java-agent.jar -jar build/libs/dropwizard-mongo-clie
``` ```
### Generate traces ### Generate traces
#### With your web browser #### With your web browser
Once the application runs. Go to the following url: Once the application runs. Go to the following url:
* [http://localhost:8080/demo/add?title=some-book-title&isbn=1234&page=42]() * [http://localhost:8080/demo/add?title=some-book-title&isbn=1234&page=42][1]
* [http://localhost:8080/demo/]() * [http://localhost:8080/demo/][2]
[1]: http://localhost:8080/demo/add?title=some-book-title&isbn=1234&page=42
[2]: http://localhost:8080/demo/
Then get back to Datadog and wait a bit to see a trace coming. Then get back to Datadog and wait a bit to see a trace coming.

View File

@ -0,0 +1,12 @@
mongo:
image: mongo:3.2
ports:
- "127.0.0.1:27017:27017"
ddagent:
image: datadog/docker-dd-agent
environment:
- DD_BIND_HOST=0.0.0.0
- DD_API_KEY
ports:
- "127.0.0.1:8126:8126"

View File

@ -11,8 +11,8 @@ description = 'dropwizard-mongo-client'
dependencies { dependencies {
compile project(':dd-trace-annotations') compile project(':dd-trace-annotations')
compile group: 'io.opentracing', name: 'opentracing-api', version: '0.+' compile group: 'io.opentracing', name: 'opentracing-api', version: '0.30.0'
compile group: 'io.opentracing', name: 'opentracing-util', version: '0.+' compile group: 'io.opentracing', name: 'opentracing-util', version: '0.30.0'
compile group: 'io.dropwizard', name: 'dropwizard-core', version: '0.9.2' compile group: 'io.dropwizard', name: 'dropwizard-core', version: '0.9.2'
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2' compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2'

View File

@ -6,7 +6,7 @@ import com.google.common.base.Optional;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import io.opentracing.tag.StringTag; import io.opentracing.ActiveSpan;
import io.opentracing.util.GlobalTracer; import io.opentracing.util.GlobalTracer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -91,33 +91,38 @@ public class SimpleCrudResource {
} }
// The methodDB is traced (see below), this will be produced a new child span // The methodDB is traced (see below), this will be produced a new child span
beforeDB(); afterDB();
return books; return books;
} }
/** /**
* The beforeDB is traced using the annotation @trace with a custom operationName and a custom * The beforeDB is traced using the annotation @Trace with a custom operationName and a custom
* tag. * tag.
* *
* @throws InterruptedException * @throws InterruptedException
*/ */
@Trace(operationName = "Before DB") @Trace(operationName = "database.before")
public void beforeDB() throws InterruptedException { public void beforeDB() throws InterruptedException {
new StringTag("mytag").set(GlobalTracer.get().activeSpan(), "myvalue"); ActiveSpan currentSpan = GlobalTracer.get().activeSpan();
Thread.sleep(333); if (currentSpan != null) {
currentSpan.setTag("status", "started");
Thread.sleep(10);
}
} }
/** /**
* The beforeDB is traced using the annotation @trace with a custom operationName and a custom * The afterDB is traced using the annotation @Trace with a custom operationName and a custom tag.
* tag.
* *
* @throws InterruptedException * @throws InterruptedException
*/ */
@Trace(operationName = "After DB") @Trace(operationName = "database.after")
public void afterDB() throws InterruptedException { public void afterDB() throws InterruptedException {
new StringTag("mytag").set(GlobalTracer.get().activeSpan(), "myvalue"); ActiveSpan currentSpan = GlobalTracer.get().activeSpan();
Thread.sleep(111); if (currentSpan != null) {
currentSpan.setTag("status", "started");
Thread.sleep(10);
}
} }
/** Flush resources */ /** Flush resources */

View File

@ -1,8 +1,8 @@
# Service name used if none is provided in the app # Service name used if none is provided in the app
defaultServiceName: java-app defaultServiceName: dropwizard-example
# The writer to use. # The writer to use.
# Could be: LoggingWritter or DDAgentWriter (default) # Could be: LoggingWriter or DDAgentWriter (default)
writer: writer:
# LoggingWriter: Spans are logged using the application configuration # LoggingWriter: Spans are logged using the application configuration
# DDAgentWriter: Spans are forwarding to a Datadog Agent # DDAgentWriter: Spans are forwarding to a Datadog Agent
@ -26,4 +26,4 @@ sampler:
enableCustomAnnotationTracingOver: ["com.example"] enableCustomAnnotationTracingOver: ["com.example"]
# Disable some instrumentations # Disable some instrumentations
# disabledInstrumentations: ["apache http", "mongo", "jetty", "tomcat", ...] # disabledInstrumentations: ["apache http", "mongo", "jetty", "tomcat", ...]