Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringBoot3+Micrometer: Missing traceId when handling exception #36353

Closed
gecan123 opened this issue Jul 12, 2023 · 1 comment
Closed

SpringBoot3+Micrometer: Missing traceId when handling exception #36353

gecan123 opened this issue Jul 12, 2023 · 1 comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid

Comments

@gecan123
Copy link

Context:
springboot version: 3.0.0 and 3.1.1
I upgraded my projected to springboot 3 and use micrometer as traceability. In most flows, the traceId is attached in MDC. But at some exception handling scenarios, the traceId is missing. Also the same test scenario use at spirngboot2 + sleuth, the traceId remains.

my configs and code:
(BTW i upload my test project with unit test at https://github.com/gecan123/tracing-test, you may click in my project and see all error scenarios in unit test. Also the test report is in github actions artifact, please read README.md)

**build.gradle**
plugins {
	id 'java'
	id 'org.springframework.boot' version '3.1.1'
	id 'io.spring.dependency-management' version '1.1.0'
}
dependencies {

	implementation 'org.springframework.boot:spring-boot-starter-actuator'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-aop'
	implementation 'io.micrometer:micrometer-observation'
	implementation 'io.micrometer:micrometer-tracing-bridge-brave'
	implementation 'io.zipkin.reporter2:zipkin-reporter-brave'

	testImplementation 'io.micrometer:micrometer-tracing-test'
	testImplementation 'io.micrometer:micrometer-test'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.restdocs:spring-restdocs-restassured'

}

**application.properties**
spring.application.name=spring-micrometer-tracing-demo
spring.jackson.serialization.indent_output = true
logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG
logging.pattern.level=%5p [${spring.zipkin.service.name:${spring.application.name:}},%X{traceId:-},%X{spanId:-}]
management.tracing.sampling.probability=1.0
@RestController
public class GlobalErrorController implements ErrorController {

    private static Logger log = LoggerFactory.getLogger(GlobalErrorController.class);

    @RequestMapping(value = "/error", produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<ApiResponse> error(HttpServletRequest request) {
        log.error("Exception occurred. apiError: {}", MDC.get("traceId"));
        return status(500).body(new ApiResponse());
    }
}

public class ApiResponse {

    private String traceId;

    public ApiResponse() {
        this.traceId = MDC.get("traceId");
    }
}

when i call /error endpoint directly, i get response with traceId.
when i call /path-not-exist and request will redirect to /error endpoint, but response without traceId.
Then i put more scenarios in my repo:

  • If a endpoint throw exception and redirect to /error, response without traceId.
  • If a endpoint only support POST, i call with get, redirect to /error, response without traceId.

I have found the issue may raised by the class in spring is ServerHttpObservationFilter, but no idea why it run another time after exception is thrown.

Thank you and look forward to your reply!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 12, 2023
@bclozel
Copy link
Member

bclozel commented Jul 12, 2023

This is a duplicate of #36285 and this limitation is explained in spring-projects/spring-framework#30675 and has been documented in spring-projects/spring-framework#29398.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2023
@bclozel bclozel added status: invalid An issue that we don't feel is valid for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants