-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve getQualifier from spring scheduling runnables
- Loading branch information
Showing
18 changed files
with
799 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
429 changes: 216 additions & 213 deletions
429
dd-java-agent/instrumentation/spring-scheduling-3.1/gradle.lockfile
Large diffs are not rendered by default.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
...-agent/instrumentation/spring-scheduling-3.1/src/latestDepTest/groovy/ShedlockTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import datadog.trace.agent.test.AgentTestRunner | ||
import datadog.trace.test.util.Flaky | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext | ||
import org.springframework.jdbc.core.JdbcTemplate | ||
|
||
import javax.sql.DataSource | ||
import java.util.concurrent.TimeUnit | ||
|
||
class ShedlockTest extends AgentTestRunner { | ||
|
||
@Flaky("task.invocationCount() == 0") | ||
def "should not disable shedlock"() { | ||
setup: | ||
def context = new AnnotationConfigApplicationContext(ShedlockConfig) | ||
JdbcTemplate jdbcTemplate = new JdbcTemplate(context.getBean(DataSource)) | ||
jdbcTemplate.execute("CREATE TABLE shedlock(name VARCHAR(64) NOT NULL PRIMARY KEY, lock_until TIMESTAMP NOT NULL,\n" + | ||
" locked_at TIMESTAMP NOT NULL, locked_by VARCHAR(255) NOT NULL);") | ||
def task = context.getBean(ShedLockedTask) | ||
|
||
expect: "lock is held for more than one second" | ||
!task.awaitInvocation(1000, TimeUnit.MILLISECONDS) | ||
task.invocationCount() == 1 | ||
|
||
cleanup: | ||
context.close() | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ent/instrumentation/spring-scheduling-3.1/src/latestDepTest/groovy/SpringAsyncTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import datadog.trace.agent.test.AgentTestRunner | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext | ||
|
||
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace | ||
|
||
class SpringAsyncTest extends AgentTestRunner { | ||
|
||
def "context propagated through @async annotation"() { | ||
setup: | ||
def context = new AnnotationConfigApplicationContext(AsyncTaskConfig) | ||
AsyncTask asyncTask = context.getBean(AsyncTask) | ||
when: | ||
runUnderTrace("root") { | ||
asyncTask.async().join() | ||
} | ||
then: | ||
assertTraces(1) { | ||
trace(3) { | ||
span { | ||
resourceName "root" | ||
} | ||
span { | ||
resourceName "AsyncTask.async" | ||
threadNameStartsWith "SimpleAsyncTaskExecutor" | ||
childOf span(0) | ||
} | ||
span { | ||
resourceName "AsyncTask.getInt" | ||
threadNameStartsWith "SimpleAsyncTaskExecutor" | ||
childOf span(1) | ||
} | ||
} | ||
} | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
...nstrumentation/spring-scheduling-3.1/src/latestDepTest/groovy/SpringSchedulingTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import static datadog.trace.agent.test.utils.TraceUtils.basicSpan | ||
|
||
import datadog.trace.agent.test.AgentTestRunner | ||
import datadog.trace.bootstrap.instrumentation.api.Tags | ||
import org.springframework.boot.actuate.scheduling.ScheduledTasksEndpoint | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
class SpringSchedulingTest extends AgentTestRunner { | ||
|
||
def legacyTracing() { | ||
false | ||
} | ||
|
||
@Override | ||
protected void configurePreAgent() { | ||
super.configurePreAgent() | ||
if (legacyTracing()) { | ||
injectSysConfig("spring-scheduling.legacy.tracing.enabled", "true") | ||
} | ||
} | ||
|
||
def "schedule trigger test according to cron expression"() { | ||
setup: | ||
def context = new AnnotationConfigApplicationContext(TriggerTaskConfig, SchedulingConfig) | ||
def task = context.getBean(TriggerTask) | ||
|
||
task.blockUntilExecute() | ||
|
||
expect: | ||
assert task != null | ||
def hasParent = legacyTracing() | ||
assertTraces(hasParent ? 1 : 2) { | ||
if (!hasParent) { | ||
trace(1) { | ||
basicSpan(it, "parent") | ||
} | ||
} | ||
trace(hasParent ? 2 : 1) { | ||
if (hasParent) { | ||
basicSpan(it, "parent") | ||
} | ||
span { | ||
resourceName "TriggerTask.run" | ||
operationName "scheduled.call" | ||
hasParent ? childOfPrevious() : parent() | ||
errored false | ||
tags { | ||
"$Tags.COMPONENT" "spring-scheduling" | ||
defaultTags() | ||
} | ||
} | ||
} | ||
} | ||
and: | ||
def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint) | ||
assert scheduledTaskEndpoint != null | ||
scheduledTaskEndpoint.scheduledTasks().getCron().each { | ||
it.getRunnable().getTarget() == TriggerTask.getName() | ||
} | ||
cleanup: | ||
context.close() | ||
} | ||
|
||
def "schedule interval test"() { | ||
setup: | ||
def context = new AnnotationConfigApplicationContext(IntervalTaskConfig, SchedulingConfig) | ||
def task = context.getBean(IntervalTask) | ||
|
||
task.blockUntilExecute() | ||
|
||
expect: | ||
assert task != null | ||
def hasParent = legacyTracing() | ||
|
||
assertTraces(hasParent ? 1 : 2) { | ||
if (!hasParent) { | ||
trace(1) { | ||
basicSpan(it, "parent") | ||
} | ||
} | ||
trace(hasParent ? 2 : 1) { | ||
if (hasParent) { | ||
basicSpan(it, "parent") | ||
} | ||
span { | ||
resourceName "IntervalTask.run" | ||
operationName "scheduled.call" | ||
hasParent ? childOfPrevious() : parent() | ||
errored false | ||
tags { | ||
"$Tags.COMPONENT" "spring-scheduling" | ||
defaultTags() | ||
} | ||
} | ||
} | ||
} | ||
cleanup: | ||
context.close() | ||
} | ||
|
||
def "schedule lambda test"() { | ||
setup: | ||
def context = new AnnotationConfigApplicationContext(LambdaTaskConfig, SchedulingConfig) | ||
def configurer = context.getBean(LambdaTaskConfigurer) | ||
|
||
configurer.singleUseLatch.await(2000, TimeUnit.MILLISECONDS) | ||
|
||
expect: | ||
def hasParent = legacyTracing() | ||
assertTraces(hasParent ? 1 : 2) { | ||
if (!hasParent) { | ||
trace(1) { | ||
basicSpan(it, "parent") | ||
} | ||
} | ||
trace(hasParent ? 2 : 1) { | ||
if (hasParent) { | ||
basicSpan(it, "parent") | ||
} | ||
span { | ||
resourceNameContains("LambdaTaskConfigurer\$\$Lambda") | ||
operationName "scheduled.call" | ||
hasParent ? childOfPrevious() : parent() | ||
errored false | ||
tags { | ||
"$Tags.COMPONENT" "spring-scheduling" | ||
defaultTags() | ||
} | ||
} | ||
} | ||
} | ||
|
||
cleanup: | ||
context.close() | ||
} | ||
} | ||
|
||
class SpringSchedulingLegacyTracingForkedTest extends SpringSchedulingTest { | ||
@Override | ||
def legacyTracing() { | ||
true | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
dd-java-agent/instrumentation/spring-scheduling-3.1/src/latestDepTest/java/AsyncTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import datadog.trace.api.Trace; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import org.springframework.scheduling.annotation.Async; | ||
|
||
public class AsyncTask { | ||
|
||
@Async | ||
public CompletableFuture<Integer> async() { | ||
return CompletableFuture.completedFuture(getInt()); | ||
} | ||
|
||
@Trace | ||
public int getInt() { | ||
return ThreadLocalRandom.current().nextInt(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...a-agent/instrumentation/spring-scheduling-3.1/src/latestDepTest/java/AsyncTaskConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
@EnableAsync | ||
public class AsyncTaskConfig { | ||
|
||
@Bean | ||
AsyncTask asyncTask() { | ||
return new AsyncTask(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
dd-java-agent/instrumentation/spring-scheduling-3.1/src/latestDepTest/java/IntervalTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class IntervalTask implements Runnable { | ||
|
||
private final CountDownLatch latch = new CountDownLatch(1); | ||
|
||
@Scheduled(fixedRate = 5000, scheduler = "tracingTaskScheduler") | ||
@Override | ||
public void run() { | ||
latch.countDown(); | ||
} | ||
|
||
public void blockUntilExecute() throws InterruptedException { | ||
latch.await(5, TimeUnit.SECONDS); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...gent/instrumentation/spring-scheduling-3.1/src/latestDepTest/java/IntervalTaskConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
public class IntervalTaskConfig { | ||
@Bean | ||
public IntervalTask scheduledTasks() { | ||
return new IntervalTask(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...-agent/instrumentation/spring-scheduling-3.1/src/latestDepTest/java/LambdaTaskConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
public class LambdaTaskConfig { | ||
|
||
@Bean | ||
LambdaTaskConfigurer lambdaTaskConfigurer() { | ||
return new LambdaTaskConfigurer(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...nt/instrumentation/spring-scheduling-3.1/src/latestDepTest/java/LambdaTaskConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.concurrent.CountDownLatch; | ||
import org.springframework.scheduling.annotation.SchedulingConfigurer; | ||
import org.springframework.scheduling.config.ScheduledTaskRegistrar; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class LambdaTaskConfigurer implements SchedulingConfigurer { | ||
|
||
public final CountDownLatch singleUseLatch = new CountDownLatch(1); | ||
|
||
@Override | ||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | ||
// need to manually set in this case since it won't use the scheduler in the annotation | ||
taskRegistrar.setTaskScheduler(new SchedulingConfig.TracingTaskScheduler()); | ||
taskRegistrar.addFixedDelayTask(singleUseLatch::countDown, 500); | ||
} | ||
} |
Oops, something went wrong.