spring-cloud / spring-cloud-sleuth

Distributed tracing for spring cloud
https://spring.io/projects/spring-cloud-sleuth
Apache License 2.0
1.78k stars 782 forks source link

spring boot webflux webclient flatmap npe #1231

Closed creatorKoo closed 5 years ago

creatorKoo commented 5 years ago

example code below:

return serviceClient
                .post()
                .body(BodyInserters.fromObject(request))
                .exchange()
                .flatMap(clientResponse -> clientResponse.bodyToMono(Response.class))
;

I think flatmap change thread context and npe exception. (i test this code on spring cloud gateway)

npe

java.lang.NullPointerException: null
    at org.springframework.cloud.sleuth.instrument.web.client.HttpClientBeanPostProcessor$AbstractTracingDoOnHandler.handle(TraceWebClientAutoConfiguration.java:483)
    at org.springframework.cloud.sleuth.instrument.web.client.HttpClientBeanPostProcessor$TracingDoOnErrorRequest.accept(TraceWebClientAutoConfiguration.java:447)
    at org.springframework.cloud.sleuth.instrument.web.client.HttpClientBeanPostProcessor$TracingDoOnErrorRequest.accept(TraceWebClientAutoConfiguration.java:434)
    at reactor.netty.http.client.HttpClientDoOnError$OnErrorTcpClient.lambda$connect$0(HttpClientDoOnError.java:200)
    at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:190)
    at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2070)
    at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.request(FluxPeekFuseable.java:138)
    at org.springframework.cloud.sleuth.instrument.reactor.ScopePassingSpanSubscriber.request(ScopePassingSpanSubscriber.java:80)
    at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreInner.onSubscribe(MonoIgnoreThen.java:218)
    at org.springframework.cloud.sleuth.instrument.reactor.ScopePassingSpanSubscriber.onSubscribe(ScopePassingSpanSubscriber.java:72)
    at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onSubscribe(FluxPeekFuseable.java:172)
    at reactor.core.publisher.MonoCurrentContext.subscribe(MonoCurrentContext.java:35)
    at reactor.core.publisher.MonoPeekFuseable.subscribe(MonoPeekFuseable.java:74)
    at reactor.core.publisher.MonoLiftFuseable.subscribe(MonoLiftFuseable.java:55)
    at reactor.core.publisher.Mono.subscribe(Mono.java:3694)
    at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:172)
    at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
    at reactor.core.publisher.MonoLiftFuseable.subscribe(MonoLiftFuseable.java:55)
    at reactor.core.publisher.Mono.subscribe(Mono.java:3694)
    at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:97)
    at reactor.core.publisher.FluxContextStart$ContextStartSubscriber.onError(FluxContextStart.java:117)
    at reactor.core.publisher.FluxRetryPredicate$RetryPredicateSubscriber.onError(FluxRetryPredicate.java:100)
    at org.springframework.cloud.sleuth.instrument.reactor.ScopePassingSpanSubscriber.onError(ScopePassingSpanSubscriber.java:105)
    at reactor.core.publisher.MonoCreate$DefaultMonoSink.error(MonoCreate.java:167)
    at reactor.netty.http.client.HttpClientConnect$HttpObserver.onUncaughtException(HttpClientConnect.java:400)
    at reactor.netty.ReactorNetty$CompositeConnectionObserver.onUncaughtException(ReactorNetty.java:351)
    at reactor.netty.resources.PooledConnectionProvider$DisposableAcquire.onUncaughtException(PooledConnectionProvider.java:506)
    at reactor.netty.resources.PooledConnectionProvider$PooledConnection.onUncaughtException(PooledConnectionProvider.java:415)
    at reactor.netty.channel.FluxReceive.drainReceiver(FluxReceive.java:170)
    at reactor.netty.channel.FluxReceive.onInboundError(FluxReceive.java:358)
    at reactor.netty.channel.ChannelOperations.onInboundError(ChannelOperations.java:398)
    at reactor.netty.channel.ChannelOperationsHandler.exceptionCaught(ChannelOperationsHandler.java:185)
    at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:285)
    at io.netty.channel.AbstractChannelHandlerContext.notifyHandlerException(AbstractChannelHandlerContext.java:856)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:215)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:199)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:192)
    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelActive(CombinedChannelDuplexHandler.java:414)
    at io.netty.channel.ChannelInboundHandlerAdapter.channelActive(ChannelInboundHandlerAdapter.java:64)
    at io.netty.channel.CombinedChannelDuplexHandler.channelActive(CombinedChannelDuplexHandler.java:213)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:213)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:199)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:192)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelActive(DefaultChannelPipeline.java:1396)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:213)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:199)
    at io.netty.channel.DefaultChannelPipeline.fireChannelActive(DefaultChannelPipeline.java:906)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:311)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:341)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:665)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:612)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:529)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:491)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:905)
    at java.lang.Thread.run(Thread.java:748)
marcingrzejszczak commented 5 years ago

Which version of Sleuth are you using?

creatorKoo commented 5 years ago

2.1.1.RELEASE

creatorKoo commented 5 years ago

my gradle dependencies below

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-webflux'
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-ribbon'
    compile 'org.springframework.cloud:spring-cloud-starter-sleuth:2.1.1.RELEASE'
    compile 'org.springframework.cloud:spring-cloud-starter-gateway'
    compile 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery'
    compile 'org.bitbucket.b_c:jose4j:0.6.5'
    compileOnly 'org.projectlombok:lombok'

    testCompileOnly 'org.projectlombok:lombok'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'io.projectreactor:reactor-test'
}
marcingrzejszczak commented 5 years ago

Why do you set that dependency manually? Can't you use the Greenwich.SR1 train?

marcingrzejszczak commented 5 years ago

I think you're having some classpath issues cause in the latest version that should have been already fixed via https://github.com/spring-cloud/spring-cloud-sleuth/issues/1224

creatorKoo commented 5 years ago

wow. I forgot Greenwich.SR1 update. very thank you for your help.