[example] update dropwizard example
MODIFY
dd-trace-examples/dropwizard-mongo-client/README.md
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
+./gradlew clean shadowJar
+```
+Then you can start all services via Docker:
```bash
-docker run -it --rm -p 27017:27017 --name mongo -d mongo
+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
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.

ADD
dd-trace-examples/dropwizard-mongo-client/docker-compose.yml
+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"

MODIFY
dd-trace-examples/dropwizard-mongo-client/dropwizard-mongo-client.gradle
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'

MODIFY
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/SimpleCrudResource.java
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 */

MODIFY
dd-trace-examples/dropwizard-mongo-client/src/main/resources/dd-trace.yaml
dd-trace-examples/dropwizard-mongo-client/src/main/resources/dd-trace.yaml
# 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

[example] update dropwizard example