Skip to content
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

Add auto configuration for spring scheduling instrumentation using aop #12438

Merged
merged 1 commit into from
Oct 16, 2024

Conversation

kyy1996
Copy link
Contributor

@kyy1996 kyy1996 commented Oct 13, 2024

Since library instrumentation for spring scheduling is missing. I would like to provide a simple library instrumentation using AOP for spring scheduling.

Using this approach, we could intercept calls of any @Scheduled annotated method and start spans for them. Though this can cover most of the use cases, there's a limitation that tasks registered through ScheduledTaskRegistrar#addCronTask programmatically will not be recorded since they don't have @Scheduled annotation.

Also, I've added an auto configuration for it, so that this can be an out-of-box instrumentation.

Properties for this instrumentation such as otel.instrumentation.spring-scheduling.experimental-span-attributes and otel.instrumentation.spring-scheduling.enabled are aligned with spring scheduling javaagent instrumentation.

@kyy1996 kyy1996 requested a review from a team as a code owner October 13, 2024 10:32
@github-actions github-actions bot added the test native This label can be applied to PRs to trigger them to run native tests label Oct 13, 2024
unproxiedTesterSimpleClassName = unproxiedTester.getClass().getSimpleName();
unproxiedTesterClassName = unproxiedTester.getClass().getName();

AspectJProxyFactory factory = new AspectJProxyFactory();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you trying to enable the AspectJ support with Spring? In this case, you could use @EnableAspectJAutoProxy (https://docs.spring.io/spring-framework/reference/core/aop/ataspectj/aspectj-support.html)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, these codes are purposed for unit tests to test the functions of SpringSchedulingInstrumentationAspect. If we use @EnableAspectJAutoProxy annotation, we must start the AnnotationConfigApplicationContext which is too heavy for tests since we only need to test the function of Aop I think. Using AspectJProxyFactory to create proxy instance and let it parse and apply the SpringSchedulingInstrumentationAspect will also meet our demands for tests.

@ConditionalOnEnabledInstrumentation(module = "spring-scheduling")
@ConditionalOnClass({Scheduled.class, Aspect.class})
@Configuration
public class SpringSchedulingInstrumentationAutoConfiguration {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class SpringSchedulingInstrumentationAutoConfiguration {
class SpringSchedulingInstrumentationAutoConfiguration {

@NonNull Context parentContext,
@NonNull REQUEST classAndMethod) {
attributes
.put(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the same thing done with the Java agent instrumentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final result is quite the same as java agent, but it's implemented differently. Java agent uses AddThreadDetailsSpanProcessor to add thread.id to the attributes of a span, while in spring boot auto configuration this information seems missing in span. Hence, to match the final span as java agent I write this.
For further extension, this class may be extracted as a common span processor and configured automatically just like the one in java agent?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your explanations. The current implementation is good to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For further extension, this class may be extracted as a common span processor and configured automatically just like the one in java agent?

I think this is a good idea 👍

I'd recommend removing it from this PR (since in the current state it's currently inconsistent with the other spring starter instrumentation), then we can merge, and then you can send a follow-up PR for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds cool to me😊. I've made the change.

Copy link
Member

@jeanbisutti jeanbisutti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me.

Thanks @kyy1996!

@trask trask merged commit 4497fbf into open-telemetry:main Oct 16, 2024
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test native This label can be applied to PRs to trigger them to run native tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants