Simplify some tests by using runUnderTrace
MODIFY
instrumentation/hibernate/core-3.3/src/test/groovy/SessionTest.groovy
instrumentation/hibernate/core-3.3/src/test/groovy/SessionTest.groovy
@@ -23,6 +23,7 @@ import org.hibernate.ReplicationMode
import org.hibernate.Session
import spock.lang.Shared
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
import static io.opentelemetry.trace.Span.Kind.CLIENT
import static io.opentelemetry.trace.Span.Kind.INTERNAL
@@ -532,9 +533,7 @@ class SessionTest extends AbstractHibernateTest {
def "test hibernate overlapping Sessions"() {
setup:
- def rootSpan = TEST_TRACER.spanBuilder("overlapping Sessions").startSpan()
- def scope = TEST_TRACER.withSpan(rootSpan)
-
+ runUnderTrace("overlapping Sessions") {
def session1 = sessionFactory.openSession()
session1.beginTransaction()
def session2 = sessionFactory.openStatelessSession()
@@ -550,10 +549,7 @@ class SessionTest extends AbstractHibernateTest {
session1.getTransaction().commit()
session1.close()
session3.close()
-
- rootSpan.end()
- scope.close()
-
+ }
expect:
assertTraces(1) {

MODIFY
instrumentation/hibernate/core-4.0/src/test/groovy/SessionTest.groovy
instrumentation/hibernate/core-4.0/src/test/groovy/SessionTest.groovy
@@ -23,6 +23,7 @@ import org.hibernate.ReplicationMode
import org.hibernate.Session
import spock.lang.Shared
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
import static io.opentelemetry.trace.Span.Kind.CLIENT
import static io.opentelemetry.trace.Span.Kind.INTERNAL
@@ -460,9 +461,7 @@ class SessionTest extends AbstractHibernateTest {
def "test hibernate overlapping Sessions"() {
setup:
- def rootSpan = TEST_TRACER.spanBuilder("overlapping Sessions").startSpan()
- def scope = TEST_TRACER.withSpan(rootSpan)
-
+ runUnderTrace("overlapping Sessions") {
def session1 = sessionFactory.openSession()
session1.beginTransaction()
def session2 = sessionFactory.openStatelessSession()
@@ -478,10 +477,7 @@ class SessionTest extends AbstractHibernateTest {
session1.getTransaction().commit()
session1.close()
session3.close()
-
- rootSpan.end()
- scope.close()
-
+ }
expect:
assertTraces(1) {

MODIFY
instrumentation/java-concurrent/akka-2.5-testing/src/test/groovy/AkkaExecutorInstrumentationTest.groovy
instrumentation/java-concurrent/akka-2.5-testing/src/test/groovy/AkkaExecutorInstrumentationTest.groovy
@@ -27,6 +27,8 @@ import java.util.concurrent.RejectedExecutionException
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
+
/**
* Test executor instrumentation for Akka specific classes.
* This is to large extent a copy of ExecutorInstrumentationTest.
@@ -54,9 +56,7 @@ class AkkaExecutorInstrumentationTest extends AgentTestRunner {
new Runnable() {
@Override
void run() {
- def parentSpan = TEST_TRACER.spanBuilder("parent").startSpan()
- def parentScope = TEST_TRACER.withSpan(parentSpan)
- try {
+ runUnderTrace("parent") {
// this child will have a span
def child1 = new AkkaAsyncChild()
// this child won't
@@ -65,9 +65,6 @@ class AkkaExecutorInstrumentationTest extends AgentTestRunner {
m(pool, child2)
child1.waitForCompletion()
child2.waitForCompletion()
- } finally {
- parentSpan.end()
- parentScope.close()
}
}
}.run()
@@ -111,9 +108,7 @@ class AkkaExecutorInstrumentationTest extends AgentTestRunner {
new Runnable() {
@Override
void run() {
- def parentSpan = TEST_TRACER.spanBuilder("parent").startSpan()
- def parentScope = TEST_TRACER.withSpan(parentSpan)
- try {
+ runUnderTrace("parent") {
try {
for (int i = 0; i < 20; ++i) {
// Our current instrumentation instrumentation does not behave very well
@@ -140,9 +135,6 @@ class AkkaExecutorInstrumentationTest extends AgentTestRunner {
for (AkkaAsyncChild child : children) {
child.unblock()
}
- } finally {
- parentSpan.end()
- parentScope.close()
}
}
}.run()

MODIFY
instrumentation/java-concurrent/scala-testing/src/test/groovy/ScalaExecutorInstrumentationTest.groovy
instrumentation/java-concurrent/scala-testing/src/test/groovy/ScalaExecutorInstrumentationTest.groovy
@@ -27,6 +27,8 @@ import java.util.concurrent.RejectedExecutionException
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
+
/**
* Test executor instrumentation for Scala specific classes.
* This is to large extent a copy of ExecutorInstrumentationTest.
@@ -54,9 +56,7 @@ class ScalaExecutorInstrumentationTest extends AgentTestRunner {
new Runnable() {
@Override
void run() {
- def parentSpan = TEST_TRACER.spanBuilder("parent").startSpan()
- def parentScope = TEST_TRACER.withSpan(parentSpan)
- try {
+ runUnderTrace("parent") {
// this child will have a span
def child1 = new ScalaAsyncChild()
// this child won't
@@ -65,9 +65,6 @@ class ScalaExecutorInstrumentationTest extends AgentTestRunner {
m(pool, child2)
child1.waitForCompletion()
child2.waitForCompletion()
- } finally {
- parentSpan.end()
- parentScope.close()
}
}
}.run()
@@ -111,9 +108,7 @@ class ScalaExecutorInstrumentationTest extends AgentTestRunner {
new Runnable() {
@Override
void run() {
- def parentSpan = TEST_TRACER.spanBuilder("parent").startSpan()
- def parentScope = TEST_TRACER.withSpan(parentSpan)
- try {
+ runUnderTrace("parent") {
try {
for (int i = 0; i < 20; ++i) {
// Our current instrumentation instrumentation does not behave very well
@@ -140,9 +135,6 @@ class ScalaExecutorInstrumentationTest extends AgentTestRunner {
for (ScalaAsyncChild child : children) {
child.unblock()
}
- } finally {
- parentSpan.end()
- parentScope.close()
}
}
}.run()

MODIFY
instrumentation/java-concurrent/src/slickTest/groovy/CompletableFutureTest.groovy
instrumentation/java-concurrent/src/slickTest/groovy/CompletableFutureTest.groovy
@@ -23,6 +23,8 @@ import java.util.concurrent.TimeUnit
import java.util.function.Function
import java.util.function.Supplier
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
+
/**
* Note: ideally this should live with the rest of ExecutorInstrumentationTest,
* but this code needs java8 so we put it here for now.
@@ -53,16 +55,11 @@ class CompletableFutureTest extends AgentTestRunner {
def result = new Supplier<String>() {
@Override
String get() {
- def parentSpan = TEST_TRACER.spanBuilder("parent").startSpan()
- def parentScope = TEST_TRACER.withSpan(parentSpan)
- try {
+ runUnderTrace("parent") {
return CompletableFuture.supplyAsync(supplier, pool)
.thenCompose({ s -> CompletableFuture.supplyAsync(new AppendingSupplier(s), differentPool) })
.thenApply(function)
.get()
- } finally {
- parentSpan.end()
- parentScope.close()
}
}
}.get()

MODIFY
instrumentation/java-concurrent/src/test/groovy/ExecutorInstrumentationTest.groovy
instrumentation/java-concurrent/src/test/groovy/ExecutorInstrumentationTest.groovy
@@ -37,6 +37,7 @@ import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
import static org.junit.Assume.assumeTrue
class ExecutorInstrumentationTest extends AgentTestRunner {
@@ -81,9 +82,7 @@ class ExecutorInstrumentationTest extends AgentTestRunner {
new Runnable() {
@Override
void run() {
- def parentSpan = TEST_TRACER.spanBuilder("parent").startSpan()
- def parentScope = TEST_TRACER.withSpan(parentSpan)
- try {
+ runUnderTrace("parent") {
// this child will have a span
def child1 = new JavaAsyncChild()
// this child won't
@@ -92,9 +91,6 @@ class ExecutorInstrumentationTest extends AgentTestRunner {
m(pool, child2)
child1.waitForCompletion()
child2.waitForCompletion()
- } finally {
- parentSpan.end()
- parentScope.close()
}
}
}.run()
@@ -171,13 +167,8 @@ class ExecutorInstrumentationTest extends AgentTestRunner {
new Runnable() {
@Override
void run() {
- def parentSpan = TEST_TRACER.spanBuilder("parent").startSpan()
- def parentScope = TEST_TRACER.withSpan(parentSpan)
- try {
+ runUnderTrace("parent") {
m(pool, w(child))
- } finally {
- parentSpan.end()
- parentScope.close()
}
}
}.run()
@@ -219,9 +210,7 @@ class ExecutorInstrumentationTest extends AgentTestRunner {
new Runnable() {
@Override
void run() {
- def parentSpan = TEST_TRACER.spanBuilder("parent").startSpan()
- def parentScope = TEST_TRACER.withSpan(parentSpan)
- try {
+ runUnderTrace("parent") {
try {
for (int i = 0; i < 20; ++i) {
// Our current instrumentation instrumentation does not behave very well
@@ -248,9 +237,6 @@ class ExecutorInstrumentationTest extends AgentTestRunner {
for (JavaAsyncChild child : children) {
child.unblock()
}
- } finally {
- parentSpan.end()
- parentScope.close()
}
}
}.run()

MODIFY
instrumentation/netty-4.1/src/test/groovy/Netty41ClientTest.groovy
instrumentation/netty-4.1/src/test/groovy/Netty41ClientTest.groovy
@@ -226,13 +226,8 @@ class Netty41ClientTest extends HttpClientTest<NettyHttpClientDecorator> {
class TracedClass {
int tracedMethod(String method) {
- def span = TEST_TRACER.spanBuilder("tracedMethod").startSpan()
- def scope = TEST_TRACER.withSpan(span)
- try {
- return doRequest(method, server.address.resolve("/success"))
- } finally {
- span.end()
- scope.close()
+ runUnderTrace("tracedMethod") {
+ doRequest(method, server.address.resolve("/success"))
}
}
}

MODIFY
instrumentation/opentelemetry-api-0.2/src/test/groovy/OpenTelemetryApiTest.groovy
instrumentation/opentelemetry-api-0.2/src/test/groovy/OpenTelemetryApiTest.groovy
@@ -17,6 +17,7 @@ import io.opentelemetry.auto.test.AgentTestRunner
import unshaded.io.opentelemetry.OpenTelemetry
import unshaded.io.opentelemetry.trace.Status
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
import static unshaded.io.opentelemetry.trace.Span.Kind.PRODUCER
class OpenTelemetryApiTest extends AgentTestRunner {
@@ -54,12 +55,10 @@ class OpenTelemetryApiTest extends AgentTestRunner {
def "capture span with implicit parent"() {
when:
def tracer = OpenTelemetry.getTracerFactory().get("test")
- def parentSpan = tracer.spanBuilder("parent").startSpan()
- def parentScope = tracer.withSpan(parentSpan)
+ runUnderTrace("parent") {
def testSpan = tracer.spanBuilder("test").startSpan()
testSpan.end()
- parentSpan.end()
- parentScope.close()
+ }
then:
assertTraces(1) {

MODIFY
instrumentation/twilio-6.6/src/test/groovy/test/TwilioClientTest.groovy
instrumentation/twilio-6.6/src/test/groovy/test/TwilioClientTest.groovy
@@ -39,6 +39,7 @@ import org.apache.http.impl.client.HttpClientBuilder
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
import static io.opentelemetry.trace.Span.Kind.CLIENT
class TwilioClientTest extends AgentTestRunner {
@@ -126,16 +127,13 @@ class TwilioClientTest extends AgentTestRunner {
1 * twilioRestClient.request(_) >> new Response(new ByteArrayInputStream(MESSAGE_RESPONSE_BODY.getBytes()), 200)
- def testSpan = TEST_TRACER.spanBuilder("test").startSpan()
- def testScope = TEST_TRACER.withSpan(testSpan)
-
- Message message = Message.creator(
+ Message message = runUnderTrace("test") {
+ Message.creator(
new PhoneNumber("+1 555 720 5913"), // To number
new PhoneNumber("+1 555 555 5215"), // From number
"Hello world!" // SMS body
).create(twilioRestClient)
- testSpan.end()
- testScope.close()
+ }
expect:
@@ -175,19 +173,15 @@ class TwilioClientTest extends AgentTestRunner {
1 * twilioRestClient.request(_) >> new Response(new ByteArrayInputStream(CALL_RESPONSE_BODY.getBytes()), 200)
- def testSpan = TEST_TRACER.spanBuilder("test").startSpan()
- def testScope = TEST_TRACER.withSpan(testSpan)
-
- Call call = Call.creator(
+ Call call = runUnderTrace("test") {
+ Call.creator(
new PhoneNumber("+15558881234"), // To number
new PhoneNumber("+15559994321"), // From number
// Read TwiML at this URL when a call connects (hold music)
new URI("http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient")
).create(twilioRestClient)
-
- testSpan.end()
- testScope.close()
+ }
expect:
@@ -251,18 +245,13 @@ class TwilioClientTest extends AgentTestRunner {
.httpClient(networkHttpClient)
.build()
-
- def testSpan = TEST_TRACER.spanBuilder("test").startSpan()
- def testScope = TEST_TRACER.withSpan(testSpan)
-
- Message message = Message.creator(
+ Message message = runUnderTrace("test") {
+ Message.creator(
new PhoneNumber("+1 555 720 5913"), // To number
new PhoneNumber("+1 555 555 5215"), // From number
"Hello world!" // SMS body
).create(realTwilioRestClient)
-
- testSpan.end()
- testScope.close()
+ }
expect:
@@ -355,16 +344,13 @@ class TwilioClientTest extends AgentTestRunner {
.httpClient(networkHttpClient)
.build()
- def testSpan = TEST_TRACER.spanBuilder("test").startSpan()
- def testScope = TEST_TRACER.withSpan(testSpan)
-
- Message message = Message.creator(
+ Message message = runUnderTrace("test") {
+ Message.creator(
new PhoneNumber("+1 555 720 5913"), // To number
new PhoneNumber("+1 555 555 5215"), // From number
"Hello world!" // SMS body
).create(realTwilioRestClient)
- testSpan.end()
- testScope.close()
+ }
expect:
@@ -470,8 +456,7 @@ class TwilioClientTest extends AgentTestRunner {
.httpClient(networkHttpClient)
.build()
- def testSpan = TEST_TRACER.spanBuilder("test").startSpan()
- def testScope = TEST_TRACER.withSpan(testSpan)
+ Message message = runUnderTrace("test") {
ListenableFuture<Message> future = Message.creator(
new PhoneNumber("+1 555 720 5913"), // To number
@@ -479,14 +464,12 @@ class TwilioClientTest extends AgentTestRunner {
"Hello world!" // SMS body
).createAsync(realTwilioRestClient)
- Message message
try {
- message = future.get(10, TimeUnit.SECONDS)
+ return future.get(10, TimeUnit.SECONDS)
} finally {
// Give the future callback a chance to run
Thread.sleep(1000)
- testSpan.end()
- testScope.close()
+ }
}
expect:
@@ -663,8 +646,7 @@ class TwilioClientTest extends AgentTestRunner {
when:
- def testSpan = TEST_TRACER.spanBuilder("test").startSpan()
- def testScope = TEST_TRACER.withSpan(testSpan)
+ Message message = runUnderTrace("test") {
ListenableFuture<Message> future = Message.creator(
new PhoneNumber("+1 555 720 5913"), // To number
@@ -672,14 +654,12 @@ class TwilioClientTest extends AgentTestRunner {
"Hello world!" // SMS body
).createAsync(twilioRestClient)
- Message message
try {
- message = future.get(10, TimeUnit.SECONDS)
+ return future.get(10, TimeUnit.SECONDS)
} finally {
// Give the future callback a chance to run
Thread.sleep(1000)
- testSpan.end()
- testScope.close()
+ }
}
then:

MODIFY
testing/src/main/groovy/io/opentelemetry/auto/test/log/events/LogEventsTestBase.groovy
testing/src/main/groovy/io/opentelemetry/auto/test/log/events/LogEventsTestBase.groovy
@@ -22,6 +22,7 @@ import io.opentelemetry.trace.Tracer
import spock.lang.Unroll
import static io.opentelemetry.auto.test.utils.ConfigUtils.withConfigOverride
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
/**
* This class represents the standard test cases that new logging library integrations MUST
@@ -44,16 +45,12 @@ abstract class LogEventsTestBase extends AgentTestRunner {
def "capture #testMethod (#capture)"() {
setup:
- def parentSpan = tracer.spanBuilder("test").startSpan()
- def parentScope = tracer.withSpan(parentSpan)
-
+ runUnderTrace("test") {
def logger = createLogger("abc")
withConfigOverride(Config.LOGS_EVENTS_THRESHOLD, "WARN") {
logger."$testMethod"("xyz")
}
-
- parentSpan.end()
- parentScope.close()
+ }
expect:
assertTraces(1) {

Simplify some tests by using runUnderTrace (#204)