spring-cloud / spring-cloud-sleuth

Distributed tracing for spring cloud

Home Page:https://spring.io/projects/spring-cloud-sleuth

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

traceid and spandid information lost after spring cloud 2021.0.6 update

sankarg931190 opened this issue · comments

Hi Team,

This is continuation for the issue #2270.
I am not sure, if the fix #2275 actually propagating the spanId to OnComplete (or OnError).
I tried to replicate with a simple Spring webflux project, and I can't see the traceId and spanId propagated during completion and during onError.
Here is the sample code for your reference,
My Pom has

org.springframework.boot : spring-boot-starter-parent : 2.7.12
org.springframework.boot : spring-boot-starter-webflux
org.springframework.boot : spring-boot-starter-test
org.springframework.cloud : spring-cloud-starter-sleuth : 3.1.8
io.projectreactor:reactor-test

Route class:

return RouterFunctions.route(GET("/helloWorld/{one}")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), helloWorldHandler::helloWorld);

And a handler class

public Mono helloWorld(ServerRequest request) {
String value = request.pathVariable("one");
if(value.equals("1")) {
throw new RuntimeException("path should contain some value");
}
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("Hello "+value));
}

If I run the application, and hit the following url "http://localhost:8080/helloWorld/1", here is the application log,

2023-05-23 18:11:41.911 TRACE [,,] 12100 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [219e5b8d-1] HTTP GET "/helloWorld/start", headers={masked}
2023-05-23 18:11:41.961 DEBUG [,9dffbac718e4a18b,9dffbac718e4a18b] 12100 --- [ctor-http-nio-2] o.s.w.r.f.s.s.RouterFunctionMapping : [219e5b8d-1] Mapped to com.traceid.traceidIssue.controller.HelloWorldRouter$$Lambda$488/0x0000000800456040@34b085f5
2023-05-23 18:11:41.998 DEBUG [,9dffbac718e4a18b,9dffbac718e4a18b] 12100 --- [ctor-http-nio-2] org.springframework.web.HttpLogging : [219e5b8d-1] Writing "Hello start"
2023-05-23 18:11:42.035 TRACE [,,] 12100 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [219e5b8d-1] Completed 200 OK, headers={masked}

The last log line is missing the traceId. This is a scenario of OnComplete.
Similarly, when run an error scenario , for example accessing "http://localhost:8080/helloWorld/1", here are the error logs.

2023-05-23 18:12:03.045 TRACE [,,] 12100 --- [ctor-http-nio-4] o.s.w.s.adapter.HttpWebHandlerAdapter : [e3f64717-2] HTTP GET "/helloWorld/1", headers={masked}
2023-05-23 18:12:03.046 DEBUG [,94da05e6b3c9fa90,94da05e6b3c9fa90] 12100 --- [ctor-http-nio-4] o.s.w.r.f.s.s.RouterFunctionMapping : [e3f64717-2] Mapped to com.traceid.traceidIssue.controller.HelloWorldRouter$$Lambda$488/0x0000000800456040@34b085f5
### 2023-05-23 18:12:03.070 DEBUG [,,] 12100 --- [ctor-http-nio-4] org.springframework.web.HttpLogging : [e3f64717-2] Resolved [RuntimeException: path should contain some value] for HTTP GET /helloWorld/1
2023-05-23 18:12:03.078 ERROR [,,] 12100 --- [ctor-http-nio-4] a.w.r.e.AbstractErrorWebExceptionHandler : [e3f64717-2] 500 Server Error for HTTP GET "/helloWorld/1"

java.lang.RuntimeException: path should contain some value
at com.traceid.traceidIssue.controller.HelloWorldHandler.helloWorld(HelloWorldHandler.java:17) ~[classes/:na]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
2023-05-23 18:12:03.080 DEBUG [,,] 12100 --- [ctor-http-nio-4] org.springframework.web.HttpLogging : [e3f64717-2] Writing
2023-05-23 18:12:03.083 TRACE [,,] 12100 --- [ctor-http-nio-4] o.s.w.s.adapter.HttpWebHandlerAdapter : [e3f64717-2] Completed 500 INTERNAL_SERVER_ERROR, headers={masked}

As you can see above (highlighted in Italics ), the traceId is missing, when the exception is thrown.

If I change the following in pom file and restart the application,
org.springframework.cloud : spring-cloud-starter-sleuth : 3.1.5

then for positive scenario, I could see the traceId propagated during onComplete. (refer the last line of below log)

2023-05-23 18:09:37.110 TRACE [,,] 19444 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [fd841721-5] HTTP GET "/helloWorld/test", headers={masked}
2023-05-23 18:09:37.111 DEBUG [,3eeeb7e23074ad47,3eeeb7e23074ad47] 19444 --- [ctor-http-nio-2] o.s.w.r.f.s.s.RouterFunctionMapping : [fd841721-5] Mapped to com.traceid.traceidIssue.controller.HelloWorldRouter$$Lambda$488/0x0000000800456040@21c4efeb
2023-05-23 18:09:37.111 DEBUG [,3eeeb7e23074ad47,3eeeb7e23074ad47] 19444 --- [ctor-http-nio-2] org.springframework.web.HttpLogging : [fd841721-5] Writing "Hello test"
2023-05-23 18:09:37.113 TRACE [,3eeeb7e23074ad47,3eeeb7e23074ad47] 19444 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [fd841721-5] Completed 200 OK, headers={masked}

Similarly for the error scenario,

2023-05-23 18:10:15.946 TRACE [,,] 19444 --- [ctor-http-nio-4] o.s.w.s.adapter.HttpWebHandlerAdapter : [d738a83e-6] HTTP GET "/helloWorld/1", headers={masked}
2023-05-23 18:10:15.946 DEBUG [,cafe29d9a35fd3bc,cafe29d9a35fd3bc] 19444 --- [ctor-http-nio-4] o.s.w.r.f.s.s.RouterFunctionMapping : [d738a83e-6] Mapped to com.traceid.traceidIssue.controller.HelloWorldRouter$$Lambda$488/0x0000000800456040@21c4efeb
_2023-05-23 18:10:15.948 DEBUG [,cafe29d9a35fd3bc,cafe29d9a35fd3bc] 19444 --- [ctor-http-nio-4] org.springframework.web.HttpLogging : [d738a83e-6] Resolved [RuntimeException: path should contain some value] for HTTP GET /helloWorld/1
2023-05-23 18:10:15.949 ERROR [,cafe29d9a35fd3bc,cafe29d9a35fd3bc] 19444 --- [ctor-http-nio-4] a.w.r.e.AbstractErrorWebExceptionHandler : [d738a83e-6] 500 Server Error for HTTP GET "/helloWorld/1"
java.lang.RuntimeException: path should contain some value
at com.traceid.traceidIssue.controller.HelloWorldHandler.helloWorld(HelloWorldHandler.java:17) ~[classes/:na]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:

2023-05-23 18:10:15.951 DEBUG [,cafe29d9a35fd3bc,cafe29d9a35fd3bc] 19444 --- [ctor-http-nio-4] org.springframework.web.HttpLogging : [d738a83e-6] Writing "
2023-05-23 18:10:15.952 TRACE [,cafe29d9a35fd3bc,cafe29d9a35fd3bc] 19444 --- [ctor-http-nio-4] o.s.w.s.adapter.HttpWebHandlerAdapter : [d738a83e-6] Completed 500 INTERNAL_SERVER_ERROR, headers={masked}_

Clearly, here as well, the traceId and spanId were propagated to OnError condition, while using spring-cloud-starter-sleuth:3.1.5

The concern here is, the feature which was working in version 3.1.5 is broken in subsequent versions (3.1.6 and above).

Appreciate, if you look into the issue and let us know if you require any further details.

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

Thanks @spencergibb for the feedback and pointing out the guidelines. Will use it for future reference.

Really looking forward to getting this fixed, as we have to stick with 2.7.x of spring boot until tracing is working with 3.x + spring webflux.

Waiting for update for this issue, @OlegDokuka @marcingrzejszczak

Please upgrade to Micrometer Tracing. Spring Cloud Sleuth is feature complete and out of OSS support.