Fix latest dep tests (#12739)

This commit is contained in:
Lauri Tulmin 2024-11-16 21:48:56 +02:00 committed by GitHub
parent 805ce0a3cc
commit f88e03ed93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 0 deletions

View File

@ -6,12 +6,49 @@
package io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1;
import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter;
import java.lang.reflect.Field;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
public class SpringSchedulingCodeAttributesGetter implements CodeAttributesGetter<Runnable> {
private static final Class<?> outcomeTrackingRunnableClass = getOutcomeTrackingRunnableClass();
private static final Field outcomeTrackingRunnableField =
getOutcomeTrackingRunnableField(outcomeTrackingRunnableClass);
private static Class<?> getOutcomeTrackingRunnableClass() {
try {
return Class.forName("org.springframework.scheduling.config.Task$OutcomeTrackingRunnable");
} catch (ClassNotFoundException exception) {
return null;
}
}
private static Field getOutcomeTrackingRunnableField(Class<?> clazz) {
try {
Field field = clazz.getDeclaredField("runnable");
field.setAccessible(true);
return field;
} catch (Exception exception) {
return null;
}
}
private static Runnable unwrap(Runnable runnable) {
if (outcomeTrackingRunnableClass != null
&& outcomeTrackingRunnableField != null
&& outcomeTrackingRunnableClass.isAssignableFrom(runnable.getClass())) {
try {
// task may be wrapped multiple times so
return unwrap((Runnable) outcomeTrackingRunnableField.get(runnable));
} catch (IllegalAccessException ignore) {
// should not happen because setAccessible was called
}
}
return runnable;
}
@Override
public Class<?> getCodeClass(Runnable runnable) {
runnable = unwrap(runnable);
if (runnable instanceof ScheduledMethodRunnable) {
ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
return scheduledMethodRunnable.getMethod().getDeclaringClass();
@ -22,6 +59,7 @@ public class SpringSchedulingCodeAttributesGetter implements CodeAttributesGette
@Override
public String getMethodName(Runnable runnable) {
runnable = unwrap(runnable);
if (runnable instanceof ScheduledMethodRunnable) {
ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
return scheduledMethodRunnable.getMethod().getName();

View File

@ -22,6 +22,7 @@ dependencies {
library("io.projectreactor:reactor-core:3.5.0")
testLibrary("org.springframework:spring-test:6.0.0")
testLibrary("org.springframework:spring-context:6.0.0")
testLibrary("jakarta.servlet:jakarta.servlet-api:6.0.0")
}