Deprecate unused Observation.Scope-related methods by shakuzen · Pull Request #7255 · micrometer-metrics/micrometer (original) (raw)

added 2 commits

March 6, 2026 12:09

@shakuzen

getEnclosingScope() was added in 1.10.6 (PR micrometer-metrics#3731) so that ObservationThreadLocalAccessor.restore(Observation) could obtain the observation's enclosing scope and call makeCurrent() before setValue().

In 1.10.x (PR micrometer-metrics#3831, NullObservation) the restore logic was changed to use the registry's current scope and getPreviousObservationScope() instead. Since then, getEnclosingScope() has not been used by any production code; the only remaining callers are tests in ObservationThreadLocalAccessorTests.

Deprecating the method is a first step toward removing it and the lastScope field in SimpleObservation, which will reduce per-observation overhead (ConcurrentHashMap field and scope bookkeeping on open/close).

@shakuzen

makeCurrent() was added in 1.10.6 (PR micrometer-metrics#3731) so that ObservationThreadLocalAccessor.restore(Observation) could re-apply an observation's scope when restoring: it called value.getEnclosingScope() then enclosingScope.makeCurrent() before setValue(value).

In 1.10.x (PR micrometer-metrics#3831, NullObservation) the restore logic was changed to use the registry's current scope and closeCurrentScope() instead. Since then, makeCurrent() has not been used by any production code; the only remaining caller is the test CurrentObservationTest.nestedScopes_makeCurrent().

Deprecating the method aligns with the getEnclosingScope() deprecation and allows future removal of the scope-rebuild logic in SimpleScope that exists only to support makeCurrent().

@shakuzen

Scope.reset() was used by ObservationThreadLocalAccessor until 1.10.x (PR micrometer-metrics#3831, NullObservation). That flow called reset() to clear thread locals before making another scope current. After micrometer-metrics#3831, OTLA uses closeCurrentScope() with scope.close() instead, so Scope.reset() is no longer used by any production code; the only remaining caller is ObservationValidatorTests.scopeResetAfterStopShouldBeValid().

ObservationHandler.onScopeReset(Context) is only invoked when Scope.reset() or Scope.makeCurrent() runs. Both are now deprecated and unused in production, so the callback is deprecated as well. Handlers should use onScopeClosed for cleanup when a scope is closed.

Deprecating these aligns with deprecating getEnclosingScope() and Scope.makeCurrent() and allows future removal of the reset/makeCurrent-related code paths.

@shakuzen shakuzen changed the titleDeprecate ObservationView.getEnclosingScope() and Scope.makeCurrent() Deprecate unused Scope-related methods

Mar 6, 2026

jonatan-ivanov

@shakuzen shakuzen changed the titleDeprecate unused Scope-related methods Deprecate unused Observation.Scope-related methods

Mar 10, 2026

etrandafir93 pushed a commit to etrandafir93/micrometer that referenced this pull request

Mar 21, 2026

@shakuzen @etrandafir93

getEnclosingScope() was added in 1.10.6 (PR micrometer-metrics#3731) so that ObservationThreadLocalAccessor.restore(Observation) could obtain the observation's enclosing scope and call makeCurrent() before setValue().

In 1.10.x (PR micrometer-metrics#3831, NullObservation) the restore logic was changed to use the registry's current scope and getPreviousObservationScope() instead. Since then, getEnclosingScope() has not been used by any production code; the only remaining callers are tests in ObservationThreadLocalAccessorTests.

Deprecating the method is a first step toward removing it and the lastScope field in SimpleObservation, which will reduce per-observation overhead (ConcurrentHashMap field and scope bookkeeping on open/close).

makeCurrent() was added in 1.10.6 (PR micrometer-metrics#3731) so that ObservationThreadLocalAccessor.restore(Observation) could re-apply an observation's scope when restoring: it called value.getEnclosingScope() then enclosingScope.makeCurrent() before setValue(value).

In 1.10.x (PR micrometer-metrics#3831, NullObservation) the restore logic was changed to use the registry's current scope and closeCurrentScope() instead. Since then, makeCurrent() has not been used by any production code; the only remaining caller is the test CurrentObservationTest.nestedScopes_makeCurrent().

Deprecating the method aligns with the getEnclosingScope() deprecation and allows future removal of the scope-rebuild logic in SimpleScope that exists only to support makeCurrent().

Scope.reset() was used by ObservationThreadLocalAccessor until 1.10.x (PR micrometer-metrics#3831, NullObservation). That flow called reset() to clear thread locals before making another scope current. After micrometer-metrics#3831, OTLA uses closeCurrentScope() with scope.close() instead, so Scope.reset() is no longer used by any production code; the only remaining caller is ObservationValidatorTests.scopeResetAfterStopShouldBeValid().

ObservationHandler.onScopeReset(Context) is only invoked when Scope.reset() or Scope.makeCurrent() runs. Both are now deprecated and unused in production, so the callback is deprecated as well. Handlers should use onScopeClosed for cleanup when a scope is closed.

Deprecating these aligns with deprecating getEnclosingScope() and Scope.makeCurrent() and allows future removal of the reset/makeCurrent-related code paths.

shakuzen added a commit that referenced this pull request

Apr 1, 2026

@shakuzen

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})