From a5c91dfbff79b0a1008fe5a5c7b20aa21fc48ace Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Mon, 29 May 2017 13:54:19 +0200 Subject: [PATCH] Refactoring DW example --- .../helloworld/HelloWorldApplication.java | 12 +-- .../helloworld/HelloWorldConfiguration.java | 26 ----- .../java/com/example/helloworld/api/Book.java | 43 ++++++++ .../com/example/helloworld/api/Saying.java | 28 ------ .../helloworld/client/TracedClient.java | 2 +- .../resources/HelloWorldResource.java | 77 --------------- .../resources/SimpleCrudResource.java | 98 +++++++++++++++++++ .../src/main/resources/configuration.yaml | 3 - 8 files changed, 144 insertions(+), 145 deletions(-) create mode 100644 dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Book.java delete mode 100644 dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Saying.java delete mode 100644 dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/HelloWorldResource.java create mode 100644 dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/SimpleCrudResource.java diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldApplication.java b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldApplication.java index 7544a5a2b0..91e7758fe2 100644 --- a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldApplication.java +++ b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldApplication.java @@ -1,7 +1,6 @@ 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 // 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()); } } diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldConfiguration.java b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldConfiguration.java index 654cb47976..d9890e4364 100644 --- a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldConfiguration.java +++ b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/HelloWorldConfiguration.java @@ -1,34 +1,8 @@ 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 diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Book.java b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Book.java new file mode 100644 index 0000000000..721ddefc4f --- /dev/null +++ b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Book.java @@ -0,0 +1,43 @@ +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); + } +} diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Saying.java b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Saying.java deleted file mode 100644 index 7ee1ce5dbf..0000000000 --- a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/api/Saying.java +++ /dev/null @@ -1,28 +0,0 @@ -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 diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/client/TracedClient.java b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/client/TracedClient.java index cac31ba4bf..ee9ce1a821 100644 --- a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/client/TracedClient.java +++ b/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(); diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/HelloWorldResource.java b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/HelloWorldResource.java deleted file mode 100644 index 4a0aaacb9a..0000000000 --- a/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/HelloWorldResource.java +++ /dev/null @@ -1,77 +0,0 @@ -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 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 documents = mongoClient.listDatabases(); - final List list = new ArrayList(); - documents.forEach(new Consumer() { - @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 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 new file mode 100644 index 0000000000..a3afc5eaae --- /dev/null +++ b/dd-trace-examples/dropwizard-mongo-client/src/main/java/com/example/helloworld/resources/SimpleCrudResource.java @@ -0,0 +1,98 @@ +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 isbn, + @QueryParam("title") Optional title, + @QueryParam("page") Optional 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 getBooks() throws InterruptedException { + + // Simple business need to execute before saving a new book + beforeDB(); + + List books = new ArrayList<>(); + try (MongoCursor 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); + } +} + diff --git a/dd-trace-examples/dropwizard-mongo-client/src/main/resources/configuration.yaml b/dd-trace-examples/dropwizard-mongo-client/src/main/resources/configuration.yaml index 57759eef03..fc42844e8e 100644 --- a/dd-trace-examples/dropwizard-mongo-client/src/main/resources/configuration.yaml +++ b/dd-trace-examples/dropwizard-mongo-client/src/main/resources/configuration.yaml @@ -1,6 +1,3 @@ -template: Hello, %s! -defaultName: Stranger - logging: level: INFO loggers: