Guarding context retreval; fixes gh-1789

This commit is contained in:
Marcin Grzejszczak
2020-12-04 11:46:14 +01:00
parent 5ad9108de9
commit 68497e0c51
2 changed files with 51 additions and 3 deletions

View File

@@ -87,13 +87,13 @@ public abstract class ReactorSleuth {
if (log.isTraceEnabled()) {
String message = "Spring Context [" + springContext
+ "] is not yet refreshed. This is unexpected. Reactor Context is ["
+ sub.currentContext() + "] and name is [" + name(sub) + "]";
+ context(sub) + "] and name is [" + name(sub) + "]";
log.trace(message);
}
return sub;
}
Context context = sub.currentContext();
Context context = context(sub);
if (log.isTraceEnabled()) {
log.trace("Spring context [" + springContext + "], Reactor context ["
@@ -108,7 +108,7 @@ public abstract class ReactorSleuth {
if (log.isTraceEnabled() || assertOn) {
String message = "Spring Context [" + springContext
+ "] did not return a CurrentTraceContext. Reactor Context is ["
+ sub.currentContext() + "] and name is [" + name(sub) + "]";
+ context(sub) + "] and name is [" + name(sub) + "]";
log.trace(message);
assert false : message; // should never happen, but don't break.
}
@@ -129,6 +129,18 @@ public abstract class ReactorSleuth {
});
}
private static <T> Context context(CoreSubscriber<? super T> sub) {
try {
return sub.currentContext();
}
catch (Exception ex) {
if (log.isDebugEnabled()) {
log.debug("Exception occurred while trying to retrieve the context", ex);
}
}
return Context.empty();
}
static String name(CoreSubscriber<?> sub) {
return Scannable.from(sub).name();
}

View File

@@ -113,6 +113,33 @@ public class ScopePassingSpanSubscriberTests {
}
};
Subscriber<Object> exceptionThrowingPassingSpanSubscriber = new CoreSubscriber<Object>() {
@Override
public void onSubscribe(Subscription s) {
}
@Override
public void onNext(Object o) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
@Override
public Context currentContext() {
throw new NullPointerException("Boom!");
}
};
AnnotationConfigApplicationContext springContext = new AnnotationConfigApplicationContext();
@Before
@@ -152,6 +179,15 @@ public class ScopePassingSpanSubscriberTests {
@Test
public void should_set_empty_context_when_context_is_null() {
ScopePassingSpanSubscriber<?> subscriber = new ScopePassingSpanSubscriber<>(
this.exceptionThrowingPassingSpanSubscriber, Context.empty(),
this.currentTraceContext, null);
then(subscriber.currentContext().isEmpty()).isTrue();
}
@Test
public void should_set_empty_context_when_exception_occurs_while_trying_to_retrieve_the_context() {
ScopePassingSpanSubscriber<?> subscriber = new ScopePassingSpanSubscriber<>(null,
Context.empty(), this.currentTraceContext, null);