Skip to content

Avoid retaining web apps that bundle Micrometer - #21574

Open
trask wants to merge 9 commits into
trask-micrometer-custom-measurementsfrom
copilot/agent-only-class-loader-fix
Open

trask wants to merge 9 commits into
trask-micrometer-custom-measurementsfrom
copilot/agent-only-class-loader-fix

Conversation

@trask

@trask trask commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

When a web app bundles Micrometer and registers a gauge through Metrics.globalRegistry, the Java agent's SDK can keep the app's class loader alive after undeploy. This PR breaks that strong reference path without interrupting gauge collection while the app runs.

Before: the SDK holds the callback strongly and keeps the app class loader alive. After: the SDK holds it weakly, and the Micrometer instrumentation loader keeps it alive while the app runs.

The Micrometer instrumentation class loader keeps callbacks alive and can unload with the app. Removing a meter releases its callback. Standalone registries still register directly.

Apps that share Micrometer still report, but a shared Metrics.globalRegistry can retain an undeployed app through its own measurement functions.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-12-04 05:02 UTC

Review the latest changes.

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.
  • Should this be with the author? Comment /dashboard route:author to route it back.
  • Anything wrong — including the routing? 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

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

Note

This is an experimental automated review from the OpenTelemetry PR review workflow. It can miss problems or flag things that are fine, so treat its findings as suggestions.

@trask
trask force-pushed the copilot/agent-only-class-loader-fix branch from 23f941a to 397f954 Compare December 2, 2026 23:49
@trask
trask removed this pull request from stack #21575 December 2, 2026 23:49
@trask
trask added this pull request to stack #21836 December 2, 2026 23:50
@trask
trask requested a balanced review from Copilot December 2, 2026 23:51

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 reflective strongReference call bypasses compile-time API checks and should use a gated direct call backed by a newer Micrometer testCompileOnly dependency.

2 open issues

Review effort: Balanced

@trask trask changed the title Prevent Micrometer callbacks from retaining application class loaders Avoid retaining web apps that bundle Micrometer Dec 3, 2026

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.

🟢 Approval recommended

No active review issues remain for the callback-lifecycle changes.

1 open issue

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.

🟢 Approval recommended

The Micrometer callback-lifecycle changes have no active review issues.

1 open issue

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.

🟢 Approval recommended

No active review issues were identified for the Micrometer callback-lifecycle changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@otelbot

otelbot Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Note

This is an experimental automated review from the OpenTelemetry PR review workflow. It can miss problems or flag things that are fine, so treat its findings as suggestions.

No findings on commit 3f651af5a52f1912f214b9b6c6853f1e91c6c2d9.

@trask
trask marked this pull request as ready for review December 4, 2026 05:01
@trask
trask requested a review from a team as a code owner December 4, 2026 05:01
trask and others added 9 commits December 4, 2026 08:34
Fix wrapped Micrometer gauge callback ownership
Run wrapped TimeGauge test when supported
Simplify Micrometer callback registration and tests

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep direct SDK callback registration in every registry. Defer Java agent ownership and wrapped gauge handling to follow-up changes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+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 App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+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 3f651af to cdbb699 Compare December 4, 2026 16:46

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