Skip to content

StringEncoderHolder tries to create "unsafe encoder" on Android #8636

Description

@Teseus84

Hello,

I am using opentelemetry on Android.
When OtlpHttpSpanExporter exports for the first time then there is StringEncoder created in StringEncoderHolder.
It tries to create "unsafe encoder", but only if java version is lower then 24.

  private static StringEncoder createInstance() {
    // UnsafeStringEncoder has slightly better performance than VarHandleStringEncoder
    // so try it first
    if (!proactivelyAvoidUnsafe()) {
      StringEncoder unsafeImpl = createUnsafeEncoder();
      if (unsafeImpl != null) {
        logger.log(Level.FINE, "Using UnsafeStringEncoder for optimized Java 8+ performance");
        return unsafeImpl;
      }
    }
	
  private static boolean proactivelyAvoidUnsafe() {
    Optional<Double> javaVersion = getJavaVersion();
    // Avoid Unsafe on Java 23+ due to JEP-498 deprecation warnings:
    // "WARNING: A terminally deprecated method in sun.misc.Unsafe has been called"
    return javaVersion.map(version -> version >= 23).orElse(true);
  }	

On Android, java.specification.version is set to 0.9
It is lower then 24 so it tries to create unsafe encoder by accessing value in String.
It is not possible on Android, exception is thrown.
Exporter works as expected because exception is catch and fallback is used.

The only problem is that Android Runtime adds unnecessary error log if some code is trying to get "value" from "String"
The String#value field is not present on Android versions >= 6.0

Could You improve Java version check, so it won't try to use "unsafe encoder" on Android?

What version and what artifacts are you using?
io.opentelemetry:opentelemetry-exporter-otlp-common:1.62.0

Tip: React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions