Avoid unsafe string encoder on Android#8637
Conversation
| // Android exposes sun.misc.Unsafe, but String does not have the OpenJDK internal fields used by | ||
| // UnsafeStringEncoder. ART logs an error when these missing fields are queried, even though the | ||
| // exception is caught. | ||
| if ("Dalvik".equals(System.getProperty("java.vm.name"))) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
I was torn between adding the check here or in UnsafeStringEncoder.createIfAvailable(). Happy to move if you have a preference.
Pull request dashboard statusStatus: Waiting on reviewers to review the latest changes. |
There was a problem hiding this comment.
Pull request overview
This PR prevents StringEncoderHolder from attempting to initialize the UnsafeStringEncoder on Android, avoiding ART error logs caused by probing OpenJDK-specific String internals while still allowing the normal fallback encoder selection to proceed.
Changes:
- Short-circuit
createUnsafeEncoder()on Android by detecting"Dalvik"viajava.vm.nameand returningnullbefore touching OpenJDKStringinternals. - Add a regression test that simulates Android by setting
java.vm.name=Dalvikand asserting the unsafe encoder is not available.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/StringEncoderHolder.java | Avoids attempting unsafe string encoding on Android by checking java.vm.name before probing String internals. |
| exporters/common/src/test/java/io/opentelemetry/exporter/internal/marshal/StringEncoderTest.java | Adds a test ensuring the unsafe encoder is skipped when running under a Dalvik-like VM name. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8637 +/- ##
=========================================
Coverage 91.60% 91.61%
- Complexity 10343 10344 +1
=========================================
Files 1013 1013
Lines 27352 27354 +2
Branches 3215 3215
=========================================
+ Hits 25057 25059 +2
Misses 1567 1567
Partials 728 728 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Avoid probing OpenJDK-specific String fields when selecting the unsafe string encoder on Android. Android exposes sun.misc.Unsafe, but querying its different String internals causes ART to emit an error before OpenTelemetry can fall back to the standard encoder.
Fixes #8636.