diff --git a/dd-trace-examples/dropwizard-mongo-client/README.md b/dd-trace-examples/dropwizard-mongo-client/README.md index f7e39fc4bd..91bc7f3c3b 100644 --- a/dd-trace-examples/dropwizard-mongo-client/README.md +++ b/dd-trace-examples/dropwizard-mongo-client/README.md @@ -12,15 +12,20 @@ We also demonstrate user cross-process tracing through the `TracedClient` exampl #### Prerequisites -1. Make sure that you have a local mongo database running (hostname: `localhost`, port: `27017`). -2. No process holding the 8080 and the 8081 port, they are used by the Dropwizard server. - -If you're using Docker, you can run a mongo instance as follow: - +Be sure to build the project so that the latest version of ``dd-trace-java`` components are used. You can build +all libraries and examples launching from the ``dd-trace-java`` root folder: ```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= docker-compose up -d +``` + +A valid ``DD_API_KEY`` is required to post collected traces to the Datadog backend. + #### Run the application 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' ``` - 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 @@ -42,14 +46,15 @@ java -javaagent:/path/to/dd-java-agent.jar -jar build/libs/dropwizard-mongo-clie ``` ### Generate traces - #### With your web browser 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/]() +* [http://localhost:8080/demo/add?title=some-book-title&isbn=1234&page=42][1] +* [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. diff --git a/dd-trace-examples/dropwizard-mongo-client/docker-compose.yml b/dd-trace-examples/dropwizard-mongo-client/docker-compose.yml new file mode 100644 index 0000000000..2c25cfab97 --- /dev/null +++ b/dd-trace-examples/dropwizard-mongo-client/docker-compose.yml @@ -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" diff --git a/dd-trace-examples/dropwizard-mongo-client/dropwizard-mongo-client.gradle b/dd-trace-examples/dropwizard-mongo-client/dropwizard-mongo-client.gradle index 0ddf755086..5ff119fe15 100644 --- a/dd-trace-examples/dropwizard-mongo-client/dropwizard-mongo-client.gradle +++ b/dd-trace-examples/dropwizard-mongo-client/dropwizard-mongo-client.gradle @@ -11,8 +11,8 @@ description = 'dropwizard-mongo-client' dependencies { compile project(':dd-trace-annotations') - compile group: 'io.opentracing', name: 'opentracing-api', version: '0.+' - compile group: 'io.opentracing', name: 'opentracing-util', version: '0.+' + compile group: 'io.opentracing', name: 'opentracing-api', version: '0.30.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: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2' diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/SimpleCrudResource.java b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/SimpleCrudResource.java index a4578dd77f..550b8302a7 100644 --- a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/SimpleCrudResource.java +++ b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/SimpleCrudResource.java @@ -6,7 +6,7 @@ import com.google.common.base.Optional; import com.mongodb.MongoClient; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; -import io.opentracing.tag.StringTag; +import io.opentracing.ActiveSpan; import io.opentracing.util.GlobalTracer; import java.util.ArrayList; 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 - beforeDB(); + afterDB(); 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. * * @throws InterruptedException */ - @Trace(operationName = "Before DB") + @Trace(operationName = "database.before") public void beforeDB() throws InterruptedException { - new StringTag("mytag").set(GlobalTracer.get().activeSpan(), "myvalue"); - Thread.sleep(333); + ActiveSpan currentSpan = GlobalTracer.get().activeSpan(); + 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 - * tag. + * The afterDB is traced using the annotation @Trace with a custom operationName and a custom tag. * * @throws InterruptedException */ - @Trace(operationName = "After DB") + @Trace(operationName = "database.after") public void afterDB() throws InterruptedException { - new StringTag("mytag").set(GlobalTracer.get().activeSpan(), "myvalue"); - Thread.sleep(111); + ActiveSpan currentSpan = GlobalTracer.get().activeSpan(); + if (currentSpan != null) { + currentSpan.setTag("status", "started"); + Thread.sleep(10); + } } /** Flush resources */ diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/resources/dd-trace.yaml b/dd-trace-examples/dropwizard-mongo-client/src/main/resources/dd-trace.yaml index beb8b2b734..fe9e345826 100644 --- a/dd-trace-examples/dropwizard-mongo-client/src/main/resources/dd-trace.yaml +++ b/dd-trace-examples/dropwizard-mongo-client/src/main/resources/dd-trace.yaml @@ -1,8 +1,8 @@ # Service name used if none is provided in the app -defaultServiceName: java-app +defaultServiceName: dropwizard-example # The writer to use. -# Could be: LoggingWritter or DDAgentWriter (default) +# Could be: LoggingWriter or DDAgentWriter (default) writer: # LoggingWriter: Spans are logged using the application configuration # DDAgentWriter: Spans are forwarding to a Datadog Agent @@ -26,4 +26,4 @@ sampler: enableCustomAnnotationTracingOver: ["com.example"] # Disable some instrumentations -# disabledInstrumentations: ["apache http", "mongo", "jetty", "tomcat", ...] \ No newline at end of file +# disabledInstrumentations: ["apache http", "mongo", "jetty", "tomcat", ...]