Fix regression in spring-scheduling span name
MODIFY
instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/SpringSchedulingCodeAttributesGetter.java
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();
}

MODIFY
instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/groovy/SpringSchedulingTest.groovy
instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/groovy/SpringSchedulingTest.groovy
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"
+ }
+ }
+ }
+ }
+ }
}

ADD
instrumentation/spring/spring-scheduling-3.1/javaagent/src/test/java/EnhancedClassTaskConfig.java
+/*
+ * 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;
+ }
+}

Fix regression in spring-scheduling span name (#5436)