Get initial hibernate tests working
MODIFY
dd-java-agent/instrumentation/hibernate/hibernate.gradle
dd-java-agent/instrumentation/hibernate/hibernate.gradle
@@ -12,13 +12,6 @@ project.ext {
// versions = "[5.4.2.Final]"
// assertInverse = true
// }
-
-// fail {
-// group = "org.elasticsearch.client"
-// module = "rest"
-// versions = "(,)"
-// assertInverse = true
-// }
//}
apply from: "${rootDir}/gradle/java.gradle"
@@ -47,22 +40,4 @@ dependencies {
testCompile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.1.Final'
testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
-// testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.5' // todo log4j
-
-// // Include httpclient instrumentation for testing because it is a dependency for elasticsearch-rest-client.
-// // It doesn't actually work though. They use HttpAsyncClient, which isn't currently instrumented.
-// // TODO: add Apache's HttpAsyncClient instrumentation when that is complete.
-// testCompile project(':dd-java-agent:instrumentation:apache-httpclient-4')
-//
-// testCompile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0'
-// testCompile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.0'
-//
-// testCompile group: 'org.elasticsearch.client', name: 'elasticsearch-rest-client', version: '6.4.0'
-// testCompile group: 'org.elasticsearch', name: 'elasticsearch', version: '6.4.0'
-// testCompile group: 'org.elasticsearch.plugin', name: 'transport-netty4-client', version: '6.4.0'
-//
-// latestDepTestCompile group: 'org.elasticsearch.client', name: 'elasticsearch-rest-client', version: '+'
-// latestDepTestCompile group: 'org.elasticsearch.client', name: 'transport', version: '6.+'
-// latestDepTestCompile group: 'org.elasticsearch', name: 'elasticsearch', version: '6.+'
-// latestDepTestCompile group: 'org.elasticsearch.plugin', name: 'transport-netty4-client', version: '6.+'
}

RENAME
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/HibernateInstrumentation.java
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/SessionInstrumentation.java
@@ -35,9 +35,9 @@ import org.hibernate.query.Query;
import org.hibernate.query.spi.QueryImplementor;
@AutoService(Instrumenter.class)
-public class HibernateInstrumentation extends Instrumenter.Default {
+public class SessionInstrumentation extends Instrumenter.Default {
- public HibernateInstrumentation() {
+ public SessionInstrumentation() {
super("hibernate");
}

ADD
dd-java-agent/instrumentation/hibernate/src/test/groovy/QueryTest.groovy
+import datadog.trace.agent.test.AgentTestRunner
+import datadog.trace.api.DDSpanTypes
+import datadog.trace.api.DDTags
+import io.opentracing.tag.Tags
+import org.hibernate.Session
+import org.hibernate.SessionFactory
+import org.hibernate.boot.MetadataSources
+import org.hibernate.boot.registry.StandardServiceRegistry
+import org.hibernate.boot.registry.StandardServiceRegistryBuilder
+import spock.lang.Shared
+
+class QueryTest extends AgentTestRunner {
+
+ @Shared
+ private SessionFactory sessionFactory
+
+ def setupSpec() {
+ final StandardServiceRegistry registry =
+ new StandardServiceRegistryBuilder()
+ .configure()
+ .build()
+ try {
+ sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory()
+ } catch (Exception e) {
+ StandardServiceRegistryBuilder.destroy(registry)
+ return
+ }
+
+ Session session = sessionFactory.openSession()
+ session.beginTransaction()
+ session.save(new Value("A Hibernate value to be serialized"))
+ session.save(new Value("Another value"))
+ session.getTransaction().commit()
+ session.close()
+ }
+
+ def cleanupSpec() {
+ if (sessionFactory != null) {
+ sessionFactory.close()
+ }
+ }
+
+ def "test hibernate query"() {
+ setup:
+
+ Session session = sessionFactory.openSession()
+ session.beginTransaction()
+ List result = session.createQuery("from Value").list()
+ for (Value value : (List<Value>) result) {
+ System.out.println(value.getName())
+ }
+ session.getTransaction().commit()
+ session.close()
+
+ expect:
+ result.size() == 2
+
+ assertTraces(1) {
+ trace(0, 3) {
+ span(0) {
+ serviceName "hibernate"
+ resourceName "hibernate.session"
+ operationName "hibernate.session"
+ spanType DDSpanTypes.HIBERNATE
+ parent()
+ tags {
+ "$Tags.COMPONENT.key" "hibernate-java"
+ "$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
+ "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
+ defaultTags()
+ }
+ }
+ span(1) {
+ serviceName "hibernate"
+ resourceName "hibernate.transaction.commit"
+ operationName "hibernate.transaction.commit"
+ spanType DDSpanTypes.HIBERNATE
+ childOf span(0)
+ tags {
+ "$Tags.COMPONENT.key" "hibernate-java"
+ "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
+ defaultTags()
+ }
+ }
+ span(2) {
+ serviceName "hibernate"
+ resourceName "hibernate.query.list"
+ operationName "hibernate.query.list"
+ spanType DDSpanTypes.HIBERNATE
+ childOf span(0)
+ tags {
+ "$Tags.COMPONENT.key" "hibernate-java"
+ "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
+ defaultTags()
+ }
+ }
+ }
+ }
+ }
+
+}

RENAME
dd-java-agent/instrumentation/hibernate/src/test/groovy/HibernateTest.groovy
dd-java-agent/instrumentation/hibernate/src/test/groovy/SessionTest.groovy
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.api.DDSpanTypes
+import datadog.trace.api.DDTags
+import io.opentracing.tag.Tags
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.hibernate.boot.MetadataSources
@@ -7,7 +9,7 @@ import org.hibernate.boot.registry.StandardServiceRegistry
import org.hibernate.boot.registry.StandardServiceRegistryBuilder
import spock.lang.Shared
-class HibernateTest extends AgentTestRunner {
+class SessionTest extends AgentTestRunner {
@Shared
private SessionFactory sessionFactory
@@ -20,10 +22,8 @@ class HibernateTest extends AgentTestRunner {
try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
- System.out.println(e.toString());
- StandardServiceRegistryBuilder.destroy(registry);
+ StandardServiceRegistryBuilder.destroy(registry)
}
-
}
def cleanupSpec() {
@@ -41,18 +41,7 @@ class HibernateTest extends AgentTestRunner {
session.getTransaction().commit()
session.close()
-// session = sessionFactory.openSession()
-// session.beginTransaction()
-// List result = session.createQuery("from Value").list()
-// for (Value value : (List<Value>) result) {
-// System.out.println(value.getName())
-// }
-// session.getTransaction().commit()
-// session.close()
-
expect:
-// result.size() == 2
-
assertTraces(1) {
trace(0, 4) {
span(0) {
@@ -68,8 +57,43 @@ class HibernateTest extends AgentTestRunner {
defaultTags()
}
}
+ span(1) {
+ serviceName "hibernate"
+ resourceName "hibernate.transaction.commit"
+ operationName "hibernate.transaction.commit"
+ spanType DDSpanTypes.HIBERNATE
+ childOf span(0)
+ tags {
+ "$Tags.COMPONENT.key" "hibernate-java"
+ "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
+ defaultTags()
+ }
+ }
+ span(2) {
+ serviceName "hibernate"
+ resourceName "hibernate.save"
+ operationName "hibernate.save"
+ spanType DDSpanTypes.HIBERNATE
+ childOf span(0)
+ tags {
+ "$Tags.COMPONENT.key" "hibernate-java"
+ "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
+ defaultTags()
+ }
+ }
+ span(3) {
+ serviceName "hibernate"
+ resourceName "hibernate.save"
+ operationName "hibernate.save"
+ spanType DDSpanTypes.HIBERNATE
+ childOf span(0)
+ tags {
+ "$Tags.COMPONENT.key" "hibernate-java"
+ "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
+ defaultTags()
+ }
+ }
}
}
}
-
}

Get initial hibernate tests working