From ec6ce259b66e4d6fa57f5152aae5a26fb84a0c65 Mon Sep 17 00:00:00 2001 From: Abhinandan Seshadri Date: Wed, 29 Mar 2023 21:40:06 -0400 Subject: [PATCH 1/7] Convert spring-core tests from groovy to java --- ...syncTaskExecutorInstrumentationTest.groovy | 93 --------------- ...eAsyncTaskExecutorInstrumentationTest.java | 111 ++++++++++++++++++ 2 files changed, 111 insertions(+), 93 deletions(-) delete mode 100644 instrumentation/spring/spring-core-2.0/javaagent/src/test/groovy/SimpleAsyncTaskExecutorInstrumentationTest.groovy create mode 100644 instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java diff --git a/instrumentation/spring/spring-core-2.0/javaagent/src/test/groovy/SimpleAsyncTaskExecutorInstrumentationTest.groovy b/instrumentation/spring/spring-core-2.0/javaagent/src/test/groovy/SimpleAsyncTaskExecutorInstrumentationTest.groovy deleted file mode 100644 index e90417f66851..000000000000 --- a/instrumentation/spring/spring-core-2.0/javaagent/src/test/groovy/SimpleAsyncTaskExecutorInstrumentationTest.groovy +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import io.opentelemetry.api.GlobalOpenTelemetry -import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification -import org.springframework.core.task.SimpleAsyncTaskExecutor -import spock.lang.Shared -import spock.lang.Unroll - -import java.util.concurrent.Callable -import java.util.concurrent.CountDownLatch - -import static io.opentelemetry.api.trace.SpanKind.INTERNAL - -class SimpleAsyncTaskExecutorInstrumentationTest extends AgentInstrumentationSpecification { - - @Shared - def executeRunnable = { e, c -> e.execute((Runnable) c) } - @Shared - def submitRunnable = { e, c -> e.submit((Runnable) c) } - @Shared - def submitCallable = { e, c -> e.submit((Callable) c) } - @Shared - def submitListenableRunnable = { e, c -> e.submitListenable((Runnable) c) } - @Shared - def submitListenableCallable = { e, c -> e.submitListenable((Callable) c) } - - @Unroll - def "should propagate context on #desc"() { - given: - def executor = new SimpleAsyncTaskExecutor() - - when: - runWithSpan("parent") { - def child1 = new AsyncTask(startSpan: true) - def child2 = new AsyncTask(startSpan: false) - method(executor, child1) - method(executor, child2) - child1.waitForCompletion() - child2.waitForCompletion() - } - - then: - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind INTERNAL - } - span(1) { - name "asyncChild" - kind INTERNAL - childOf(span(0)) - } - } - } - - where: - desc | method - "execute Runnable" | executeRunnable - "submit Runnable" | submitRunnable - "submit Callable" | submitCallable - "submitListenable Runnable" | submitListenableRunnable - "submitListenable Callable" | submitListenableCallable - } -} - -class AsyncTask implements Runnable, Callable { - private static final TRACER = GlobalOpenTelemetry.getTracer("test") - - final latch = new CountDownLatch(1) - boolean startSpan - - @Override - void run() { - if (startSpan) { - TRACER.spanBuilder("asyncChild").startSpan().end() - } - latch.countDown() - } - - @Override - Object call() throws Exception { - run() - return null - } - - void waitForCompletion() throws InterruptedException { - latch.await() - } -} diff --git a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java new file mode 100644 index 000000000000..1d307f9a09ab --- /dev/null +++ b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java @@ -0,0 +1,111 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.function.ThrowingConsumer; +import org.springframework.core.task.SimpleAsyncTaskExecutor; + +public class SimpleAsyncTaskExecutorInstrumentationTest { + + @RegisterExtension + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + + private static final SimpleAsyncTaskExecutor EXECUTOR = new SimpleAsyncTaskExecutor(); + + @Test + public void executeRunnable() { + executeTwoTasks(EXECUTOR::execute); + } + + @Test + public void submitRunnable() { + executeTwoTasks(task -> EXECUTOR.submit((Runnable) task)); + } + + @Test + public void submitCallable() { + executeTwoTasks(task -> EXECUTOR.submit((Callable) task)); + } + + @Test + public void submitListenableRunnable() { + executeTwoTasks(task -> EXECUTOR.submitListenable((Runnable) task)); + } + + @Test + public void submitListenableCallable() { + executeTwoTasks(task -> EXECUTOR.submitListenable((Callable) task)); + } + + public void executeTwoTasks(ThrowingConsumer task) { + testing.runWithSpan( + "parent", + () -> { + AsyncTask child1 = new AsyncTask(true); + AsyncTask child2 = new AsyncTask(false); + try { + task.accept(child1); + task.accept(child2); + } catch (Throwable throwable) { + throw new AssertionError(throwable); + } + child1.waitForCompletion(); + child2.waitForCompletion(); + }); + testing.waitAndAssertTraces( + trace -> + trace + .hasSize(2) + .hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("asyncChild") + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(0)))); + } + + static class AsyncTask implements Runnable, Callable { + + private static final Tracer TRACER = GlobalOpenTelemetry.getTracer("test"); + + private final boolean startSpan; + private final CountDownLatch latch = new CountDownLatch(1); + + @Override + public void run() { + if (startSpan) { + TRACER.spanBuilder("asyncChild").startSpan().end(); + } + latch.countDown(); + } + + @Override + public Object call() { + run(); + return null; + } + + void waitForCompletion() { + try { + latch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new AssertionError(e); + } + } + + public AsyncTask(boolean startSpan) { + this.startSpan = startSpan; + } + } +} From 334604cad82dd6ba864c4858a4186056c9d1823f Mon Sep 17 00:00:00 2001 From: Abhinandan Seshadri Date: Thu, 30 Mar 2023 23:17:25 -0400 Subject: [PATCH 2/7] Update instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java Co-authored-by: Mateusz Rzeszutek --- .../test/java/SimpleAsyncTaskExecutorInstrumentationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java index 1d307f9a09ab..968cf18fe7f1 100644 --- a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java +++ b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java @@ -47,7 +47,7 @@ public void submitListenableCallable() { executeTwoTasks(task -> EXECUTOR.submitListenable((Callable) task)); } - public void executeTwoTasks(ThrowingConsumer task) { + private void executeTwoTasks(ThrowingConsumer task) { testing.runWithSpan( "parent", () -> { From 2265f6400d41e67c05411b2135ff3ae62d62dba5 Mon Sep 17 00:00:00 2001 From: Abhinandan Seshadri Date: Thu, 30 Mar 2023 23:17:39 -0400 Subject: [PATCH 3/7] Update instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java Co-authored-by: Mateusz Rzeszutek --- .../test/java/SimpleAsyncTaskExecutorInstrumentationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java index 968cf18fe7f1..76d07054515b 100644 --- a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java +++ b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java @@ -23,7 +23,7 @@ public class SimpleAsyncTaskExecutorInstrumentationTest { private static final SimpleAsyncTaskExecutor EXECUTOR = new SimpleAsyncTaskExecutor(); @Test - public void executeRunnable() { + void executeRunnable() { executeTwoTasks(EXECUTOR::execute); } From e66ff689118d21f1b9db5fb789f438fbc69b1ec6 Mon Sep 17 00:00:00 2001 From: Abhinandan Seshadri Date: Thu, 30 Mar 2023 23:19:59 -0400 Subject: [PATCH 4/7] Set min java version --- .../spring/spring-core-2.0/javaagent/build.gradle.kts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/instrumentation/spring/spring-core-2.0/javaagent/build.gradle.kts b/instrumentation/spring/spring-core-2.0/javaagent/build.gradle.kts index e941d2620ad0..e012ab37e1f4 100644 --- a/instrumentation/spring/spring-core-2.0/javaagent/build.gradle.kts +++ b/instrumentation/spring/spring-core-2.0/javaagent/build.gradle.kts @@ -19,3 +19,10 @@ dependencies { // 4.0 introduces submitListenable() methods testLibrary("org.springframework:spring-core:4.0.0.RELEASE") } + +// spring 6 requires java 17 +if (findProperty("testLatestDeps") as Boolean) { + otelJava { + minJavaVersionSupported.set(JavaVersion.VERSION_17) + } +} From d6268f7f59da5cd8d6697443553656c17c0cd59f Mon Sep 17 00:00:00 2001 From: Abhinandan Seshadri Date: Thu, 30 Mar 2023 23:24:42 -0400 Subject: [PATCH 5/7] Fix style --- .../SimpleAsyncTaskExecutorInstrumentationTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java index 76d07054515b..97615572ff66 100644 --- a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java +++ b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java @@ -47,7 +47,7 @@ public void submitListenableCallable() { executeTwoTasks(task -> EXECUTOR.submitListenable((Callable) task)); } - private void executeTwoTasks(ThrowingConsumer task) { + private static void executeTwoTasks(ThrowingConsumer task) { testing.runWithSpan( "parent", () -> { @@ -81,6 +81,10 @@ static class AsyncTask implements Runnable, Callable { private final boolean startSpan; private final CountDownLatch latch = new CountDownLatch(1); + public AsyncTask(boolean startSpan) { + this.startSpan = startSpan; + } + @Override public void run() { if (startSpan) { @@ -103,9 +107,5 @@ void waitForCompletion() { throw new AssertionError(e); } } - - public AsyncTask(boolean startSpan) { - this.startSpan = startSpan; - } } } From ffa52289d79521a1dc6ccc7d7020be4266d00613 Mon Sep 17 00:00:00 2001 From: Abhinandan Seshadri Date: Thu, 30 Mar 2023 23:36:00 -0400 Subject: [PATCH 6/7] Remove private access modifier --- .../java/SimpleAsyncTaskExecutorInstrumentationTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java index 97615572ff66..5e2baddaffd2 100644 --- a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java +++ b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java @@ -28,22 +28,22 @@ void executeRunnable() { } @Test - public void submitRunnable() { + void submitRunnable() { executeTwoTasks(task -> EXECUTOR.submit((Runnable) task)); } @Test - public void submitCallable() { + void submitCallable() { executeTwoTasks(task -> EXECUTOR.submit((Callable) task)); } @Test - public void submitListenableRunnable() { + void submitListenableRunnable() { executeTwoTasks(task -> EXECUTOR.submitListenable((Runnable) task)); } @Test - public void submitListenableCallable() { + void submitListenableCallable() { executeTwoTasks(task -> EXECUTOR.submitListenable((Callable) task)); } From 13b7a798b6d1c2654f705ea48cc4be2a629a3d14 Mon Sep 17 00:00:00 2001 From: Abhinandan Seshadri Date: Thu, 30 Mar 2023 23:36:00 -0400 Subject: [PATCH 7/7] Remove public access modifier --- .../java/SimpleAsyncTaskExecutorInstrumentationTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java index 97615572ff66..5e2baddaffd2 100644 --- a/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java +++ b/instrumentation/spring/spring-core-2.0/javaagent/src/test/java/SimpleAsyncTaskExecutorInstrumentationTest.java @@ -28,22 +28,22 @@ void executeRunnable() { } @Test - public void submitRunnable() { + void submitRunnable() { executeTwoTasks(task -> EXECUTOR.submit((Runnable) task)); } @Test - public void submitCallable() { + void submitCallable() { executeTwoTasks(task -> EXECUTOR.submit((Callable) task)); } @Test - public void submitListenableRunnable() { + void submitListenableRunnable() { executeTwoTasks(task -> EXECUTOR.submitListenable((Runnable) task)); } @Test - public void submitListenableCallable() { + void submitListenableCallable() { executeTwoTasks(task -> EXECUTOR.submitListenable((Callable) task)); }