Skip to content

Commit f5ec59f

Browse files
authored
Polish "Fixing Scope setting and resetting via ObservationThreadLocalAccessor" (#3624)
See gh-3613
1 parent 42b4500 commit f5ec59f

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

micrometer-core/src/test/kotlin/io/micrometer/core/instrument/kotlin/AsContextElementKtTests.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ internal class AsContextElementKtTests {
5353

5454
@Test
5555
fun `should return observation from coroutine context when KotlinContextElement present`(): Unit = runBlocking {
56-
val observationRegistry = ObservationRegistry.create()
5756
observationRegistry.observationConfig().observationHandler { true }
5857
val nextObservation = Observation.start("name", observationRegistry)
5958
val inScope = nextObservation.openScope()

micrometer-observation/src/main/java/io/micrometer/observation/Observation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ interface Scope extends AutoCloseable {
757757

758758
/**
759759
* Clears the current scope and notifies the handlers that the scope was closed.
760-
* You don't need to call this method manually, if you use try-with-resource, it
760+
* You don't need to call this method manually. If you use try-with-resource, it
761761
* will call this for you. Please only call this method if you know what you are
762762
* doing and your use-case demands the usage of it.
763763
*/
@@ -766,9 +766,10 @@ interface Scope extends AutoCloseable {
766766

767767
/**
768768
* Clears the current scope and notifies the handlers that the scope was reset.
769-
* You don't need to call this method in most of the cases, please only call this
769+
* You don't need to call this method in most of the cases. Please only call this
770770
* method if you know what you are doing and your use-case demands the usage of
771771
* it.
772+
* @since 1.10.4
772773
*/
773774
void reset();
774775

micrometer-observation/src/main/java/io/micrometer/observation/ObservationHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ default void onScopeClosed(T context) {
7373
* Reacts to resetting of scopes. If your handler uses a {@link ThreadLocal} value,
7474
* this method should clear that {@link ThreadLocal}.
7575
* @param context an {@link Observation.Context}
76+
* @since 1.10.4
7677
*/
7778
default void onScopeReset(T context) {
7879
}

micrometer-observation/src/main/java/io/micrometer/observation/contextpropagation/ObservationThreadLocalAccessor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ public void restore(Observation value) {
7474
reset();
7575
Observation.Scope observationScope = value.getContext().get(SCOPE_KEY);
7676
if (observationScope != null) {
77-
observationScope.close(); // We close the previous scope -
78-
// it will put its parent as current and call all handlers
77+
// We close the previous scope - it will put its parent as current and call
78+
// all handlers.
79+
observationScope.close();
7980
}
80-
setValue(value); // We open the previous scope again, however this time in TL
81-
// we have the whole hierarchy of scopes re-attached via handlers
81+
// We open the previous scope again, however this time in TL we have the whole
82+
// hierarchy of scopes re-attached via handlers.
83+
setValue(value);
8284
}
8385

8486
}

0 commit comments

Comments
 (0)