Skip to content

Commit 4debb04

Browse files
ObservationAwareBaggageThreadLocalAccessor loses scopes in the wrong order
fixed via changing the scope closing order fixes gh-579, gh-635
1 parent e5e70b9 commit 4debb04

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2023 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.tracing.contextpropagation;
17+
18+
public class TestObservationAwareBaggageThreadLocalAccessor {
19+
20+
public static void closeCurrentScope(ObservationAwareBaggageThreadLocalAccessor accessor) {
21+
accessor.closeCurrentScope();
22+
}
23+
24+
}

micrometer-tracing-tests/micrometer-tracing-integration-test/src/test/java/io/micrometer/tracing/test/contextpropagation/AbstractObservationAwareSpanThreadLocalAccessorTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,23 @@ void onlyReactorPropagatesBaggageWithContextCaptureAndObservation() {
376376
assertThat(tenant).isEqualTo("tenantValue");
377377
}
378378

379+
@Test
380+
void observationAwareBaggageThreadLocalAccessorSetsAndClosesBaggageToPropagate() {
381+
then(getTracer().currentTraceContext().context()).isNull();
382+
383+
Observation.createNotStarted("First span", observationRegistry).observeChecked(() -> {
384+
then(getTracer().currentTraceContext().context()).isNotNull();
385+
386+
BaggageToPropagate baggageToPropagate = new BaggageToPropagate("tenant", "tenantValue", "tenant2",
387+
"tenant2Value");
388+
observationAwareBaggageThreadLocalAccessor.setValue(baggageToPropagate);
389+
TestObservationAwareBaggageThreadLocalAccessor
390+
.closeCurrentScope(observationAwareBaggageThreadLocalAccessor);
391+
});
392+
393+
then(getTracer().currentTraceContext().context()).isNull();
394+
}
395+
379396
private String asyncCall() {
380397
logWithSpan("TASK EXECUTOR");
381398
if (getTracer().currentSpan() == null) {

micrometer-tracing/src/main/java/io/micrometer/tracing/contextpropagation/ObservationAwareBaggageThreadLocalAccessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private static BaggageAndScope baggageScopeClosingScope(Entry<String, String> en
162162
if (scope == null) {
163163
return scopeClosingBaggageAndScope(entry, baggage);
164164
}
165-
return scope.andThen(scopeClosingBaggageAndScope(entry, baggage));
165+
return scopeClosingBaggageAndScope(entry, baggage).andThen(scope);
166166
}
167167

168168
private static BaggageAndScope scopeClosingBaggageAndScope(Entry<String, String> entry, BaggageInScope baggage) {
@@ -212,7 +212,7 @@ private BaggageAndScope scopeRestoringBaggageAndScope(BaggageAndScope currentSco
212212
});
213213
}
214214

215-
private void closeCurrentScope() {
215+
void closeCurrentScope() {
216216
BaggageAndScope scope = baggageInScope.get(Thread.currentThread());
217217
if (log.isTraceEnabled()) {
218218
log.trace("Before close scope [" + scope + "]");

0 commit comments

Comments
 (0)