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

Fix regression in spring-scheduling span name #5436

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SpringSchedulingCodeAttributesGetter implements CodeAttributesGette
public Class<?> codeClass(Runnable runnable) {
if (runnable instanceof ScheduledMethodRunnable) {
ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
return scheduledMethodRunnable.getTarget().getClass();
return scheduledMethodRunnable.getMethod().getDeclaringClass();
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect that it might be better to attempt to unwrap the target object.

// unwrap spring aop proxy
// based on org.springframework.test.util.AopTestUtils#getTargetObject
@SuppressWarnings("unchecked")
public static <T> T unwrapProxy(T candidate) {
try {
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) {
Object target = ((Advised) candidate).getTargetSource().getTarget();
if (target != null) {
return (T) target;
}
}
return candidate;
} catch (Throwable ignore) {
return candidate;
}
}

Unfortunately this is a common issue with getting class from object, there is no easy way to tell whether we got a proxy and how to unwrap it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I gave this a try, but the unwrapped class in the test I added was still EnhancedClassTaskConfig$$EnhancerByCGLIB$$b6a5b321@4ac197a7

} else {
return runnable.getClass();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import org.springframework.context.annotation.AnnotationConfigApplicationContext

import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

class SpringSchedulingTest extends AgentInstrumentationSpecification {
Expand Down Expand Up @@ -54,7 +55,6 @@ class SpringSchedulingTest extends AgentInstrumentationSpecification {
}
}
}

}

def "schedule lambda test"() {
Expand All @@ -81,4 +81,28 @@ class SpringSchedulingTest extends AgentInstrumentationSpecification {
cleanup:
context.close()
}

// by putting the scheduled method directly on the TaskConfig, this verifies the case where the
// class is enhanced and so has a different class name, e.g. TaskConfig$$EnhancerByCGLIB$$b910c4a9
def "schedule enhanced class test"() {
setup:
def context = new AnnotationConfigApplicationContext(EnhancedClassTaskConfig)
def latch = context.getBean(CountDownLatch)

latch.await(5, TimeUnit.SECONDS)

expect:
assertTraces(1) {
trace(0, 1) {
span(0) {
name "EnhancedClassTaskConfig.run"
hasNoParent()
attributes {
"code.namespace" "EnhancedClassTaskConfig"
"code.function" "run"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import java.util.concurrent.CountDownLatch;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
public class EnhancedClassTaskConfig {

private final CountDownLatch latch = new CountDownLatch(1);

@Scheduled(fixedRate = 5000)
public void run() {
latch.countDown();
}

@Bean
public CountDownLatch countDownLatch() {
return latch;
}
}