[example] update Spark example so it runs with dd-java-agent
MODIFY
dd-trace-examples/rest-spark/src/main/java/Hello.java
dd-trace-examples/rest-spark/src/main/java/Hello.java
-import static spark.Spark.*;
-
-import com.datadoghq.trace.resolver.DDTracerFactory;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import io.opentracing.ActiveSpan;
import io.opentracing.Tracer;
-import java.util.Arrays;
import org.bson.Document;
+import java.util.Arrays;
+
+import static spark.Spark.get;
+
public class Hello {
private static MongoDatabase mDatabase;
private static Tracer mTracer;
- public static void main(String[] args) {
- // Init the tracer from the configuration file
- mTracer = DDTracerFactory.createFromConfigurationFile();
- io.opentracing.util.GlobalTracer.register(mTracer);
+ public static void main(final String[] args) {
+ // Get the global tracer
+ mTracer = io.opentracing.util.GlobalTracer.get();
// initialize the Mongo database
mDatabase = MongoDriver.getDatabase("rest_spark");
@@ -28,17 +27,17 @@ public class Hello {
try (ActiveSpan activeSpan = mTracer.buildSpan("spark.request").startActive()) {
activeSpan.setTag("http.url", req.url());
- String id = req.params(":id");
+ final String id = req.params(":id");
// create a collection
- Document doc =
+ final Document doc =
new Document("name", "MongoDB")
.append("type", "database")
.append("identifier", id)
.append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
.append("info", new Document("x", 203).append("y", 102));
- MongoCollection<Document> collection = mDatabase.getCollection("calls");
+ final MongoCollection<Document> collection = mDatabase.getCollection("calls");
collection.insertOne(doc);
// write the count somewhere

MODIFY
dd-trace-examples/rest-spark/src/main/java/MongoDriver.java
dd-trace-examples/rest-spark/src/main/java/MongoDriver.java
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
-import io.opentracing.Tracer;
-import io.opentracing.contrib.mongo.TracingMongoClient;
public class MongoDriver {
- public static MongoDatabase getDatabase(String dbName) {
- Tracer tracer = io.opentracing.util.GlobalTracer.get();
- MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
- MongoClient mongoClient = new TracingMongoClient(tracer, connectionString);
+ public static MongoDatabase getDatabase(final String dbName) {
+ final MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
+ final MongoClient mongoClient = new MongoClient(connectionString);
return mongoClient.getDatabase(dbName);
}
}

[example] update Spark example so it runs with dd-java-agent