You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Pattern 2 — Non-null placeholder `AdviceScope` for bookkeeping
234
+
235
+
Use a non-null `AdviceScope` with nullable internals only when exit advice still needs state even
236
+
if no tracing scope was started, for example:
237
+
238
+
-`CallDepth` tracking
239
+
- wrapped arguments or listeners that must be carried from enter to exit
240
+
- other bookkeeping that must survive even on the "no span started" path
241
+
242
+
In this pattern, `AdviceScope` is the carrier object for control-flow state, not just tracing
243
+
state. Nullable inner fields and exit guards are justified here.
244
+
245
+
### Do not use the defensive hybrid as a standard pattern
246
+
247
+
Avoid the hybrid shape where:
248
+
249
+
-`@Advice.Enter` is `@Nullable AdviceScope`
250
+
-`AdviceScope` still stores a nullable `Scope`
251
+
-`AdviceScope.end()` has an extra `if (scope == null)` guard
252
+
253
+
That double-guards the same condition and makes simple advice harder to reason about.
254
+
If the helper that creates the context can return `null`, prefer returning `null` from
255
+
`AdviceScope.start()` and only creating `AdviceScope` when the context is present.
256
+
257
+
### Async completion is not a separate `AdviceScope` state pattern
258
+
259
+
Async instrumentations may end spans from listeners, callbacks, or wrapped handlers instead of
260
+
directly in method exit. That affects **where** the scope/span is completed, but it does not
261
+
justify a third core `AdviceScope` state model. Async advice should still use either Pattern 1 or
262
+
Pattern 2 as appropriate.
263
+
175
264
## Never Throw Exceptions in Javaagent Code
176
265
177
266
Javaagent instrumentations must never throw exceptions. The goal is to be invisible to the
@@ -193,3 +282,5 @@ the instrumentation automatically rather than letting it fail at runtime.
193
282
-**`@Advice.OnMethodExit` method named `onEnter`** (or vice versa) — the method name should match the annotation. A mismatch is a copy-paste bug that compiles but confuses readers and may mask intent errors.
194
283
-**Advice referenced in `transform()` using anything other than `getClass().getName() + "$InnerAdvice"`** — see `javaagent-module-patterns.md` for the canonical pattern. Flag `this.getClass().getName() + "$InnerAdvice"` as a redundant qualifier, and flag both `InnerAdvice.class.getName()` and `OuterInstrumentation.class.getName() + "$InnerAdvice"` because any `.class` literal in a `transform()` method triggers unwanted class loading.
195
284
-**`onThrowable = Throwable.class` on return-only exit advice** — if the exit method only processes `@Advice.Return` and has no `@Advice.Enter` state to clean up, `onThrowable` should be omitted. The return value is `null`/zero on the exceptional path, and dereferencing it causes a suppressed exception for no benefit. Keep `suppress = Throwable.class` but remove `onThrowable`.
285
+
-**One-off `AdviceScope` method naming** — for new ordinary advice, prefer `start()` / `end()` for `AdviceScope` methods, and avoid introducing unique names such as `create()`.
286
+
-**Defensive hybrid `AdviceScope` in simple advice** — if `@Advice.Enter` is already nullable, flag simple advice that also stores a nullable inner `Scope`/`Context` and re-checks it inside `AdviceScope.end()`. Prefer returning `null` from the factory and keeping the created `AdviceScope` fully initialized.
Copy file name to clipboardExpand all lines: instrumentation/influxdb-2.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/influxdb/v2_4/InfluxDbScope.java
Copy file name to clipboardExpand all lines: instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-annotations/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/DefaultRequestContextInstrumentation.java
Copy file name to clipboardExpand all lines: instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-cxf-3.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/CxfRequestContextInstrumentation.java
+19-11Lines changed: 19 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -60,19 +60,26 @@ public static class AdviceScope {
Copy file name to clipboardExpand all lines: instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-resteasy-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/Resteasy30RequestContextInstrumentation.java
+17-10Lines changed: 17 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -41,19 +41,26 @@ public static class AdviceScope {
Copy file name to clipboardExpand all lines: instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-resteasy-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/Resteasy31RequestContextInstrumentation.java
+17-11Lines changed: 17 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -41,20 +41,26 @@ public static class AdviceScope {
Copy file name to clipboardExpand all lines: instrumentation/jaxrs/jaxrs-3.0/jaxrs-3.0-annotations/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v3_0/DefaultRequestContextInstrumentation.java
0 commit comments