From e8dfe1ae02c1454a609848026d13fbf40799f81d Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 9 Mar 2018 11:36:53 +0100 Subject: [PATCH] Reusing reactor context for span continuing subscriber without this change we were not reusing a span context if there was no previous span. Instead we were overwrriting it with an empty one with this change, if the context is there we are reusing it instead of overwriting it fixes gh-897 --- .../reactor/ScopePassingSpanSubscriber.java | 4 +- .../ScopePassingSpanSubscriberTests.java | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/reactor/ScopePassingSpanSubscriberTests.java diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/reactor/ScopePassingSpanSubscriber.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/reactor/ScopePassingSpanSubscriber.java index ac56b67b0..9561b6545 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/reactor/ScopePassingSpanSubscriber.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/reactor/ScopePassingSpanSubscriber.java @@ -46,8 +46,8 @@ final class ScopePassingSpanSubscriber extends AtomicBoolean implements SpanS Span root = ctx != null ? ctx.getOrDefault(Span.class, this.tracer.currentSpan()) : null; this.span = root; - this.context = root != null ? - ctx.put(Span.class, root): Context.empty(); + this.context = ctx != null && root != null ? ctx.put(Span.class, root) : + ctx != null ? ctx : Context.empty(); } @Override public void onSubscribe(Subscription subscription) { diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/reactor/ScopePassingSpanSubscriberTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/reactor/ScopePassingSpanSubscriberTests.java new file mode 100644 index 000000000..eef935906 --- /dev/null +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/reactor/ScopePassingSpanSubscriberTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.sleuth.instrument.reactor; + +import brave.Span; +import brave.Tracer; +import brave.Tracing; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +import reactor.util.context.Context; + +import static org.assertj.core.api.BDDAssertions.then; + +/** + * @author Marcin Grzejszczak + */ +@RunWith(MockitoJUnitRunner.class) +public class ScopePassingSpanSubscriberTests { + + Tracing tracing = Tracing.newBuilder().build(); + + @Test + public void should_propagate_current_context() { + ScopePassingSpanSubscriber subscriber = new ScopePassingSpanSubscriber( + null, Context.of("foo", "bar"), this.tracing); + + then((String) subscriber.currentContext().get("foo")).isEqualTo("bar"); + } + + @Test + public void should_set_empty_context_when_context_is_null() { + ScopePassingSpanSubscriber subscriber = new ScopePassingSpanSubscriber( + null, null, this.tracing); + + then(subscriber.currentContext().isEmpty()).isTrue(); + } + + @Test + public void should_put_current_span_to_context() { + Span span = this.tracing.tracer().nextSpan(); + try (Tracer.SpanInScope ws = this.tracing.tracer().withSpanInScope(span.start())) { + ScopePassingSpanSubscriber subscriber = new ScopePassingSpanSubscriber( + null, Context.empty(), this.tracing); + + then(subscriber.currentContext().get(Span.class)).isEqualTo(span); + } + + } +} \ No newline at end of file