Skip to content

Clean up Micrometer callbacks when meters are removed or registries close - #21569

Merged
trask merged 4 commits into
mainfrom
copilot/open-telemetryopentelemetry-java-instrumentation-2
Dec 3, 2026
Merged

trask merged 4 commits into
mainfrom
copilot/open-telemetryopentelemetry-java-instrumentation-2

Conversation

@trask

@trask trask commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Closing an application-owned Micrometer registry can leave its observable callbacks registered with OpenTelemetry. Unregister callbacks when the registry closes or a meter is removed, including callbacks for time gauges and meters hidden from search.

@opentelemetry-pr-dashboard

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

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-12-03 15:50 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@trask trask changed the title [WIP] Extract explicit cleanup layer from open-telemetry/opentelemetry-java-instrumentation#21372 Clean up Micrometer callbacks without disrupting Spring’s global registry Nov 28, 2026
@trask
trask requested a balanced review from Copilot November 28, 2026 03:46

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 time-gauge wrapper lacks the OpenTelemetryInstrument marker, allowing composite reads to select a forwarding-only child and return NaN instead of reading from a capable registry.

1 open issue
What changed in this PR

Updates the Micrometer bridge to release observable callbacks when meters are removed or caller-owned registries close, while preserving the javaagent’s shared registry across Spring context shutdown.

Changes:

  • Adds idempotent callback cleanup, including hidden meters and time gauges.
  • Retains generic measurements until removal and protects the shared registry from both Spring closer implementations.
  • Adds lifecycle and shutdown regression tests and updates the API diff.
File Description
ActuatorTest.java Tests global metrics after Spring context shutdown.
SpringBootActuatorInstrumentationModule.java Registers both registry-closer instrumentations.
SpringBootActuatorIgnoredTypesConfigurer.java Allows instrumentation of Spring closer classes.
NestedMeterRegistryCloserInstrumentation.java Excludes the agent singleton from nested closer cleanup.
MeterRegistryCloserInstrumentation.java Prevents standalone closer tracking of the singleton.
ObservableCallbackLifecycleTest.java Tests callback removal and visible/hidden registry cleanup.
OpenTelemetryTimeGauge.java Adds a time-gauge wrapper with removal cleanup.
OpenTelemetryMeterRegistryBuilder.java Passes the internal close-disabled option.
OpenTelemetryMeterRegistry.java Creates removable time gauges and cleans callbacks on close.
OpenTelemetryMeter.java Retains measurements until removal and guards repeated cleanup.
OpenTelemetryLongTaskTimer.java Makes timer callback removal idempotent.
OpenTelemetryGauge.java Makes gauge callback removal idempotent.
OpenTelemetryFunctionTimer.java Guards repeated removal of both timer callbacks.
OpenTelemetryFunctionCounter.java Makes counter callback removal idempotent.
Internal.java Adds the internal close-disabled hook.
MicrometerSingletons.java Disables closure of the shared agent registry.
opentelemetry-micrometer-1.5.txt Records the registry’s explicit close() override.

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.

🔵 Needs a closer look

The nested registry closer constructor-advice path lacks integration coverage verifying that shutdown keeps the agent globally registered while closing and detaching Spring-owned registries.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Add integration coverage for Boot 3.5 constructor-advice shutdown

instrumentation/​spring/​spring-boot-actuator-autoconfigure-2.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​spring/​boot/​actuator/​autoconfigure/​v2_0/​NestedMeterRegistryCloserInstrumentation.java:50

This constructor-advice path has no integration coverage: the module's default test dependency is Boot 2.0 (which has no registry closer), while the newer Boot 4 implementation uses the standalone closer. The new shutdown test therefore does not verify removal from the nested closer's Iterable, the separate path used by the current Boot 3.5 implementation. Please add a wired agent-test execution pinned to a supported release with this constructor shape and run the shutdown regression there, checking that the agent remains globally registered while Spring-owned registries are closed and detached.

1 resolved since last review

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 lazily registered test suite can be skipped by normal check, leaving the Spring Boot 3.5.16 shutdown regression untested without an explicit lifecycle dependency.

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.

🔵 Needs a closer look

Missing lifecycle coverage for custom-meter ownership and cleanup requires human judgment about potential premature collection or retention regressions.

No open issues 🎉

1 resolved since last review

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 measurement-retention change lacks garbage-collection regression tests for observation survival and release of supplier-captured state after removal and registry close.

1 open issue

Review effort: Balanced

@trask
trask force-pushed the copilot/open-telemetryopentelemetry-java-instrumentation-2 branch from 2adc8a7 to 9f0bc69 Compare December 3, 2026 04:31
@trask
trask removed this pull request from stack #21836 December 3, 2026 05:11
@trask
trask added this pull request to stack #21842 December 3, 2026 05:11

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 are reported for the Micrometer callback-cleanup 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

No active review issues were identified in the callback cleanup and time-gauge changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@trask
trask marked this pull request as ready for review December 3, 2026 06:00
@trask
trask requested a review from a team as a code owner December 3, 2026 06:00
Base automatically changed from copilot/extract-spring-boot-micrometer-fix to main December 3, 2026 07:02
trask and others added 4 commits December 3, 2026 09:02
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>
@laurit
laurit force-pushed the copilot/open-telemetryopentelemetry-java-instrumentation-2 branch from ab89629 to d2f233a Compare December 3, 2026 07:02
@opentelemetry-pr-dashboard
opentelemetry-pr-dashboard Bot requested a balanced review from Copilot December 3, 2026 07:03

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 block the Micrometer callback lifecycle cleanup changes.

No open issues 🎉

Review effort: Balanced

@trask
trask merged commit 11d7122 into main Dec 3, 2026
74 checks passed
@trask
trask deleted the copilot/open-telemetryopentelemetry-java-instrumentation-2 branch December 3, 2026 15:49
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.

4 participants