Dont load hibernate instrumentation unless correct version of hibernate is on the classpath
ADD
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/AbstractHibernateInstrumentation.java
+package datadog.trace.instrumentation.hibernate.core.v3_5;
+
+import static datadog.trace.agent.tooling.ClassLoaderMatcher.classLoaderHasClasses;
+import static net.bytebuddy.matcher.ElementMatchers.not;
+
+import datadog.trace.agent.tooling.Instrumenter;
+import net.bytebuddy.matcher.ElementMatcher;
+
+public abstract class AbstractHibernateInstrumentation extends Instrumenter.Default {
+
+ public AbstractHibernateInstrumentation() {
+ super("hibernate", "hibernate-core");
+ }
+
+ @Override
+ public String[] helperClassNames() {
+ return new String[] {
+ "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
+ "datadog.trace.instrumentation.hibernate.SessionState",
+ "datadog.trace.agent.decorator.BaseDecorator",
+ "datadog.trace.agent.decorator.ClientDecorator",
+ "datadog.trace.agent.decorator.DatabaseClientDecorator",
+ "datadog.trace.agent.decorator.OrmClientDecorator",
+ "datadog.trace.instrumentation.hibernate.HibernateDecorator",
+ };
+ }
+
+ @Override
+ public ElementMatcher<ClassLoader> classLoaderMatcher() {
+ return not(classLoaderHasClasses("org.hibernate.SharedSessionContract"))
+ .and(classLoaderHasClasses("org.hibernate.EntityNameResolver"));
+ }
+}

MODIFY
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/CriteriaInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/CriteriaInstrumentation.java
@@ -22,30 +22,13 @@ import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.Criteria;
@AutoService(Instrumenter.class)
-public class CriteriaInstrumentation extends Instrumenter.Default {
-
- public CriteriaInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class CriteriaInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
return singletonMap("org.hibernate.Criteria", SessionState.class.getName());
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.Criteria")));

MODIFY
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/QueryInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/QueryInstrumentation.java
@@ -24,30 +24,13 @@ import org.hibernate.Query;
import org.hibernate.SQLQuery;
@AutoService(Instrumenter.class)
-public class QueryInstrumentation extends Instrumenter.Default {
-
- public QueryInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class QueryInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
return singletonMap("org.hibernate.Query", SessionState.class.getName());
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.Query")));

MODIFY
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/SessionFactoryInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/SessionFactoryInstrumentation.java
@@ -27,11 +27,7 @@ import org.hibernate.Session;
import org.hibernate.StatelessSession;
@AutoService(Instrumenter.class)
-public class SessionFactoryInstrumentation extends Instrumenter.Default {
-
- public SessionFactoryInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class SessionFactoryInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
@@ -42,18 +38,6 @@ public class SessionFactoryInstrumentation extends Instrumenter.Default {
return stores;
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.SessionFactory")));

MODIFY
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/SessionInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/SessionInstrumentation.java
@@ -33,11 +33,7 @@ import org.hibernate.StatelessSession;
import org.hibernate.Transaction;
@AutoService(Instrumenter.class)
-public class SessionInstrumentation extends Instrumenter.Default {
-
- public SessionInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class SessionInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
@@ -50,19 +46,6 @@ public class SessionInstrumentation extends Instrumenter.Default {
return Collections.unmodifiableMap(map);
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface())

MODIFY
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/TransactionInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-3.5/src/main/java/datadog/trace/instrumentation/hibernate/core/v3_5/TransactionInstrumentation.java
@@ -22,30 +22,13 @@ import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.Transaction;
@AutoService(Instrumenter.class)
-public class TransactionInstrumentation extends Instrumenter.Default {
-
- public TransactionInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class TransactionInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
return singletonMap("org.hibernate.Transaction", SessionState.class.getName());
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.Transaction")));

ADD
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/AbstractHibernateInstrumentation.java
+package datadog.trace.instrumentation.hibernate.core.v4_0;
+
+import static datadog.trace.agent.tooling.ClassLoaderMatcher.classLoaderHasClasses;
+
+import datadog.trace.agent.tooling.Instrumenter;
+import net.bytebuddy.matcher.ElementMatcher;
+
+public abstract class AbstractHibernateInstrumentation extends Instrumenter.Default {
+
+ public AbstractHibernateInstrumentation() {
+ super("hibernate", "hibernate-core");
+ }
+
+ @Override
+ public String[] helperClassNames() {
+ return new String[] {
+ "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
+ "datadog.trace.instrumentation.hibernate.SessionState",
+ "datadog.trace.agent.decorator.BaseDecorator",
+ "datadog.trace.agent.decorator.ClientDecorator",
+ "datadog.trace.agent.decorator.DatabaseClientDecorator",
+ "datadog.trace.agent.decorator.OrmClientDecorator",
+ "datadog.trace.instrumentation.hibernate.HibernateDecorator",
+ };
+ }
+
+ @Override
+ public ElementMatcher<ClassLoader> classLoaderMatcher() {
+ return classLoaderHasClasses("org.hibernate.SharedSessionContract");
+ }
+}

MODIFY
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/CriteriaInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/CriteriaInstrumentation.java
@@ -22,30 +22,13 @@ import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.Criteria;
@AutoService(Instrumenter.class)
-public class CriteriaInstrumentation extends Instrumenter.Default {
-
- public CriteriaInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class CriteriaInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
return singletonMap("org.hibernate.Criteria", SessionState.class.getName());
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.Criteria")));

MODIFY
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/QueryInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/QueryInstrumentation.java
@@ -24,30 +24,13 @@ import org.hibernate.Query;
import org.hibernate.SQLQuery;
@AutoService(Instrumenter.class)
-public class QueryInstrumentation extends Instrumenter.Default {
-
- public QueryInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class QueryInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
return singletonMap("org.hibernate.Query", SessionState.class.getName());
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.Query")));

MODIFY
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/SessionFactoryInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/SessionFactoryInstrumentation.java
@@ -25,29 +25,13 @@ import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.SharedSessionContract;
@AutoService(Instrumenter.class)
-public class SessionFactoryInstrumentation extends Instrumenter.Default {
-
- public SessionFactoryInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class SessionFactoryInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
return singletonMap("org.hibernate.SharedSessionContract", SessionState.class.getName());
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.SessionFactory")));

MODIFY
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/SessionInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/SessionInstrumentation.java
@@ -32,11 +32,7 @@ import org.hibernate.SharedSessionContract;
import org.hibernate.Transaction;
@AutoService(Instrumenter.class)
-public class SessionInstrumentation extends Instrumenter.Default {
-
- public SessionInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class SessionInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
@@ -48,19 +44,6 @@ public class SessionInstrumentation extends Instrumenter.Default {
return Collections.unmodifiableMap(map);
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.SharedSessionContract")));

MODIFY
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/TransactionInstrumentation.java
dd-java-agent/instrumentation/hibernate/core-4.0/src/main/java/datadog/trace/instrumentation/hibernate/core/v4_0/TransactionInstrumentation.java
@@ -22,30 +22,13 @@ import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.Transaction;
@AutoService(Instrumenter.class)
-public class TransactionInstrumentation extends Instrumenter.Default {
-
- public TransactionInstrumentation() {
- super("hibernate", "hibernate-core");
- }
+public class TransactionInstrumentation extends AbstractHibernateInstrumentation {
@Override
public Map<String, String> contextStore() {
return singletonMap("org.hibernate.Transaction", SessionState.class.getName());
}
- @Override
- public String[] helperClassNames() {
- return new String[] {
- "datadog.trace.instrumentation.hibernate.SessionMethodUtils",
- "datadog.trace.instrumentation.hibernate.SessionState",
- "datadog.trace.agent.decorator.BaseDecorator",
- "datadog.trace.agent.decorator.ClientDecorator",
- "datadog.trace.agent.decorator.DatabaseClientDecorator",
- "datadog.trace.agent.decorator.OrmClientDecorator",
- "datadog.trace.instrumentation.hibernate.HibernateDecorator",
- };
- }
-
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.Transaction")));

MODIFY
dd-java-agent/instrumentation/hibernate/core-4.3/core-4.3.gradle
dd-java-agent/instrumentation/hibernate/core-4.3/core-4.3.gradle
@@ -10,6 +10,7 @@ muzzle {
group = "org.hibernate"
module = "hibernate-core"
versions = "[4.3.0.Final,)"
+ assertInverse = true
}
}
@@ -27,7 +28,7 @@ dependencies {
compileOnly group: 'org.hibernate', name: 'hibernate-core', version: '4.3.0.Final'
compile project(':dd-java-agent:agent-tooling')
- compile project(':dd-java-agent:instrumentation:hibernate:core-4.0')
+ compile project(':dd-java-agent:instrumentation:hibernate')
compile deps.bytebuddy
compile deps.opentracing
@@ -36,6 +37,7 @@ dependencies {
testCompile project(':dd-java-agent:testing')
testCompile project(':dd-java-agent:instrumentation:jdbc')
+ testCompile project(':dd-java-agent:instrumentation:hibernate:core-4.0')
testCompile group: 'org.hibernate', name: 'hibernate-core', version: '4.3.0.Final'
testCompile group: 'org.hsqldb', name: 'hsqldb', version: '2.0.0'

Dont load hibernate instrumentation unless correct version of hibernate is on the classpath