Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indy advice docs & migration procedure #11546

Merged
merged 48 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
4dd52a9
hibernate + minimal doc
SylvainJuge Jun 10, 2024
7766f35
awk sdk
SylvainJuge Jun 10, 2024
07a5f90
spotless
SylvainJuge Jun 10, 2024
c1ba53b
remove leftover
SylvainJuge Jun 10, 2024
b7b3676
add elasticsearch
SylvainJuge Jun 10, 2024
41abb88
add netty
SylvainJuge Jun 10, 2024
b03321b
inline aws
SylvainJuge Jun 11, 2024
8827c1e
hibernate-3
SylvainJuge Jun 11, 2024
9efaf2d
hibernate-4
SylvainJuge Jun 11, 2024
bcaeaf4
document advice local variables
SylvainJuge Jun 11, 2024
8dbccdc
document indy modules
SylvainJuge Jun 11, 2024
c012aac
hibernate-6
SylvainJuge Jun 11, 2024
27e36cd
hibernate-procedure-call
SylvainJuge Jun 11, 2024
98f74d0
aws remove explicit inline
SylvainJuge Jun 11, 2024
957a7b8
reword documentation with transition proposal
SylvainJuge Jun 11, 2024
25660bd
revert indy module for hibernate-*
SylvainJuge Jun 11, 2024
ac0e649
move aws-sdk to #11552
SylvainJuge Jun 11, 2024
f48e203
move hibernate to #11553
SylvainJuge Jun 11, 2024
9aaf628
move elasticsearch to #11554
SylvainJuge Jun 11, 2024
cd85238
document params, return value and fields
SylvainJuge Jun 11, 2024
ae87603
move netty-* to #11559
SylvainJuge Jun 12, 2024
7fc8e15
revert changes for akka
SylvainJuge Jun 12, 2024
c477985
add indy/inline clarification
SylvainJuge Jun 12, 2024
fc0be19
post-review changes
SylvainJuge Jun 26, 2024
cfefa13
clarify with 3 buckets
SylvainJuge Jun 26, 2024
530609c
document for indy native + comment for indy compat
SylvainJuge Jun 26, 2024
ce07791
add clarification on typing
SylvainJuge Jun 26, 2024
2d73c74
loading classes in app CL
SylvainJuge Jun 26, 2024
9c9a1a2
post-review fix inconsistency
SylvainJuge Jun 27, 2024
f976a12
remove redundant comment on indy inlined
SylvainJuge Jul 1, 2024
bf29897
Update docs/contributing/writing-instrumentation-module.md
SylvainJuge Jul 1, 2024
8287992
Apply suggestions from code review
SylvainJuge Jul 1, 2024
0a6bbc1
Apply suggestions from code review
SylvainJuge Jul 1, 2024
0d089c2
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Jul 9, 2024
547d422
docuemnt advice method signature limitations
SylvainJuge Jul 9, 2024
e84d908
Merge branch 'indy-dispatch' of github.com:SylvainJuge/opentelemetry-…
SylvainJuge Jul 9, 2024
e1bdfaf
Apply suggestions from code review
SylvainJuge Jul 10, 2024
ac90f56
bytebuddy -> ByteBuddy
SylvainJuge Jul 18, 2024
f254887
remove advice method signature limitation
SylvainJuge Aug 12, 2024
fe702b7
remove limitation on advice return type
SylvainJuge Aug 12, 2024
5625aa1
Apply suggestions from code review
SylvainJuge Aug 14, 2024
a1d30a9
Apply suggestions from code review
SylvainJuge Aug 14, 2024
89624af
remove dynamic assignment requirement
SylvainJuge Aug 14, 2024
87d535c
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Aug 14, 2024
14f3423
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Aug 19, 2024
db007a4
capitalize for consistency
SylvainJuge Aug 19, 2024
da744fa
capitalize headers
SylvainJuge Aug 20, 2024
3405ae1
remove paragraph
SylvainJuge Aug 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/agent-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ provides.
- Can set different defaults for properties
- Can customize tracer configuration programmatically
- Can provide custom exporter, propagator, sampler
- Can hook into bytebuddy to customize bytecode manipulation
- Can hook into ByteBuddy to customize bytecode manipulation
- Noteworthy instrumentation
- Log injection of IDs (logback, log4j2, log4j)
- Automatic context propagation across `Executor`s
Expand Down
163 changes: 163 additions & 0 deletions docs/contributing/writing-instrumentation-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,166 @@ For example:
```

[suppress]: https://opentelemetry.io/docs/instrumentation/java/automatic/agent-config/#suppressing-specific-auto-instrumentation

## Use non-inlined advice code with `invokedynamic`

Using non-inlined advice code is possible thanks to the `invokedynamic` instruction, this strategy
is referred as "indy" in reference to this. By extension "indy modules" are the instrumentation
modules using this instrumentation strategy.

The most common way to instrument code with ByteBuddy relies on inlining, this strategy will be
referred as "inlined" strategy as opposed to "indy".

For inlined advices, the advice code is directly copied into the instrumented method.
In addition, all helper classes are injected into the classloader of the instrumented classes.

For indy, advice classes are not inlined. Instead, they are loaded alongside all helper classes
into a special `InstrumentationModuleClassloader`, which sees the classes from both the instrumented
application classloader and the agent classloader.
The instrumented classes call the advice classes residing in the `InstrumentationModuleClassloader` via
invokedynamic bytecode instructions.

SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
Making the instrumentation indy
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

- allows instrumentations to have breakpoints set in them and be debugged using standard debugging techniques
- provides clean isolation of instrumentation advice from the application and other instrumentations
- allows advice classes to contain non-static fields and methods - in fact generally good development practices are enabled (whereas inlined advices are [restricted in how they can be implemented](#use-advice-classes-to-write-code-that-will-get-injected-to-the-instrumented-library-classes))
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

### Indy modules and transition

Making an instrumentation "indy" compatible (or native "indy") is not as straightforward as making it "inlined".
However, ByteBuddy provides a set of tools and APIs that are mentioned below to make the process as smooth as possible.

Due to the changes needed on most of the instrumentation modules the migration can't be achieved in a single step,
we thus have to use intermediate steps.

Instrumentation modules that are "indy native" must have:
- `InstrumentationModule#isIndyModule` implementation return `true`
- advice methods annotated with `@Advice.OnMethodEnter` or `@Advice.OnMethodExit` with `inlined = false` explicitly set.
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

The `otel.javaagent.experimental.indy` (default `false`) configuration option allows to opt-in for
using "indy". When set to `true`, the `io.opentelemetry.javaagent.tooling.instrumentation.indy.AdviceTransformer`
will transform advices automatically to make them "indy native". Using this option is temporary and will
be removed once all the instrumentations are "indy native".

This configuration is automatically enabled in CI with `testIndy*` checks or when the `-PtestIndy=true` parameter is added to gradle.
trask marked this conversation as resolved.
Show resolved Hide resolved

In order to preserve compatibility with both instrumentation strategies, we have to omit the `inlined = false`
from the advice method annotations.

We have three sets of instrumentation modules:
- "inlined only": only compatible with "inlined", `isIndyModule` returns `false`.
- "indy compatible": compatible with both "indy" and "inlined", do not override `isIndyModule`, advices are modified with `AdviceTransformer` to be made "indy native" or "inlined" at runtime.
- "indy native": only compatible with "indy" `isIndyModule` returns `true`.

The first step of the migration is to move all the "inlined only" to the "indy compatible" category
by refactoring them with the limitations described below.

Once everything is "indy compatible", we can evaluate changing the default value of `otel.javaagent.experimental.indy`
to `true` and make it non-experimental.
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

### shared classes and common classloader

By default, all the advices of an instrumentation module will be loaded into isolated classloaders,
one per instrumentation module. Some instrumentations require to use a common classloader in order
to preserve the semantics of `static` fields and to share classes.
trask marked this conversation as resolved.
Show resolved Hide resolved

In order to load multiple `InstrumentationModule` implementations in the same classloader, you need to
override the `ExperimentalInstrumentationModule#getModuleGroup` to return an identical value.

### classes injected in application classloader

Injecting classes in the application classloader is possible by implementing the
`ExperimentalInstrumentationModule#injectedClassNames` method. All the class names listed by the
returned value will be loaded in the application classloader instead of the agent or instrumentation
module classloader.

This allows for example to access package-private methods that would not be accessible otherwise.

### advice local variables
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

With inlined advices, declaring an advice method argument with `@Advice.Local` allows defining
a variable that is local to the advice execution for communication between the enter and exit advices.

When advices are not inlined, usage of `@Advice.Local` is not possible. It is however possible to
return a value from the enter advice and get the value in the exit advice with a parameter annotated
with `@Advice.Enter`, for example:

```java
@Advice.OnMethodEnter(suppress = Throwable.class, inlined = false)
public static Object onEnter(@Advice.Argument(1) Object request) {
return "enterValue";
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inlined = false)
public static void onExit(@Advice.Argument(1) Object request,
@Advice.Enter Object enterValue) {
// do something with enterValue
}
```

### modifying method arguments

With inlined advices, using the `@Advice.Argument` annotation on method parameter with `readOnly = false`
allows modifying instrumented method arguments.

When using non-inlined advices, reading the argument values is still done with `@Advice.Argument`
annotated parameters, however modifying the values is done through the advice method return value
and `@Advice.AssignReturned.ToArguments` annotation:

For return type, the `typing = Assigner.Typing.DYNAMIC` is needed to instruct ByteBuddy to properly cast the
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
value to the appropriate type.

```java
@Advice.OnMethodEnter(suppress = Throwable.class, inlined = false)
@Advice.AssignReturned.ToArguments(@ToArgument(value = 1, typing = Assigner.Typing.DYNAMIC))
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
public static Object onEnter(@Advice.Argument(1) Object request) {
return "hello";
}
```

It is possible to modify multiple arguments at once by using an array, see usages of
`@Advice.AssignReturned.ToArguments` for detailed examples.

### modifying method return value

With inlined advices, using the `@Advice.Return` annotation on method parameter with `readOnly = false`
allows modifying instrumented method return value on exit advice.

When using non-inlined advices, reading the original return value is still done with the `@Advice.Return`
annotated parameter, however modifying the value is done through the advice method return value
and `@Advice.AssignReturned.ToReturned`.

For return type, the `typing = Assigner.Typing.DYNAMIC` is needed to instruct ByteBuddy to properly cast the
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
value to the appropriate type.

```java
@Advice.OnMethodExit(suppress = Throwable.class, inlined = false)
@Advice.AssignReturned.ToReturned(typing = Assigner.Typing.DYNAMIC)
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
public static Object onExit(@Advice.Return Object returnValue) {
return "hello";
}
```

### writing to internal class fields

With inlined advices, using the `@Advice.FieldValue(value = "fieldName", readOnly = false)` annotation
on advice method parameters allows modifying the `fieldName` field of the instrumented class.

When using non-inlined advices, reading the original field value is still done with the `@Advice.FieldValue`
annotated parameter, however modifying the value is done through the advice method return value
and `@Advice.AssignReturned.ToFields` annotation.

For return type, the `typing = Assigner.Typing.DYNAMIC` is needed to instruct ByteBuddy to properly cast the
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
value to the appropriate type.

```java
@Advice.OnMethodEnter(suppress = Throwable.class, inlined = false)
@Advice.AssignReturned.ToFields(@ToField(value = "fieldName", typing = Assigner.Typing.DYNAMIC))
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
public static Object onEnter(@Advice.FieldValue("fieldName") Object originalFieldValue) {
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
return "newFieldValue";
}
```

It is possible to modify multiple fields at once by using an array, see usages of
`@Advice.AssignReturned.ToFields` for detailed examples.
Loading