From da58bb89e9d62c4c5c5b9ed8ce1ce77f2d4911db Mon Sep 17 00:00:00 2001 From: Emanuele Palazzetti Date: Thu, 20 Jul 2017 20:55:10 +0200 Subject: [PATCH 1/6] [example] add Spark framework example --- dd-trace-examples/rest-spark/.gitignore | 25 ++++++++++++++++++++ dd-trace-examples/rest-spark/build.gradle | 21 ++++++++++++++++ dd-trace-examples/rest-spark/settings.gradle | 1 + 3 files changed, 47 insertions(+) create mode 100644 dd-trace-examples/rest-spark/.gitignore create mode 100644 dd-trace-examples/rest-spark/build.gradle create mode 100644 dd-trace-examples/rest-spark/settings.gradle diff --git a/dd-trace-examples/rest-spark/.gitignore b/dd-trace-examples/rest-spark/.gitignore new file mode 100644 index 0000000000..d7242d926c --- /dev/null +++ b/dd-trace-examples/rest-spark/.gitignore @@ -0,0 +1,25 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# IDE +.idea/ diff --git a/dd-trace-examples/rest-spark/build.gradle b/dd-trace-examples/rest-spark/build.gradle new file mode 100644 index 0000000000..217fe34160 --- /dev/null +++ b/dd-trace-examples/rest-spark/build.gradle @@ -0,0 +1,21 @@ +group 'me.palazzetti' +version '0.1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + 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' + + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/dd-trace-examples/rest-spark/settings.gradle b/dd-trace-examples/rest-spark/settings.gradle new file mode 100644 index 0000000000..7af1c35b92 --- /dev/null +++ b/dd-trace-examples/rest-spark/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'rest-spark' From ee84eb689cbbeb6420b0ec4202568cf62e0974d0 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Tue, 25 Jul 2017 10:25:46 +0200 Subject: [PATCH 2/6] Formatting --- dd-trace-examples/rest-spark/.gitignore | 25 -------------------- dd-trace-examples/rest-spark/build.gradle | 21 ---------------- dd-trace-examples/rest-spark/settings.gradle | 1 - 3 files changed, 47 deletions(-) delete mode 100644 dd-trace-examples/rest-spark/.gitignore delete mode 100644 dd-trace-examples/rest-spark/build.gradle delete mode 100644 dd-trace-examples/rest-spark/settings.gradle diff --git a/dd-trace-examples/rest-spark/.gitignore b/dd-trace-examples/rest-spark/.gitignore deleted file mode 100644 index d7242d926c..0000000000 --- a/dd-trace-examples/rest-spark/.gitignore +++ /dev/null @@ -1,25 +0,0 @@ -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -# IDE -.idea/ diff --git a/dd-trace-examples/rest-spark/build.gradle b/dd-trace-examples/rest-spark/build.gradle deleted file mode 100644 index 217fe34160..0000000000 --- a/dd-trace-examples/rest-spark/build.gradle +++ /dev/null @@ -1,21 +0,0 @@ -group 'me.palazzetti' -version '0.1.0-SNAPSHOT' - -apply plugin: 'java' - -sourceCompatibility = 1.8 - -repositories { - mavenCentral() -} - -dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - 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' - - testCompile group: 'junit', name: 'junit', version: '4.12' -} diff --git a/dd-trace-examples/rest-spark/settings.gradle b/dd-trace-examples/rest-spark/settings.gradle deleted file mode 100644 index 7af1c35b92..0000000000 --- a/dd-trace-examples/rest-spark/settings.gradle +++ /dev/null @@ -1 +0,0 @@ -rootProject.name = 'rest-spark' From 7dc170cc6f743579503f6c61893575dfe8ac7219 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 27 Jul 2017 15:07:33 +0200 Subject: [PATCH 3/6] mongo helper for norm --- .../trace/agent/integration/MongoHelper.java | 46 +++++++++++++++++-- dd-java-agent/src/main/resources/otarules.btm | 11 +++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java b/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java index 5fb586628b..ad6b831ca6 100644 --- a/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java +++ b/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java @@ -1,13 +1,28 @@ package com.datadoghq.trace.agent.integration; +import com.datadoghq.trace.DDTags; import com.mongodb.MongoClientOptions; +import com.mongodb.event.CommandStartedEvent; +import io.opentracing.Span; import io.opentracing.contrib.mongo.TracingCommandListener; +import lombok.extern.slf4j.Slf4j; +import org.bson.BsonDocument; +import org.bson.BsonString; +import org.bson.BsonValue; import org.jboss.byteman.rule.Rule; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + /** Patch the Mongo builder before constructing the final client */ +@Slf4j public class MongoHelper extends DDAgentTracingHelper { - public MongoHelper(Rule rule) { + private static final List WHILDCARD_FIELDS = Arrays.asList("ordered", "insert"); + private static final BsonValue HIDDEN_CAR = new BsonString("?"); + + public MongoHelper(final Rule rule) { super(rule); } @@ -20,14 +35,39 @@ public class MongoHelper extends DDAgentTracingHelper entry : origin.entrySet()) { + if (WHILDCARD_FIELDS.contains(entry.getKey())) { + normalized.put(entry.getKey(), entry.getValue()); + } else if (entry.getValue().isDocument()) { + final BsonDocument child = new BsonDocument(); + normalized.put(entry.getKey(), child); + norm(entry.getValue().asDocument(), child); + } else { + normalized.put(entry.getKey(), HIDDEN_CAR); + } + } + } } diff --git a/dd-java-agent/src/main/resources/otarules.btm b/dd-java-agent/src/main/resources/otarules.btm index 86bdb75892..a38c9ab964 100644 --- a/dd-java-agent/src/main/resources/otarules.btm +++ b/dd-java-agent/src/main/resources/otarules.btm @@ -20,6 +20,17 @@ DO patch($this); ENDRULE +RULE mongo-decorator +CLASS io.opentracing.contrib.mongo.TracingCommandListener +METHOD decorate +HELPER com.datadoghq.trace.agent.integration.MongoHelper +AT EXIT +IF TRUE +DO + decorate($1, $2); +ENDRULE + + # Instrument AWS SDK client # ========================== From b652c252420db0edaf82ca443ab3b5f070f246cd Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 27 Jul 2017 15:08:11 +0200 Subject: [PATCH 4/6] some stuff needed --- .../rest-spark/rest-spark.gradle | 4 +++ .../java/com/datadoghq/trace/DDBaseSpan.java | 3 +- .../DBStatementAsResourceName.java | 35 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/dd-trace-examples/rest-spark/rest-spark.gradle b/dd-trace-examples/rest-spark/rest-spark.gradle index 495df5a4e5..a203dad0e5 100644 --- a/dd-trace-examples/rest-spark/rest-spark.gradle +++ b/dd-trace-examples/rest-spark/rest-spark.gradle @@ -16,4 +16,8 @@ dependencies { 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' + compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3' + compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' + + } diff --git a/dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java b/dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java index c79a4a2ca2..0058084a25 100644 --- a/dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java +++ b/dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java @@ -4,12 +4,13 @@ import com.datadoghq.trace.util.Clock; import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonIgnore; import io.opentracing.BaseSpan; +import lombok.extern.slf4j.Slf4j; + import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Queue; import java.util.concurrent.TimeUnit; -import lombok.extern.slf4j.Slf4j; @Slf4j public abstract class DDBaseSpan implements BaseSpan { diff --git a/dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java b/dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java index a31a7044cb..4b8a4511ad 100644 --- a/dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java +++ b/dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java @@ -10,4 +10,39 @@ public class DBStatementAsResourceName extends DDSpanContextDecorator { this.setMatchingTag(Tags.DB_STATEMENT.getKey()); this.setSetTag(DDTags.RESOURCE_NAME); } + + //{ "insert" : "calls", "ordered" : true, "documents" : [{ "_id" : { "$oid" : "5979bbb0ed6fed5749cc9e7c" }, "name" : "MongoDB", "type" : "database", "identifier" : "10", "versions" : ["v3.2", "v3.0", "v2.6"], "info" : { "x" : 203, "y" : 102 } }] } + private void normalizeFilter(final Object f) { + + + } } + + /* +def normalize_filter(f=None): + if f is None: + return {} + elif isinstance(f, list): + # normalize lists of filters + # e.g. {$or: [ { age: { $lt: 30 } }, { type: 1 } ]} + return [normalize_filter(s) for s in f] + elif isinstance(f, dict): + # normalize dicts of filters + # {$or: [ { age: { $lt: 30 } }, { type: 1 } ]}) + out = {} + for k, v in iteritems(f): + if k == "$in" or k == "$nin": + # special case $in queries so we don't loop over lists. + out[k] = "?" + elif isinstance(v, list) or isinstance(v, dict): + # RECURSION ALERT: needs to move to the agent + out[k] = normalize_filter(v) + else: + # NOTE: this shouldn't happen, but let's have a safeguard. + out[k] = '?' + return out + else: + # FIXME[matt] unexpected type. not sure this should ever happen, but at + # least it won't crash. + return {}*/ + From f20ed93741fca1fb5d56a2bcbba840738f06a9e9 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 27 Jul 2017 15:10:49 +0200 Subject: [PATCH 5/6] formatting --- .../trace/agent/integration/MongoHelper.java | 7 +-- .../java/com/datadoghq/trace/DDBaseSpan.java | 3 +- .../DBStatementAsResourceName.java | 58 +++++++++---------- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java b/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java index ad6b831ca6..0c9a003a13 100644 --- a/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java +++ b/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java @@ -5,16 +5,15 @@ import com.mongodb.MongoClientOptions; import com.mongodb.event.CommandStartedEvent; import io.opentracing.Span; import io.opentracing.contrib.mongo.TracingCommandListener; +import java.util.Arrays; +import java.util.List; +import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.bson.BsonDocument; import org.bson.BsonString; import org.bson.BsonValue; import org.jboss.byteman.rule.Rule; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - /** Patch the Mongo builder before constructing the final client */ @Slf4j public class MongoHelper extends DDAgentTracingHelper { diff --git a/dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java b/dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java index 0058084a25..c79a4a2ca2 100644 --- a/dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java +++ b/dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java @@ -4,13 +4,12 @@ import com.datadoghq.trace.util.Clock; import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonIgnore; import io.opentracing.BaseSpan; -import lombok.extern.slf4j.Slf4j; - import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Queue; import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; @Slf4j public abstract class DDBaseSpan implements BaseSpan { diff --git a/dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java b/dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java index 4b8a4511ad..1113d0d635 100644 --- a/dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java +++ b/dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java @@ -12,37 +12,33 @@ public class DBStatementAsResourceName extends DDSpanContextDecorator { } //{ "insert" : "calls", "ordered" : true, "documents" : [{ "_id" : { "$oid" : "5979bbb0ed6fed5749cc9e7c" }, "name" : "MongoDB", "type" : "database", "identifier" : "10", "versions" : ["v3.2", "v3.0", "v2.6"], "info" : { "x" : 203, "y" : 102 } }] } - private void normalizeFilter(final Object f) { - - - } + private void normalizeFilter(final Object f) {} } /* -def normalize_filter(f=None): - if f is None: - return {} - elif isinstance(f, list): - # normalize lists of filters - # e.g. {$or: [ { age: { $lt: 30 } }, { type: 1 } ]} - return [normalize_filter(s) for s in f] - elif isinstance(f, dict): - # normalize dicts of filters - # {$or: [ { age: { $lt: 30 } }, { type: 1 } ]}) - out = {} - for k, v in iteritems(f): - if k == "$in" or k == "$nin": - # special case $in queries so we don't loop over lists. - out[k] = "?" - elif isinstance(v, list) or isinstance(v, dict): - # RECURSION ALERT: needs to move to the agent - out[k] = normalize_filter(v) - else: - # NOTE: this shouldn't happen, but let's have a safeguard. - out[k] = '?' - return out - else: - # FIXME[matt] unexpected type. not sure this should ever happen, but at - # least it won't crash. - return {}*/ - + def normalize_filter(f=None): + if f is None: + return {} + elif isinstance(f, list): + # normalize lists of filters + # e.g. {$or: [ { age: { $lt: 30 } }, { type: 1 } ]} + return [normalize_filter(s) for s in f] + elif isinstance(f, dict): + # normalize dicts of filters + # {$or: [ { age: { $lt: 30 } }, { type: 1 } ]}) + out = {} + for k, v in iteritems(f): + if k == "$in" or k == "$nin": + # special case $in queries so we don't loop over lists. + out[k] = "?" + elif isinstance(v, list) or isinstance(v, dict): + # RECURSION ALERT: needs to move to the agent + out[k] = normalize_filter(v) + else: + # NOTE: this shouldn't happen, but let's have a safeguard. + out[k] = '?' + return out + else: + # FIXME[matt] unexpected type. not sure this should ever happen, but at + # least it won't crash. + return {}*/ From e830e1b7e645dcaf726ede36f12315e6c4b3b8cc Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Fri, 28 Jul 2017 09:09:19 +0200 Subject: [PATCH 6/6] fix the operation name --- .../com/datadoghq/trace/agent/integration/MongoHelper.java | 6 ++++-- dd-trace/src/main/resources/dd-trace-decorators.yaml | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java b/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java index 0c9a003a13..454ccca5a4 100644 --- a/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java +++ b/dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/MongoHelper.java @@ -50,9 +50,11 @@ public class MongoHelper extends DDAgentTracingHelper