Refactoring DW example
MODIFY
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldApplication.java
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldApplication.java
package com.example.helloworld;
-import com.example.helloworld.resources.HelloWorldResource;
-
+import com.example.helloworld.resources.SimpleCrudResource;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
@@ -21,17 +20,10 @@ public class HelloWorldApplication extends Application<HelloWorldConfiguration>
// nothing to do yet
}
-
@Override
public void run(HelloWorldConfiguration configuration, Environment environment) {
- // Register resources
- final HelloWorldResource resource = new HelloWorldResource(
- configuration.getTemplate(),
- configuration.getDefaultName()
- );
-
- environment.jersey().register(resource);
+ environment.jersey().register(new SimpleCrudResource());
}
}

MODIFY
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldConfiguration.java
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldConfiguration.java
package com.example.helloworld;
import io.dropwizard.Configuration;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import org.hibernate.validator.constraints.NotEmpty;
public class HelloWorldConfiguration extends Configuration {
- @NotEmpty
- private String template;
- @NotEmpty
- private String defaultName = "Stranger";
-
- @JsonProperty
- public String getTemplate() {
- return template;
- }
-
- @JsonProperty
- public void setTemplate(String template) {
- this.template = template;
- }
-
- @JsonProperty
- public String getDefaultName() {
- return defaultName;
- }
-
- @JsonProperty
- public void setDefaultName(String name) {
- this.defaultName = name;
- }
}
\ No newline at end of file

ADD
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Book.java
+package com.example.helloworld.api;
+
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.bson.Document;
+
+public class Book {
+
+ private final String title;
+ private final int numberPages;
+ private final String IsbnCode;
+
+
+ public Book(String isbnCode, String title, int numberPages) {
+ this.title = title;
+ this.numberPages = numberPages;
+ IsbnCode = isbnCode;
+ }
+
+ public Book(Document d) {
+ this(d.getString("isbn"), d.getString("title"), d.getInteger("page").intValue());
+ }
+
+ @JsonProperty("ISBN")
+ public String getIsbnCode() {
+ return IsbnCode;
+ }
+
+
+ public String getTitle() {
+ return title;
+ }
+
+ public int getNumberPages() {
+ return numberPages;
+ }
+
+ public Document toDocument() {
+ return new Document("isbn", IsbnCode)
+ .append("title", title)
+ .append("page", numberPages);
+ }
+}

DELETE
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Saying.java
-package com.example.helloworld.api;
-
-import com.fasterxml.jackson.annotation.JsonProperty;
-
-public class Saying {
- private long id;
-
- private String content;
-
- public Saying() {
- // Jackson deserialization
- }
-
- public Saying(long id, String content) {
- this.id = id;
- this.content = content;
- }
-
- @JsonProperty
- public long getId() {
- return id;
- }
-
- @JsonProperty
- public String getContent() {
- return content;
- }
-}
\ No newline at end of file

MODIFY
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/client/TracedClient.java
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/client/TracedClient.java
@@ -18,7 +18,7 @@ public class TracedClient {
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
- .url("http://localhost:8080/hello/history")
+ .url("http://localhost:8080/demo/")
.build();
Response response = client.newCall(request).execute();

DELETE
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/HelloWorldResource.java
-package com.example.helloworld.resources;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.Consumer;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.MediaType;
-
-import org.bson.Document;
-
-import com.codahale.metrics.annotation.Timed;
-import com.example.helloworld.api.Saying;
-import com.google.common.base.Optional;
-import com.mongodb.MongoClient;
-import com.mongodb.client.ListDatabasesIterable;
-
-import io.opentracing.contrib.agent.Trace;
-
-@Path("/hello")
-@Produces(MediaType.APPLICATION_JSON)
-public class HelloWorldResource {
- private final String template;
- private final String defaultName;
- private final AtomicLong counter;
-
- // Instantiate Synchronous Tracing MongoClient
- private final MongoClient mongoClient = new MongoClient("localhost", 27017);
-
- public HelloWorldResource(String template, String defaultName) {
- this.template = template;
- this.defaultName = defaultName;
- this.counter = new AtomicLong();
- }
-
- @GET
- @Timed
- public Saying sayHello(@QueryParam("name") Optional<String> name) throws InterruptedException {
- final String value = String.format(template, name.or(defaultName));
- return new Saying(counter.incrementAndGet(), value);
- }
-
- @GET
- @Path("/history")
- public Object history() throws InterruptedException {
- beforeDB();
-
- // Trace: Do some stuff with the DB
- ListDatabasesIterable<Document> documents = mongoClient.listDatabases();
- final List<String> list = new ArrayList<String>();
- documents.forEach(new Consumer<Document>() {
- @Override
- public void accept(Document t) {
- list.add(t.toJson());
- }
- });
-
- afterDB();
-
- return list;
- }
-
- @Trace(operationName="Before DB",tagsKV={"mytag","myvalue"})
- public void beforeDB() throws InterruptedException{
- Thread.sleep(333);
- }
-
- @Trace(operationName="After DB",tagsKV={"mytag","myvalue"})
- public void afterDB() throws InterruptedException{
- Thread.sleep(111);
- }
-
-}
\ No newline at end of file

ADD
dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/SimpleCrudResource.java
+package com.example.helloworld.resources;
+
+import com.example.helloworld.api.Book;
+import com.google.common.base.Optional;
+import com.mongodb.MongoClient;
+import com.mongodb.MongoClientOptions;
+import com.mongodb.client.MongoCursor;
+import com.mongodb.client.MongoDatabase;
+import io.opentracing.contrib.agent.Trace;
+import org.bson.Document;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import java.util.ArrayList;
+import java.util.List;
+
+@Path("/demo")
+@Produces(MediaType.APPLICATION_JSON)
+public class SimpleCrudResource {
+
+ // Instantiate Synchronous Tracing MongoClient
+ private final MongoDatabase db;
+
+ public SimpleCrudResource() {
+
+ MongoClientOptions settings = MongoClientOptions.builder()
+ .codecRegistry(com.mongodb.MongoClient.getDefaultCodecRegistry())
+ .build();
+
+ MongoClient client = new MongoClient("localhost", settings);
+
+ client.dropDatabase("demo");
+
+ db = client.getDatabase("demo");
+ db.createCollection("books");
+
+
+ }
+
+
+ @GET
+ @Path("/add")
+ public String addBook(
+ @QueryParam("isbn") Optional<String> isbn,
+ @QueryParam("title") Optional<String> title,
+ @QueryParam("page") Optional<Integer> page
+ ) throws InterruptedException {
+
+
+ // Simple business need to execute before saving a new book
+ beforeDB();
+
+ if (!isbn.isPresent()) {
+ throw new IllegalArgumentException("ISBN should not be null");
+ }
+
+ Book book = new Book(
+ isbn.get(),
+ title.or("missing title"),
+ page.or(0));
+
+ db.getCollection("books").insertOne(book.toDocument());
+ return "Book saved";
+ }
+
+ @GET
+ public List<Book> getBooks() throws InterruptedException {
+
+ // Simple business need to execute before saving a new book
+ beforeDB();
+
+ List<Book> books = new ArrayList<>();
+ try (MongoCursor<Document> cursor = db.getCollection("books").find().iterator();) {
+ while (cursor.hasNext()) {
+ books.add(new Book(cursor.next()));
+ }
+ }
+
+ // Simple business need to execute after retrieve the book list
+ afterDB();
+
+ return books;
+ }
+
+ @Trace(operationName = "Before DB", tagsKV = {"mytag", "myvalue"})
+ public void beforeDB() throws InterruptedException {
+ Thread.sleep(333);
+ }
+
+ @Trace(operationName = "After DB", tagsKV = {"mytag", "myvalue"})
+ public void afterDB() throws InterruptedException {
+ Thread.sleep(111);
+ }
+}
+

MODIFY
dd-trace-examples/dropwizard-mongo-client/src/main/resources/configuration.yaml
dd-trace-examples/dropwizard-mongo-client/src/main/resources/configuration.yaml
-template: Hello, %s!
-defaultName: Stranger
-
logging:
level: INFO
loggers:

Refactoring DW example