Skip to content

Commit 44e4a2d

Browse files
committed
Merge pull request spring-cloud#30 from spring-cloud/threadlocal-remove
Use ThreadLocal.remove() rather than .set(null)
2 parents 8841d41 + b1537f3 commit 44e4a2d

File tree

9 files changed

+45
-8
lines changed

9 files changed

+45
-8
lines changed

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/TraceContextHolder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import lombok.extern.apachecommons.CommonsLog;
2020
import org.springframework.core.NamedThreadLocal;
21+
import org.springframework.util.Assert;
2122

2223
/**
2324
* @author Spencer Gibb
@@ -31,12 +32,17 @@ public static Span getCurrentSpan() {
3132
}
3233

3334
public static void setCurrentSpan(Span span) {
35+
Assert.notNull(span, "span can not be null. Use removeCurrentSpan() instead.");
3436
if (log.isTraceEnabled()) {
3537
log.trace("Setting current span " + span);
3638
}
3739
currentSpan.set(span);
3840
}
3941

42+
public static void removeCurrentSpan() {
43+
currentSpan.remove();
44+
}
45+
4046
public static boolean isTracing() {
4147
return currentSpan.get() != null;
4248
}

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/integration/TraceChannelInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
5858
if (traceScope != null) {
5959
traceScope.close();
6060
}
61-
this.traceScopeHolder.set(null);
61+
this.traceScopeHolder.remove();
6262
// TODO: Maybe the TraceScope could handle this
6363
TraceContextHolder.setCurrentSpan(this.spanHolder.get());
6464
}

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/integration/TraceContextPropagationChannelInterceptor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
2323
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
2424
import static org.springframework.cloud.sleuth.TraceContextHolder.getCurrentSpan;
25+
import static org.springframework.cloud.sleuth.TraceContextHolder.removeCurrentSpan;
2526
import static org.springframework.cloud.sleuth.TraceContextHolder.setCurrentSpan;
2627

2728
import java.util.HashMap;
@@ -113,15 +114,15 @@ protected void resetPropagatedContext() {
113114
Span originalContext = ORIGINAL_CONTEXT.get();
114115
try {
115116
if (originalContext == null) {
116-
setCurrentSpan(null);
117+
removeCurrentSpan();
117118
ORIGINAL_CONTEXT.remove();
118119
}
119120
else {
120121
setCurrentSpan(originalContext);
121122
}
122123
}
123124
catch (Throwable t) {// NOSONAR
124-
setCurrentSpan(null);
125+
removeCurrentSpan();
125126
}
126127
}
127128

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ else if (skip) {
146146
addResponseAnnotations(response);
147147
traceScope.close();
148148
}
149-
TraceContextHolder.setCurrentSpan(null);
149+
TraceContextHolder.removeCurrentSpan();
150150
}
151151
}
152152

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2013-2015 the original author or authors.
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+
* http://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+
17+
package org.springframework.cloud.sleuth;
18+
19+
import org.junit.Test;
20+
21+
/**
22+
* @author Spencer Gibb
23+
*/
24+
public class TraceContextHolderTests {
25+
26+
@Test(expected = IllegalArgumentException.class)
27+
public void setCurrentSpanNotNull() {
28+
TraceContextHolder.setCurrentSpan(null);
29+
}
30+
}

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/integration/TraceChannelInterceptorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void init() {
7676

7777
@After
7878
public void close() {
79-
TraceContextHolder.setCurrentSpan(null);
79+
TraceContextHolder.removeCurrentSpan();
8080
this.channel.unsubscribe(this);
8181
}
8282

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/integration/TraceContextPropagationChannelInterceptorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class TraceContextPropagationChannelInterceptorTests {
6161

6262
@After
6363
public void close() {
64-
TraceContextHolder.setCurrentSpan(null);
64+
TraceContextHolder.removeCurrentSpan();
6565
}
6666

6767
@Test

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class TraceFilterIntegrationTests {
5555
@Before
5656
@SneakyThrows
5757
public void init() {
58-
TraceContextHolder.setCurrentSpan(null);
58+
TraceContextHolder.removeCurrentSpan();
5959
this.context.refresh();
6060
this.request = builder().buildRequest(new MockServletContext());
6161
this.response = new MockHttpServletResponse();

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/TraceRestTemplateInterceptorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void setup() {
6060

6161
@After
6262
public void clean() {
63-
TraceContextHolder.setCurrentSpan(null);
63+
TraceContextHolder.removeCurrentSpan();
6464
}
6565

6666
@Test

0 commit comments

Comments
 (0)