diff --git a/instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/SpringSchedulingCodeAttributesGetter.java b/instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/SpringSchedulingCodeAttributesGetter.java index d241be9457..0da9cdf045 100644 --- a/instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/SpringSchedulingCodeAttributesGetter.java +++ b/instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/SpringSchedulingCodeAttributesGetter.java @@ -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(); } else { return runnable.getClass(); } diff --git a/instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/groovy/SpringSchedulingTest.groovy b/instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/groovy/SpringSchedulingTest.groovy index 23dc08d87a..32f5a6077d 100644 --- a/instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/groovy/SpringSchedulingTest.groovy +++ b/instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/groovy/SpringSchedulingTest.groovy @@ -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 { @@ -54,7 +55,6 @@ class SpringSchedulingTest extends AgentInstrumentationSpecification { } } } - } def "schedule lambda test"() { @@ -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" + } + } + } + } + } } diff --git a/instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/java/EnhancedClassTaskConfig.java b/instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/java/EnhancedClassTaskConfig.java new file mode 100644 index 0000000000..e575167128 --- /dev/null +++ b/instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/java/EnhancedClassTaskConfig.java @@ -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; + } +}