Skip to content

Prevent the Micrometer javaagent from retaining application class loaders - #21574

Draft
trask wants to merge 4 commits into
copilot/open-telemetryopentelemetry-java-instrumentation-2from
copilot/agent-only-class-loader-fix
Draft

trask wants to merge 4 commits into
copilot/open-telemetryopentelemetry-java-instrumentation-2from
copilot/agent-only-class-loader-fix

Conversation

@trask

@trask trask commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

The Java agent's SDK can keep an undeployed application's class loader alive through Micrometer observable callbacks. The SDK now holds a weak delegate for callbacks from the agent's registry. Standalone registries still register callbacks directly. This PR and the explicit cleanup in #21569 replace #21372.

When a child application shares its parent's Micrometer, the application's measurement function or strongly held gauge object owns the callback. This includes suppliers hidden by Micrometer's own wrappers. Explicit meter removal releases the callback without waiting for the loader to unload.

The ownership check follows the parent chain and does not cover sibling-loader delegation. The shared-Micrometer regressions register directly with the agent registry and use a null thread context class loader to isolate callback ownership from the global composite and the separate SDK thread context class-loader retention issue.

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-28 05:17 UTC

Move out of draft to request review.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@trask trask changed the title [WIP] Extract agent-only class-loader fix from PR #21372 Prevent the Micrometer javaagent from retaining application class loaders Nov 28, 2026
@trask
trask requested a balanced review from Copilot November 28, 2026 05:27

Copilot AI 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.

🟡 Changes recommended

Supplier and strong-reference gauges can fall back to Meter.Id callback ownership, retaining plugin class loaders when Micrometer is shared with the agent.

2 open issues
What changed in this PR

Introduces agent-specific callback ownership for Micrometer class-loader cleanup, building on #21569 while preserving standalone-library registration behavior.

Changes:

  • Routes observable-meter callbacks through an internal registration abstraction.
  • Uses weak SDK delegates with class-scoped ownership and explicit release on removal.
  • Adds Gauge and TimeGauge regressions for disposable and shared class loaders.
File Description
OpenTelemetryMeterRegistryBuilder.java Defaults to direct registration and accepts an internal override.
OpenTelemetryMeterRegistry.java Passes the registrar and ownership metadata to observable meters.
OpenTelemetryMeter.java Routes custom-meter callbacks through the registrar.
OpenTelemetryLongTaskTimer.java Manages active-task and duration callback registrations.
OpenTelemetryGauge.java Registers gauge callbacks with function ownership metadata.
OpenTelemetryFunctionTimer.java Applies ownership handling to count and total-time callbacks.
OpenTelemetryFunctionCounter.java Applies ownership handling to function-counter callbacks.
ObservableCallbackRegistrar.java Defines registration handles, release hooks, and the direct default.
Internal.java Adds the internal registrar configuration bridge.
MicrometerCallbackGcCloseTest.java Tests metric export and class-loader collection.
GaugeRegistrar.java Supplies disposable-loader Gauge and TimeGauge fixtures.
MicrometerSingletons.java Enables the agent-specific registrar.
MicrometerCallbackRegistrar.java Implements weak delegation, ownership selection, and release.

Review effort: Balanced

Copilot AI 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.

🟡 Changes recommended

The parent-only callback ownership lookup fails under sibling-loader delegation, causing the shared Meter.Id ClassValue entry to retain the application's measurement function and class loader and prevent unloading.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment on lines +102 to +106
while (loader != null && loader != ownerLoader) {
loader = loader.getParent();
if (loader == ownerLoader) {
return functionClass;
}
trask and others added 4 commits November 28, 2026 10:07
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@trask
trask force-pushed the copilot/agent-only-class-loader-fix branch from b7a0071 to a16a100 Compare November 28, 2026 18:08

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants