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 latest dep tests #12739

Merged
merged 2 commits into from
Nov 16, 2024
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 @@ -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
Loading