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

Add Android API-friendliness checks #4505

Merged
merged 6 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
26 changes: 0 additions & 26 deletions instrumentation-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ru.vyarus.gradle.plugin.animalsniffer.AnimalSniffer

plugins {
id("org.xbib.gradle.plugin.jflex")

Expand Down Expand Up @@ -67,28 +65,4 @@ tasks {
isFailOnNoMatchingTests = false
}
}

withType<AnimalSniffer>().configureEach {
// we catch NoClassDefFoundError when use caffeine3 is not available on classpath and fall back to caffeine2
exclude("**/internal/shaded/caffeine3/**")

ignoreClasses = listOf(
// ignore shaded caffeine3 references
"io.opentelemetry.instrumentation.api.internal.shaded.caffeine3.cache.Caffeine",
"io.opentelemetry.instrumentation.api.internal.shaded.caffeine3.cache.Cache",

// these are being referenced by some caffeine2 classes, but we don't use them
"java.util.concurrent.CompletableFuture",
"java.util.concurrent.CompletionException",
"java.util.concurrent.CompletionStage",
"java.util.concurrent.ForkJoinPool",
"java.util.concurrent.ConcurrentHashMap",
// LongAdder is also referenced by our own SupportabilityMetrics; only if agent debug is enabled though
"java.util.concurrent.atomic.LongAdder",

// these standard Java classes seem to be desugared properly
"java.lang.ClassValue",
"sun.misc.Unsafe",
)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,40 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;

final class JdkErrorCauseExtractor implements ErrorCauseExtractor {
static final ErrorCauseExtractor INSTANCE = new JdkErrorCauseExtractor();

@Nullable
private static final Class<?> COMPLETION_EXCEPTION_CLASS = getCompletionExceptionClass();

@Override
public Throwable extractCause(Throwable error) {
if (error.getCause() != null
&& (error instanceof ExecutionException
|| error instanceof CompletionException
|| isInstanceOfCompletionException(error)
|| error instanceof InvocationTargetException
|| error instanceof UndeclaredThrowableException)) {
return extractCause(error.getCause());
}
return error;
}

private static boolean isInstanceOfCompletionException(Throwable error) {
return COMPLETION_EXCEPTION_CLASS != null && COMPLETION_EXCEPTION_CLASS.isInstance(error);
}

@Nullable
private static Class<?> getCompletionExceptionClass() {
try {
return Class.forName("java.util.concurrent.CompletionException");
} catch (ClassNotFoundException e) {
// Android level 21 does not support java.util.concurrent.CompletionException
return null;
}
}

private JdkErrorCauseExtractor() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import io.opentelemetry.instrumentation.api.internal.SupportabilityMetrics;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

/**
* Base class for all instrumentation specific tracer implementations.
Expand All @@ -48,6 +48,9 @@
public abstract class BaseTracer {
private static final SupportabilityMetrics supportability = SupportabilityMetrics.instance();

@Nullable
private static final Class<?> COMPLETION_EXCEPTION_CLASS = getCompletionExceptionClass();

private final Tracer tracer;
private final ContextPropagators propagators;

Expand Down Expand Up @@ -242,7 +245,7 @@ public void onException(Context context, Throwable throwable) {
protected Throwable unwrapThrowable(Throwable throwable) {
if (throwable.getCause() != null
&& (throwable instanceof ExecutionException
|| throwable instanceof CompletionException
|| isInstanceOfCompletionException(throwable)
|| throwable instanceof InvocationTargetException
|| throwable instanceof UndeclaredThrowableException)) {
return unwrapThrowable(throwable.getCause());
Expand Down Expand Up @@ -283,4 +286,18 @@ public <C> Context extract(C carrier, TextMapGetter<C> getter) {
public <C> void inject(Context context, C carrier, TextMapSetter<C> setter) {
propagators.getTextMapPropagator().inject(context, carrier, setter);
}

private static boolean isInstanceOfCompletionException(Throwable error) {
return COMPLETION_EXCEPTION_CLASS != null && COMPLETION_EXCEPTION_CLASS.isInstance(error);
}

@Nullable
private static Class<?> getCompletionExceptionClass() {
try {
return Class.forName("java.util.concurrent.CompletionException");
} catch (ClassNotFoundException e) {
// Android level 21 does not support java.util.concurrent.CompletionException
return null;
}
}
}