Skip to content

Commit

Permalink
Fix latest dep tests (#12739)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Nov 16, 2024
1 parent 805ce0a commit f88e03e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -22,6 +59,7 @@ public Class<?> getCodeClass(Runnable runnable) {

@Override
public String getMethodName(Runnable runnable) {
runnable = unwrap(runnable);
if (runnable instanceof ScheduledMethodRunnable) {
ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
return scheduledMethodRunnable.getMethod().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down

0 comments on commit f88e03e

Please sign in to comment.