-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First "MVP" support for Spring @Scheduled / EJB @Schedule annotation #569
Merged
eyalkoren
merged 4 commits into
elastic:master
from
timmhirsens:spring-scheduled-annotation
Apr 11, 2019
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>apm-agent-plugins</artifactId> | ||
<groupId>co.elastic.apm</groupId> | ||
<version>1.5.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>apm-spring-scheduled-plugin</artifactId> | ||
<name>${project.groupId}:${project.artifactId}</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<version>2.0.2.RELEASE</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.awaitility</groupId> | ||
<artifactId>awaitility</artifactId> | ||
<version>3.1.6</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
104 changes: 104 additions & 0 deletions
104
.../co/elastic/apm/agent/spring/scheduled/SpringScheduledTransactionNameInstrumentation.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,104 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.scheduled; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import co.elastic.apm.agent.bci.ElasticApmInstrumentation; | ||
import co.elastic.apm.agent.bci.VisibleForAdvice; | ||
import co.elastic.apm.agent.bci.bytebuddy.SimpleMethodSignatureOffsetMappingFactory.SimpleMethodSignature; | ||
import co.elastic.apm.agent.impl.ElasticApmTracer; | ||
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration; | ||
import co.elastic.apm.agent.impl.transaction.TraceContext; | ||
import co.elastic.apm.agent.impl.transaction.TraceContextHolder; | ||
import co.elastic.apm.agent.impl.transaction.Transaction; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.NamedElement; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import net.bytebuddy.matcher.ElementMatchers; | ||
|
||
import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isInAnyPackage; | ||
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
public class SpringScheduledTransactionNameInstrumentation extends ElasticApmInstrumentation { | ||
|
||
@VisibleForAdvice | ||
public static final Logger logger = LoggerFactory.getLogger(SpringScheduledTransactionNameInstrumentation.class); | ||
|
||
private Collection<String> applicationPackages = Collections.emptyList(); | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
private static void setTransactionName(@SimpleMethodSignature String signature, @Advice.Origin Class<?> clazz, @Advice.Local("transaction") Transaction transaction) { | ||
if (tracer != null) { | ||
TraceContextHolder<?> active = tracer.getActive(); | ||
if (active == null) { | ||
transaction = tracer.startTransaction(TraceContext.asRoot(), null, clazz.getClassLoader()) | ||
.withName(signature) | ||
.withType("scheduled") | ||
.activate(); | ||
|
||
} else { | ||
logger.debug("Not creating transaction for method {} because there is already a transaction running ({})", signature, active); | ||
} | ||
} | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onMethodExit(@Nullable @Advice.Local("transaction") Transaction transaction, | ||
@Advice.Thrown Throwable t) { | ||
if (transaction != null) { | ||
transaction.captureException(t) | ||
.deactivate() | ||
.end(); | ||
} | ||
} | ||
|
||
@Override | ||
public void init(ElasticApmTracer tracer) { | ||
applicationPackages = tracer.getConfig(StacktraceConfiguration.class).getApplicationPackages(); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> getTypeMatcher() { | ||
return isInAnyPackage(applicationPackages, ElementMatchers.<NamedElement>none()) | ||
.and(declaresMethod(getMethodMatcher())); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<? super MethodDescription> getMethodMatcher() { | ||
return isAnnotatedWith(named("org.springframework.scheduling.annotation.Scheduled")); | ||
timmhirsens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public Collection<String> getInstrumentationGroupNames() { | ||
return Arrays.asList("concurrent", "spring-scheduled"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ng-scheduled-plugin/src/main/java/co/elastic/apm/agent/spring/scheduled/package-info.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,23 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
@NonnullApi | ||
package co.elastic.apm.agent.spring.scheduled; | ||
|
||
import co.elastic.apm.agent.annotation.NonnullApi; |
1 change: 1 addition & 0 deletions
1
...n/src/main/resources/META-INF/services/co.elastic.apm.agent.bci.ElasticApmInstrumentation
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 @@ | ||
co.elastic.apm.agent.spring.scheduled.SpringScheduledTransactionNameInstrumentation |
39 changes: 39 additions & 0 deletions
39
...-spring-scheduled-plugin/src/test/java/co/elastic/apm/agent/spring/scheduled/Counter.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,39 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.scheduled; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class Counter { | ||
private AtomicInteger count = new AtomicInteger(0); | ||
|
||
@Scheduled(fixedDelay = 5) | ||
public void scheduled() { | ||
this.count.incrementAndGet(); | ||
} | ||
|
||
public int getInvocationCount() { | ||
return this.count.get(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...scheduled-plugin/src/test/java/co/elastic/apm/agent/spring/scheduled/ScheduledConfig.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,30 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.scheduled; | ||
|
||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
@ComponentScan("co.elastic.apm.agent.spring.scheduled") | ||
public class ScheduledConfig { | ||
} |
80 changes: 80 additions & 0 deletions
80
...elastic/apm/agent/spring/scheduled/SpringScheduledTransactionNameInstrumentationTest.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,80 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.scheduled; | ||
|
||
import java.util.Collections; | ||
|
||
import org.awaitility.Duration; | ||
import org.junit.BeforeClass; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import co.elastic.apm.agent.MockReporter; | ||
import co.elastic.apm.agent.bci.ElasticApmAgent; | ||
import co.elastic.apm.agent.configuration.SpyConfiguration; | ||
import co.elastic.apm.agent.impl.ElasticApmTracer; | ||
import co.elastic.apm.agent.impl.ElasticApmTracerBuilder; | ||
import net.bytebuddy.agent.ByteBuddyAgent; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.mockito.Mockito.atLeast; | ||
import static org.mockito.Mockito.verify; | ||
|
||
/** | ||
* TODO <<Zweck und Verantwortung des Moduls, ggf. mehrere Zeilen>> | ||
timmhirsens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
@SpringJUnitConfig(ScheduledConfig.class) | ||
timmhirsens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class SpringScheduledTransactionNameInstrumentationTest { | ||
|
||
private static MockReporter reporter; | ||
private static ElasticApmTracer tracer; | ||
|
||
@BeforeClass | ||
@BeforeAll | ||
static void setUpAll() { | ||
reporter = new MockReporter(); | ||
tracer = new ElasticApmTracerBuilder() | ||
.configurationRegistry(SpyConfiguration.createSpyConfig()) | ||
.reporter(reporter) | ||
.build(); | ||
ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install(), | ||
Collections.singletonList(new SpringScheduledTransactionNameInstrumentation())); | ||
} | ||
|
||
@SpyBean | ||
private Counter counter; | ||
|
||
@Test | ||
void testScheduledAnnotatedMethodsAreTraced() { | ||
reporter.reset(); | ||
await() | ||
timmhirsens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.atMost(Duration.FIVE_HUNDRED_MILLISECONDS) | ||
.untilAsserted(() -> verify(counter, atLeast(5)).scheduled()); | ||
timmhirsens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assertThat(reporter.getTransactions().size(), greaterThanOrEqualTo(counter.getInvocationCount())); | ||
assertThat(reporter.getTransactions().get(0).getName().toString(), containsString("#scheduled")); | ||
} | ||
|
||
} |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a good choice for "type"? I did not find anything besided "request" in the current code base.