[example] add Spark framework example (#60)

* [example] add Spark framework example
* [example] formatting
This commit is contained in:
Emanuele Palazzetti 2017-07-27 21:09:58 +02:00 committed by GitHub
parent 864db966f1
commit d10e8e0c34
6 changed files with 132 additions and 0 deletions

View File

@ -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"

View File

@ -0,0 +1,19 @@
apply from: "${rootDir}/gradle/java.gradle"
apply from: "${rootDir}/gradle/jacoco.gradle"
version = 'demo'
description = 'rest-spark'
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
// compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':dd-trace')
compile 'com.sparkjava:spark-core:2.6.0'
compile 'org.mongodb:mongodb-driver:3.4.2'
compile 'io.opentracing:opentracing-api:0.30.0'
compile 'io.opentracing:opentracing-util:0.30.0'
compile 'io.opentracing.contrib:opentracing-mongo-driver:0.0.2'
}

View File

@ -0,0 +1,51 @@
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;
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);
// initialize the Mongo database
mDatabase = MongoDriver.getDatabase("rest_spark");
// our routes
get("/healthz", (req, res) -> "OK!");
get(
"/key/:id",
(req, res) -> {
try (ActiveSpan activeSpan = mTracer.buildSpan("spark.request").startActive()) {
activeSpan.setTag("http.url", req.url());
String id = req.params(":id");
// create a collection
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");
collection.insertOne(doc);
// write the count somewhere
System.out.println(collection.count());
return "Stored!";
}
});
}
}

View File

@ -0,0 +1,15 @@
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);
return mongoClient.getDatabase(dbName);
}
}

View File

@ -0,0 +1,34 @@
# Main service name for the app
defaultServiceName: rest-spark
# The writer to use.
# Could be: LoggingWritter or DDAgentWriter (default)
writer:
# LoggingWriter: Spans are logged using the application configuration
# DDAgentWriter: Spans are forwarding to a Datadog trace Agent
# - Param 'host': the hostname where the DD trace Agent is running (default: localhost)
# - Param 'port': the port to reach the DD trace Agent (default: 8126)
type: DDAgentWriter
host: localhost
port: 8126
# The sampler to use.
# Could be: AllSampler (default) or RateSampler
sampler:
# AllSampler: all spans are reported to the writer
# RateSample: only a portion of spans are reported to the writer
# - Param 'rate': the portion of spans to keep
type: AllSampler
# Skip some traces if the root span tag values matches some regexp patterns
# skipTagsPatterns: {"http.url": ".*/demo/add.*"}
# Enable custom tracing (Custom annotations for now)
# enableCustomAnnotationTracingOver: ["io","org","com"]
# Disable some instrumentations
# disabledInstrumentations: ["opentracing-apache-httpclient", "opentracing-mongo-driver", "opentracing-web-servlet-filter"]
decorators:
- type: DBComponent
matchingValue: java-mongo
setValue: mongo-spark

View File

@ -6,6 +6,7 @@ include ':dd-java-agent-ittests'
include ':dd-trace-examples:async-tracing'
include ':dd-trace-examples:dropwizard-mongo-client'
include ':dd-trace-examples:spring-boot-jdbc'
include ':dd-trace-examples:rest-spark'
include ':dd-trace-examples'
include ':dd-trace-annotations'