Skip to content

Instruction audit: Cache repeated reflective accessors by their declaring type - #21582

Open
otelbot[bot] wants to merge 2 commits into
mainfrom
otelbot/instruction-audit-46156261593
Open

otelbot[bot] wants to merge 2 commits into
mainfrom
otelbot/instruction-audit-46156261593

Conversation

@otelbot

@otelbot otelbot Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This PR fixes a finding from an instruction audit run that checked the following repository rule:

For production Java code that repeats the same reflective method lookup on a path that may execute
more than once, cache the resulting Method or MethodHandle according to the actual declaring
type. Use one static final field for a fixed declaring type, including a version-selected
interface. A method from an interface or base class works for compatible implementations.
If the fixed type is optional or unavailable at compile time, load it once by name without
initialization, for example with Class.forName(name, false, helperClassLoader). Cache missing
methods too when supported versions may not provide them, using a nullable field for a fixed type.
When the required metadata varies by concrete runtime class, such as for unrelated generated
classes that each declare the member, use ClassValue instead of a static map keyed by Class<?>,
which can keep application class loaders alive. Neither an Object receiver nor an existing
getClass() or ClassValue proves that lookup depends on the runtime class. For example,
DomainSocketAddress.path() and unwrap() on a version-selected Connection interface each
use a fixed declaring type and do not need per-class entries. This caching rule does not apply to
test code or lookups that provably run once during initialization.

Explanation

Cache reflective accessors used by repeated instrumentation calls instead of rediscovering methods. Fixed API types share accessors across compatible implementations; unrelated generated types use ClassValue without a global class-keyed map. Missing optional methods remain unavailable without repeated probes, while Mongo writer settings remain instance-specific.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-11-28 18:31 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new accessor cache lacks installed-agent tests covering other resource types, missing getters, and repeated operations, leaving per-class isolation of cached results unverified.

1 open issue
What changed in this PR

This PR reduces repeated reflective lookups across instrumentation and agent tooling by caching accessors according to their declaring type.

Changes:

  • Cache fixed API accessors once and use ClassValue for generated types.
  • Cache unavailable optional getters while keeping MongoDB writer settings instance-specific.
  • Add regression tests for MongoDB settings isolation and Elasticsearch action names.
File Description
javaagent-tooling/​src/​main/​java/​io/​opentelemetry/​javaagent/​tooling/​field/​RuntimeFieldBasedImplementationSupplier.java Caches accessors per generated virtual-field class.
instrumentation/​twilio-6.6/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​twilio/​v6_6/​TwilioExperimentalAttributesExtractor.java Caches available and missing getters per resource class.
instrumentation/​spring/​spring-webflux/​spring-webflux-5.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​spring/​webflux/​v5_0/​client/​HttpProtocolVersion.java Shares protocol accessors by Reactor API type.
instrumentation/​spring/​spring-boot-resources/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​spring/​boot/​resources/​SystemHelper.java Lazily caches optional ProcessHandle accessors.
instrumentation/​mongo/​mongo-3.1/​library/​src/​test/​java/​io/​opentelemetry/​instrumentation/​mongo/​v3_1/​internal/​MongoDbAttributesGetterTest.java Verifies writer settings remain independent.
instrumentation/​mongo/​mongo-3.1/​library/​src/​main/​java/​io/​opentelemetry/​instrumentation/​mongo/​v3_1/​internal/​MongoDbAttributesGetter.java Caches writer-builder methods without sharing settings.
instrumentation/​hbase/​hbase-client-common-1.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​hbase/​client/​common/​v1_0/​HbaseClientUtil.java Caches shaded and unshaded descriptor accessors.
instrumentation/​elasticsearch/​elasticsearch-transport-common-5.0/​javaagent/​src/​unitTests/​java/​io/​opentelemetry/​javaagent/​instrumentation/​elasticsearch/​transport/​common/​v5_0/​ElasticsearchTransportAttributesGetterTest.java Tests action-name reuse and incompatible receivers.
instrumentation/​elasticsearch/​elasticsearch-transport-common-5.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​elasticsearch/​transport/​common/​v5_0/​ElasticsearchActionNames.java Caches the version-appropriate action-name accessor.

Review effort: Balanced

Comment on lines +29 to +32
private static final ClassValue<Map<String, Method>> getters =
new ClassValue<Map<String, Method>>() {
@Override
protected Map<String, Method> computeValue(Class<?> type) {
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The changed reflective extraction path lacks integration coverage for generated resources with missing getters and cache reuse across resource types, including attribute assertions on repeated calls.

1 open issue

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants