opentelemetry-java icon indicating copy to clipboard operation
opentelemetry-java copied to clipboard

RandomSupplier failure for Android APIs 25 or earlier

Open roliveiravictor opened this issue 4 years ago • 7 comments

Describe the bug

Many apps require configurations at startup. OTel configuration has introduced an unpleasant behavior, which adhered several unnoted (unfortunately also due to other faulties) crashes into our App. The given error was only reproducible for Android APIs 25 or earlier. Specifically, the API 23 is the one that reported most events.

Captura de Tela 2022-02-01 às 09 54 12

During our hotfix, it was identified at OTel's source code that the failure was known to some (not specified) Android APIs. Quoting:

  • {@link RandomSupplier} instance that doesn't use {@link java.util.concurrent.ThreadLocalRandom},
  • which is broken on most versions of Android (it uses the same seed everytime it starts up).

Steps to reproduce

Using Android APIs 25 or earlier, preferably 23, set a SdkTracerProviderBuilder up, which by default through TracerSharedState will spawn an IdGenerator. Then:

java.lang.NoClassDefFoundError:
at io.opentelemetry.sdk.internal.RandomSupplier.platformDefault (RandomSupplier.java:30)
at io.opentelemetry.sdk.trace.RandomIdGenerator.<clinit> (RandomIdGenerator.java:18)
at io.opentelemetry.sdk.trace.IdGenerator.random (IdGenerator.java:25)
at io.opentelemetry.sdk.trace.SdkTracerProviderBuilder.<init> (SdkTracerProviderBuilder.java:24)
at io.opentelemetry.sdk.trace.SdkTracerProvider.builder (SdkTracerProvider.java:44)
at com.android.opentelemetry.OpenTelemetryConfigurable.buildProvider (OpenTelemetryConfigurable.kt:38)
at com.android.opentelemetry.OpenTelemetryConfigurable.configure (OpenTelemetryConfigurable.kt:22)
at com.core.MainApplicationConfigurationKt.initBefore (MainApplicationConfiguration.kt:118)
at com.core.MainApplicationConfigurationKt.mainApplication (MainApplicationConfiguration.kt:101)
at com.core.MainApplicationConfigurationKt.mainApplication$default (MainApplicationConfiguration.kt:98)
at com.core.MainApplication.onCreate (MainApplication.kt:12)
at android.app.Instrumentation.callApplicationOnCreate (Instrumentation.java:1037)
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6428)
at android.app.ActivityThread.access$1800 (ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1887)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:7331)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

What did you expect to see?

The application simpling starting up.

What did you see instead?

A crash even before the app completely opened.

What version and what artifacts are you using? Artifacts: opentelemetry-api, opentelemetry-sdk, opentelemetry-exporter-otlp, opentelemetry-bom Version: 1.7.0 How did you reference these artifacts? build.gradle

Artifacts: io.grpc:grpc-okhttp Version: 1.41.0 How did you reference these artifacts? build.gradle

Environment Compiler: JDK 1.8.0_301 OS: macOS Big Sur Runtime (if different from JDK above): N/A OS (if different from OS compiled on): N/A

Issue Goal

It would be great if this random supplier was fully fixed for Android. Otherwise, if this information was less hidden, it also could help.

During our DX, we accessed SdkTracerProvider file many times, but we never got into RandomSupplier. Maybe the given warning could be exposed earlier. We mean, such as in the ImplicitContextKeyed interface, we appreciated the heads up for Kotlin coroutine users 🙂

roliveiravictor avatar Feb 01 '22 15:02 roliveiravictor

@mateuszrzeszutek @breedx-splk have we seen anything like this in our Android instrumentation?

jkwatson avatar Feb 02 '22 00:02 jkwatson

@roliveiravictor have you enabled core-library desugaring so that the java 8 APIs will work?

jkwatson avatar Feb 02 '22 02:02 jkwatson

@mateuszrzeszutek @breedx-splk have we seen anything like this in our Android instrumentation?

Nope, it's the first time I'm seeing this particular error.

mateuszrzeszutek avatar Feb 02 '22 10:02 mateuszrzeszutek

@roliveiravictor can you post a reproduction case somewhere? At Splunk, we haven't seen this issue in our Android instrumentation, but if there's an issue here we want to fix it.

jkwatson avatar Feb 02 '22 14:02 jkwatson

Hi @jkwatson,

In fact, enabling core-library desugaring on our test app solved the problem. We don't use it by default on our main app, but that would be an internal conflict that we would have to solve.

Following, a minimal repo on the issue, close as possible to our versions (gradle, AGP, kotlin, etc). I reproduced the event on API 23.

Far as I understood (and correct if I'm wrong), if AndroidFriendlyRandomHolder is used to avoid usage of ThreadLocalRandom cause disabled desugaring excludes it, so it's also excluding Supplier because it's listed on desugaring table.

Screen Shot 2022-02-02 at 12 32 51

The absence of Supplier at AndroidFriendlyRandomHolder is triggering the issue on APIs 25 or earlier, no? Such as presented on the previous repo.

Also, considering the same assumptions for desugaring, enabling it should increase our app size, right? which would be a huge problem for us. I thought about maybe keep stuff with Proguard, but no luck with a quick attempt. Can you think of any other possible way for fixing this?

Thank you (Splunk Team) very much for the quick & kind support

roliveiravictor avatar Feb 02 '22 17:02 roliveiravictor

Also, considering the same assumptions for desugaring, enabling it should increase our app size, right? which would be a huge problem for us. I thought about maybe keep stuff with Proguard, but no luck with a quick attempt. Can you think of any other possible way for fixing this?

For good or ill (mostly for good from my view as a maintainer here, and an Android instrumentation author), core library desugaring is an absolute requirement to use OpenTelemetry APIs and SDKs on Android. We extensively use java 8 APIs in all parts of the code, and there's no going back from that now.

jkwatson avatar Feb 02 '22 18:02 jkwatson

desugaring is an absolute requirement to use OpenTelemetry

Maybe a proper disclaimer at README.md could help others avoid this issue then? I mean, it's just a suggestion considering no further development action will be taken as an outcome of this talk

roliveiravictor avatar Feb 02 '22 21:02 roliveiravictor