Reduces scope copies when desired context is already current (#1558)

This commit is contained in:
Adrian Cole
2020-02-13 14:56:47 -08:00
committed by GitHub
parent 707abda92a
commit 47416a8eef
2 changed files with 15 additions and 1 deletions

View File

@@ -54,7 +54,9 @@ final class ScopePassingSpanSubscriber<T> implements SpanSubscription<T>, Scanna
this.subscriber = subscriber;
this.currentTraceContext = currentTraceContext;
this.parent = parent;
this.context = parent != null ? ctx.put(TraceContext.class, parent) : ctx;
this.context = parent != null
&& !parent.equals(ctx.getOrDefault(TraceContext.class, null))
? ctx.put(TraceContext.class, parent) : ctx;
if (log.isTraceEnabled()) {
log.trace("Parent span [" + parent + "], context [" + this.context + "]");
}

View File

@@ -138,6 +138,18 @@ public class ScopePassingSpanSubscriberTests {
then((String) subscriber.currentContext().get("foo")).isEqualTo("bar");
}
/**
* This ensures when the desired context is in the reactor context we don't copy it.
*/
@Test
public void should_not_redundantly_copy_context() {
Context initial = Context.of(TraceContext.class, context);
ScopePassingSpanSubscriber<?> subscriber = new ScopePassingSpanSubscriber<>(null,
initial, this.currentTraceContext, context);
then(initial).isSameAs(subscriber.currentContext());
}
@Test
public void should_set_empty_context_when_context_is_null() {
ScopePassingSpanSubscriber<?> subscriber = new ScopePassingSpanSubscriber<>(null,