Next steps to making the invokedynamic instrumentation approach the default #13031
Description
We want to make the invokedynamic-based instrumentation approach the default one eventually.
This issue is intended to document the next steps towards this goal.
Per discussion in the SIG-Meeting, we'll do the following things first before switching the default of otel.javaagent.experimental.indy
from false
to true
:
- Rewrite advice source code of included instrumentations to not require runtime-transformation (see below section for details)
- Ensure
VirtualField
s work efficiently in indy-mode- Enhance muzzle to pick up
VirtualField.find
calls from static initializers of helpers and advice classes - Replace all direct usages of
VirtualField.find
in advice classes with static fields in helpers
- Enhance muzzle to pick up
After that is done, we should be able to default otel.javaagent.experimental.indy
to true
. Based on our confidence in the feature, we'll have to decide whether to do this in a minor or major release.
Inline advice will still be technically supported by overriding that config option back to false
. This leaves us an escape hatch in case we find bugs without impacting users much (they can immediately switch back to false
to avoid the bugs).
Then, in a later major version we'll remove support for inlined-advice and then can start gaining the benefits, such as removing shading.
In addition, we should clean up the APIs added to ExperimentalInstrumentationModule
(see the API feature to (not) promote sections below). This is however not considered a blocker for defaulting otel.javaagent.experimental.indy
to true
.
Advice source code migration
For instrumentations to work with indy, the advice code needs to be written slightly differently, as described in the guide. This is currently done automatically for most advices at runtime via a runtime transformation of the advice bytecode. This of course is not ideal from a maintenance perspective, as the source-code doesn't match what is actually executed.
I'd suggest to do the following:
- Add a new marker method
boolean requiresAdviceRewritingForIndy()
toExperimentalInstrumentationModule
and only apply the runtime-transformation accordingly - Rewrite all the instrumentation advice code to no require the runtime transformation anymore (aka apply the guide)
- Only exception: Keep
inlined = true
(which is the default): All other changes are backwards compatible and allow us to still run withotel.javaagent.experimental.indy=false
- Once we remove support for inlined advice, we can add
inlined = false
, which is an easy, IDE-supported change (e.g. IntelliJ structural replace feature) - I'll try to provide an automated tool to do the more complex sourcecode-transformation (e.g. instering
@Advice.AssignReturned
) based on javaparser a tool for AST-based, structural code modification with a lexically preserving printer (e.g. original comments and whitespace are preserved)
- Only exception: Keep
- We can then disable the
AdviceTransformer
for instrumentations shipped with the agent. For external instrumentations, we can keep it around for some grace period and print a warning (e.g.Please migrate your advices, here is a link to our guide
).
API features to promote
With the completion #11457, we'll have all our instrumentation capable of working with invokedynamic. We need to make sure to also have a clean plugin interface with indy-support when making it the default for external instrumentations. Currently, those features live in the ExperimentalInstrumentationModule
class.
We'll need to clean up and migrate the relevant features to InstrumentationModule
:
In order to make the
- Promote injectClasses(ClassInjector)
- Replace injectedClassNames with a method on
ClassInjector
instead - Promote the module-opening mechanism added via make rmi instrumentation indy-compatible + add module opener #12585
The following two features I'm not sure about whether we want to actually have them in the public API or whether we'll keep them for internal modules only:
API features not to promote
getModuleGroup
getModuleGroup(): This feature was added to ease the conversion of some instrumentations which are tightly coupled by accessing classes from each other directly. A cleaner approach for this requirement is to have:
- A shared API-project for those instrumentations which is loaded by the agent-classloader (=non-helper classes)
- Have the individual instrumentations communicate through that API-layer instead
This would involve a very big refactor for our existing instrumentations, but I don't feel like this is the case for external instrumentations. Therefore I think it would be better in terms of "best-practices" to not have this method part of the public API to promote the correct way of doing things. Maybe we could rewrite one of the internal modules (after inlining is not supported anymore) to showcase how to do this.
agentPackagesToHide
agentPackagesToHide(): This method is used when trying to instrument classes which are also present in the agent-classloader: The problem is that for linking, classes found in the agent classloader take precedence over classes from the instrumented classloader. This means that if the classes are present in both, the instrumentation won't be able to actually use/instrument them. I feel like this is an esoteric edge which should not be exposed to the public api in order to keep it simpler, as it should be very rare to stumble across.
An example where this is needed internally is the instrumentation of the opentelemetry-api: The instrumentation needs to bridge application opentelemetry-APIs, while at the same timing bringing it's own opentelemetry-API version (eventually unshaded).