Skip to content

Make decorator exception safe 1/3 - #11998

Closed
PerfectSlayer wants to merge 7 commits into
masterfrom
bbujon/context
Closed

PerfectSlayer wants to merge 7 commits into
masterfrom
bbujon/context

Conversation

@PerfectSlayer

@PerfectSlayer PerfectSlayer commented Jul 20, 2026 •

Copy link
Copy Markdown
Collaborator

What Does This Do

This is a follow up of #11951 and proposes an improvement to instrumentation API decorators to make them "exception safe".

The main ideas are:

  • Simple / easy to use API for instrumentations - they are left untouched to avoid large refactoring but can be deduplicated at some later point
  • Smaller SPI to implement - refactored few implementations to only have 1 method per signal (vs the multiple existing overloads) to implement
  • A construct that prevent exception to bubble into application code - beforeFinish to call / doBeforeFinish to implementation, where safety is done on beforeFinish that can't be overridden.

Additionally:

  • I added nullability information too because I find the default pattern quite confusing (calling onEvent with null values when the even did not happen),
  • I simplified existing child class to override the single method per signal - the one with all parameters

Motivation

The goal is to simplify how write instrumentations, and make the dumb way safe.

Additional Notes

This part is part of stack PRs:

Contributor Checklist

Jira ticket: [PROJ-IDENT]

@datadog-prod-us1-5

This comment has been minimized.

@dd-octo-sts

dd-octo-sts Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.03 s 13.96 s [-0.2%; +1.2%] (no difference)
startup:insecure-bank:tracing:Agent 12.96 s 12.94 s [-0.7%; +1.0%] (no difference)
startup:petclinic:appsec:Agent 16.88 s 16.82 s [-0.6%; +1.4%] (no difference)
startup:petclinic:iast:Agent 16.95 s 16.93 s [-0.8%; +1.0%] (no difference)
startup:petclinic:profiling:Agent 16.81 s 16.91 s [-2.0%; +0.8%] (no difference)
startup:petclinic:sca:Agent 16.96 s 16.79 s [+0.2%; +1.9%] (maybe worse)
startup:petclinic:tracing:Agent 16.11 s 16.10 s [-0.8%; +0.9%] (no difference)

Commit: c86d62b5 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@PerfectSlayer
PerfectSlayer marked this pull request as ready for review July 20, 2026 10:29
@PerfectSlayer
PerfectSlayer requested review from a team as code owners July 20, 2026 10:29
@PerfectSlayer
PerfectSlayer requested review from dougqh and vandonr and removed request for a team July 20, 2026 10:29

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e4fd8b9d1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/add_new_instrumentation.md Resolved

@datadog-prod-us1-5 datadog-prod-us1-5 Bot 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.

Datadog Autotest: WARN

In OpenAiDecorator.doBeforeFinish, the override incorrectly calls super.doBeforeFinish(span) instead of super.doBeforeFinish(context): span is derived from the context via fromContext and can be null (violating @Nonnull), and even when non-null it is semantically wrong — the original context may carry additional keyed data that would be silently dropped if a parent class ever adds a non-empty doBeforeFinish body. The fix — already committed — is a one-line change to pass context instead of span.

View proposed fix
📊 Validated against 6 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit 5e4fd8b · What is Autotest? · Any feedback? Reach out in #autotest

@PerfectSlayer PerfectSlayer changed the title Make decorator exception safe Make decorator exception safe 1/3 Jul 21, 2026
Decorator lifecycle hooks (`onError`, `beforeFinish`, …) are called from advice around
`scope.close()` / `span.finish()`, so a decorator that throws would leak scopes and spans — and
force every call site into defensive `try/finally` blocks. To prevent this, decorators make
these public methods `final` and routes them through an exception barrier that logs and swallows any

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.

Suggested change
these public methods `final` and routes them through an exception barrier that logs and swallows any
these public methods `final` and route them through an exception barrier that logs and swallows any

protected void finishInferredProxySpan(Context context) {
InferredProxySpan inferredProxySpan;
if ((inferredProxySpan = InferredProxySpan.fromContext(context)) != null) {
inferredProxySpan.finish(AgentSpan.fromContext(context));

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.

I'd keep AgentSpan.fromContext throughout here as the class tells what it's extracting


protected void doOnError(
@Nullable final AgentSpan span, @Nullable final Throwable throwable, byte errorPriority) {
if (throwable != null && span != null) {

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.

WDYT about moving these null checks to the outermost onError methods, like you did for beforeFinish?

That way anyone extending doOnError won't have to check them again

@mcculls mcculls 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.

Nice improvement - just a small suggestion about moving the null checks out of doOnError and into the respective onError final methods

@PerfectSlayer

Copy link
Copy Markdown
Collaborator Author

Merged in #12017

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants