Skip to content

Commit

Permalink
Add Android API-friendliness checks (#4505)
Browse files Browse the repository at this point in the history
* Add Android API-friendliness checks

* Improve comments

* Remove ignores

* Handle CompletionException

* Spotless

Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
Mateusz Rzeszutek and trask committed Nov 24, 2021
1 parent 9039269 commit 9a4a68d
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions conventions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
implementation("org.ow2.asm:asm-tree:9.1")
implementation("org.apache.httpcomponents:httpclient:4.5.13")
implementation("org.gradle:test-retry-gradle-plugin:1.2.1")
implementation("ru.vyarus:gradle-animalsniffer-plugin:1.5.3")
// When updating, also update dependencyManagement/build.gradle.kts
implementation("net.bytebuddy:byte-buddy-gradle-plugin:1.11.22")
implementation("gradle.plugin.io.morethan.jmhreport:gradle-jmh-report:0.9.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
`java-library`

id("ru.vyarus.animalsniffer")
}

dependencies {
add("signature", "com.toasttab.android:gummy-bears-api-21:0.3.0:coreLib@signature")
}

animalsniffer {
sourceSets = listOf(java.sourceSets.main.get())
}
1 change: 1 addition & 0 deletions instrumentation-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("org.xbib.gradle.plugin.jflex")

id("otel.java-conventions")
id("otel.animalsniffer-conventions")
id("otel.jacoco-conventions")
id("otel.japicmp-conventions")
id("otel.publish-conventions")
Expand Down
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("otel.library-instrumentation")
id("otel.nullaway-conventions")
id("otel.animalsniffer-conventions")
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions instrumentation/grpc-1.6/library/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("otel.library-instrumentation")
id("otel.animalsniffer-conventions")
}

val grpcVersion = "1.6.0"
Expand Down
1 change: 1 addition & 0 deletions instrumentation/okhttp/okhttp-3.0/library/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("otel.library-instrumentation")
id("otel.nullaway-conventions")
id("otel.animalsniffer-conventions")
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pluginManagement {
id("org.xbib.gradle.plugin.jflex") version "1.5.0"
id("nebula.release") version "15.3.1"
id("com.github.johnrengelman.shadow") version "7.0.0"
id("ru.vyarus.animalsniffer") version "1.5.3"
}
}

Expand Down

0 comments on commit 9a4a68d

Please sign in to comment.