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
Changes from 22 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
126 changes: 126 additions & 0 deletions docs/contributing/writing-instrumentation-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,129 @@ 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
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
modules using this instrumentation strategy.

The most common way to instrument code with bytebuddy relies on inlining, this strategy will be
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
referred as "inlined" strategy in opposition to "indy".
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

### Indy modules and transition

Having all instrumentation rely on native "indy" instrumentation is a tedious task and can't be
achieved in a "big bang" step, we thus have to use intermediate steps.
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

Instrumentation modules that are "indy native" must have their `InstrumentationModule#isIndyModule`
implementation return `true`. Also, all the instrumentation advice methods annotated with
`@Advice.OnMethodEnter` or `@Advice.OnMethodExit` must have the `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".
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

This configuration is automatically enabled in CI when `test indy` label is added to a pull-request
or when the `-PtestIndy=true` parameter is added to gradle.

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

We have two sets of instrumentation modules:
- "indy compatible": compatible with both "indy" and "inlined", do not override `isIndyModule`
- "inlined only": only compatible with "inlined", `isIndyModule` returns `false`.

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.

### 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 to define
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
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)
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
public static Object onEnter(@Advice.Argument(1) Object request) {
return "enterValue";
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
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 to modify instrumented method arguments.
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

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:

```java
@Advice.OnMethodEnter(suppress = Throwable.class)
@Advice.AssignReturned.ToArguments(@ToArgument(1))
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 to modify instrumented method return value on exit advice.
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

When using non-inlined advices, reading the original return value is still done with `@Advice.Return`
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
annotated parameter, however modifying the value is done through the advice method return value
and `@Advice.AssignReturned.ToReturned`

```java
@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.AssignReturned.ToReturned
public static Object onEnter(@Advice.Return Object returnValue) {
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
return "hello";
}
```

### writing to internal class fields

With inlined advices, using the `@Advice.FieldValue(value = "fieldName", readOnly = false)` annotation
on advice method parameter allows to modify the `fieldName` field of the instrumented class.
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved

When using non-inlined advices, reading the original field value is still done with `@Advice.FieldValue`
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
annotated parameter, however modifying the value is done through the advice method return value
and `@Advice.AssignReturned.ToFields` annotation:

```java
@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.AssignReturned.ToFields(@ToField(value = "fieldName"))
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