Skip to content

Add opt-in instrumentation support for generated lambda classes - #12346

Open
amarziali wants to merge 28 commits into
masterfrom
andrea.marziali/lambda
Open

Add opt-in instrumentation support for generated lambda classes#12346
amarziali wants to merge 28 commits into
masterfrom
andrea.marziali/lambda

Conversation

@amarziali

@amarziali amarziali commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

What Does This Do?

Adds opt-in infrastructure for applying Datadog instrumentations to classes generated by LambdaMetafactory.

Generated lambda classes are normally defined without passing through the standard Java agent transformation path. This change instruments InnerClassLambdaMetafactory at the byte-generation point and routes
eligible lambda bytecode through the existing transformer before class definition.

The infrastructure is disabled by default and can be enabled with:

dd.trace.lambda.enabled=true

Enabling it alone does not select any production lambda classes. An enabled instrumentation must explicitly implement Instrumenter.ForLambda and register the exact functional interface it supports.

Notable Changes

  • Introduces Instrumenter.ForLambda for instrumentations targeting generated implementations of an exact functional interface.
  • Instruments java.lang.invoke.InnerClassLambdaMetafactory immediately after lambda bytecode generation.
  • Routes eligible generated classes through the existing matching, context-store, helper-injection, and transformation pipeline.
  • Supports the metafactory layouts used by JDK 8, JDK 11+, and the ClassFile API path used by newer JDKs.
  • Uses the module-aware ClassFileTransformer entry point on JDK 9+.
  • Applies only transformations registered for the generated lambda’s functional interface.
  • Avoids hidden-lambda cache pollution when symbolic class names are reused.
  • Skips the mechanism during GraalVM Native Image builds.

Compatibility and Limitations

Lambda transformation is intentionally best-effort:

  • The feature is disabled by default.
  • Reentrant lambda generation during transformation is skipped to prevent recursion.
  • CDS/AppCDS lambda classes that bypass runtime metafactory generation are not covered.
  • GraalVM Native Image builder JVMs are excluded. Instrumented lambda shapes conflict with Graal’s lambda-renaming analysis.
  • If a future JDK changes the metafactory byte-generation layout, the agent leaves the metafactory unchanged and emits a warning.
  • Security restrictions that prevent access to the defining loader, module, or protection domain cause transformation to fall back to the original bytes.

Motivation

Additional Notes

Contributor Checklist

Jira ticket: [PROJ-IDENT]

@amarziali

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-prod-us1-6

This comment has been minimized.

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

Datadog Autotest: FAIL

On JDK 8 and JDK 11, the new structure matcher requires a field that does not exist. The lambda transformer does not run, and Runnable lambdas still use wrappers.

Open Bits AI session

🤖 Datadog Autotest · Commit 054841c · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

@amarziali
amarziali force-pushed the andrea.marziali/lambda branch from 054841c to 5742c20 Compare August 31, 2026 12:41
@amarziali

Copy link
Copy Markdown
Contributor Author

@DataDog review

@amarziali
amarziali requested a review from mcculls August 31, 2026 13:04

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

Datadog Autotest: PASS

More details

The PR instruments generated Runnable lambdas so executor propagation keeps task identity, with supported JDK paths and a Graal build guard. This update fixes the earlier JDK 8 and 11 samBase mismatch and adds no new finding; the documented concurrent-reuse limitation remains, and no review finding is open.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Datadog Autotest · Commit 5655a6f · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

@amarziali

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-09T13:28:01.820141Z 490090a Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@amarziali
amarziali requested a review from bric3 August 31, 2026 13:09
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 5655a6ff32

ℹ️ 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".

@dd-octo-sts

dd-octo-sts Bot commented Aug 31, 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.04 s 13.96 s [-0.1%; +1.2%] (no difference)
startup:insecure-bank:tracing:Agent 12.86 s 13.00 s [-1.9%; -0.4%] (maybe better)
startup:petclinic:appsec:Agent 16.99 s 16.85 s [-0.3%; +2.0%] (no difference)
startup:petclinic:iast:Agent 16.92 s 16.58 s [-2.4%; +6.5%] (no difference)
startup:petclinic:profiling:Agent 16.34 s 16.88 s [-7.7%; +1.3%] (no difference)
startup:petclinic:sca:Agent 16.92 s 16.29 s [-0.5%; +8.3%] (no difference)
startup:petclinic:tracing:Agent 16.25 s 16.16 s [-0.5%; +1.6%] (no difference)

Commit: ec52a02c · 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.

@amarziali
amarziali marked this pull request as ready for review August 31, 2026 15:06
@amarziali
amarziali requested review from a team as code owners August 31, 2026 15:06
@amarziali
amarziali requested review from jordan-wong and removed request for a team August 31, 2026 15:06

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

⚠️ Autotest was unable to start this review.

Please try again by commenting @autotest review.

@dd-octo-sts

dd-octo-sts Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Hi! 👋 Thanks for your pull request! 🎉

To help us review it, please make sure to:

  • Add at least one type, and one component or instrumentation label to the pull request

If you need help, please check our contributing guidelines.

@amarziali amarziali added inst: java Core Java language instrumentation tag: concurrency Virtual Threads, Coroutines, Async, RX, Executors type: feature Enhancements and improvements labels Aug 31, 2026

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

⚠️ Autotest was unable to start this review.

Please try again by commenting @autotest review.

@amarziali

Copy link
Copy Markdown
Contributor Author

@DataDog review

@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: a721063dff

ℹ️ 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".

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

Datadog Autotest: FAIL

A hidden Runnable lambda can put its type description in the shared name cache. A later real class with the same legal binary name can receive wrong instrumentation on JDK 21 and later.

Open Bits AI session

🤖 Datadog Autotest · Commit a721063 · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

@amarziali

Copy link
Copy Markdown
Contributor Author

@DataDog review

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

Datadog Autotest: FAIL

The new module has no Gradle lock file. Its dependency graph can change between builds.

Open Bits AI session

🤖 Datadog Autotest · Commit b11c8c6 · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

Comment thread dd-java-agent/instrumentation/java/java-lambda/java-lambda-1.8/build.gradle Outdated
@amarziali
amarziali force-pushed the andrea.marziali/lambda branch from b64ffbe to 490090a Compare September 9, 2026 13:17
@amarziali
amarziali marked this pull request as ready for review September 9, 2026 13:17

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

Datadog Autotest: PASS

More details

The opt-in lambda path limits transformation to registered functional interfaces and keeps generated-class metadata out of shared caches.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Datadog Autotest · Commit 490090a · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

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

Labels

inst: java Core Java language instrumentation tag: concurrency Virtual Threads, Coroutines, Async, RX, Executors type: feature Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants