Skip to content

Commit 5561296

Browse files
authored
Set the current scope on ObservationRegistry when Scope#makeCurrent is called (#3808)
Currently, `scope#makeCurrent()` does not update the current observation scope on `ObservationRegistry`. This commit changes the `makeCurrent()` method to also update the scope on the registry.
1 parent 8a53a77 commit 5561296

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ public void makeCurrent() {
365365
for (SimpleScope simpleScope : scopes) {
366366
simpleScope.currentObservation.notifyOnScopeMakeCurrent();
367367
}
368+
this.registry.setCurrentObservationScope(this);
368369
}
369370

370371
}

micrometer-observation/src/test/java/io/micrometer/observation/CurrentObservationTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,21 @@ void nestedSamples_sameThread() {
113113
assertThat(registry.getCurrentObservation()).isNull();
114114
}
115115

116+
@Test
117+
void nestedScopes_makeCurrent() {
118+
Observation observation = Observation.createNotStarted("test.observation", registry);
119+
assertThat(registry.getCurrentObservationScope()).isNull();
120+
try (Observation.Scope scopeA = observation.openScope()) {
121+
assertThat(registry.getCurrentObservationScope()).isSameAs(scopeA);
122+
try (Observation.Scope scopeB = observation.openScope()) {
123+
assertThat(registry.getCurrentObservationScope()).isSameAs(scopeB);
124+
125+
scopeA.makeCurrent();
126+
assertThat(registry.getCurrentObservationScope()).isSameAs(scopeA);
127+
}
128+
assertThat(registry.getCurrentObservationScope()).isSameAs(scopeA);
129+
}
130+
assertThat(registry.getCurrentObservationScope()).isNull();
131+
}
132+
116133
}

0 commit comments

Comments
 (0)