Skip to content

Tracing propagation does not work in ControllerAdvice with async controllers since 3.2.7 #41266

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

Closed
raczynsm opened this issue Jun 28, 2024 · 6 comments
Assignees
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue

Comments

@raczynsm
Copy link

In my application I have async controllers and ControllerAdvice with exceptions handling.
Tracing propagation was working until I upgraded from 3.2.6 to 3.2.7.
I use below dependencies:

implementation("org.springframework.boot:spring-boot-starter-web")
implementation("io.micrometer:micrometer-tracing-bridge-brave")

This is minimal reproduction for the issue:

  • main class:
package com.example.tracing

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableAsync

@SpringBootApplication
@EnableAsync
class TracingApplication

fun main(args: Array<String>) {
	runApplication<TracingApplication>(*args)
}

  • async controller with controller advice:
package com.example.tracing

import io.micrometer.context.ContextExecutorService
import io.micrometer.context.ContextSnapshotFactory
import io.micrometer.tracing.Tracer
import org.springframework.context.annotation.Configuration
import org.springframework.http.ResponseEntity
import org.springframework.scheduling.annotation.Async
import org.springframework.scheduling.annotation.AsyncConfigurer
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.context.request.WebRequest
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors


@Controller
class WebController(
    val tracer : Tracer
) {

    @Async
    @GetMapping(path = ["/hello"], produces = ["text/plain"])
    @ResponseBody
    fun hello() : CompletableFuture<String> {
        val traceId = tracer.currentSpan()?.context()?.traceId()
        println("TRACING in Controller: $traceId")
        return CompletableFuture.completedFuture("Hello with [${traceId}]\n")
    }

    @Async
    @GetMapping(path = ["/fail"], produces = ["text/plain"])
    @ResponseBody
    fun fail() : CompletableFuture<String> {
        val traceId = tracer.currentSpan()?.context()?.traceId()
        println("TRACING in Controller: $traceId")
        throw IllegalStateException()
    }
}

@ControllerAdvice
class MyErrorHandler(
    val tracer : Tracer
) {

    @ExceptionHandler(value = [IllegalStateException::class])
    protected fun handleException(ex: RuntimeException, request: WebRequest): ResponseEntity<String> {
        val traceId = tracer.currentSpan()?.context()?.traceId()
        println("TRACING in ErrorHandler: $traceId")
        return ResponseEntity.ok("FAILURE with [$traceId]\n")
    }
}

@Configuration
internal class AsyncContextPropagationConfig : AsyncConfigurer {
    override fun getAsyncExecutor() = ContextExecutorService.wrap(executor) {
        contextSnapshotFactory.captureAll()
    }

    companion object {
        val executor: ExecutorService = Executors.newCachedThreadPool()
        val contextSnapshotFactory = ContextSnapshotFactory.builder()
            .build()
    }
}

Since 3.2.7 curl http://localhost:8080/fail returns:

FAILURE with [null]

before 3.2.7 there was a traceId:

FAILURE with [667e86d51406d20c273b9f4e8a9bc9a5]
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 28, 2024
@bclozel
Copy link
Member

bclozel commented Jun 28, 2024

Duplicates spring-projects/spring-framework#33091

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2024
@bclozel bclozel added status: duplicate A duplicate of another issue 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 Jun 28, 2024
@raczynsm
Copy link
Author

@bclozel I am not sure if this is exactly the same issue as the other one mentioned regression between 3.3.0 and 3.3.1 and in my case the regression is between 3.2.6 and 3.2.7

@wilkinsona
Copy link
Member

Spring Boot 3.2.7 and 3.3.1 both contain the same Spring Framework upgrade so they're both affected by spring-projects/spring-framework#32730.

@bclozel
Copy link
Member

bclozel commented Jun 28, 2024

Apologies if this was closed as a duplicate too quickly.
I assumed that this was a duplicate of spring-projects/spring-framework#33091 because both Spring Boot 3.2.7 and 3.3.1 upgraded to Spring Framework 6.1.9 (see #41019 and #41034).

Can you confirm you can reproduce the problem even after downgrading your application to Spring Framework 6.1.8?
You can use the following for Gradle:

ext['spring-framework.version'] = "6.1.8"

If you cannot reproduce the problem after the downgrade, this means that the problem is in Spring Framework directly and that it is most likely linked to spring-projects/spring-framework#32730.

@bclozel bclozel reopened this Jun 28, 2024
@bclozel bclozel added status: waiting-for-triage An issue we've not yet triaged and removed status: duplicate A duplicate of another issue for: external-project For an external project and not something we can fix labels Jun 28, 2024
@bclozel bclozel self-assigned this Jun 28, 2024
@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Jun 28, 2024
@raczynsm
Copy link
Author

I cannot reproduce after downgrading this dependency to implementation("org.springframework:spring-web:6.1.8")

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 28, 2024
@bclozel
Copy link
Member

bclozel commented Jun 28, 2024

Thanks for letting me know. I'll close this one as a duplicate then. Please follow spring-projects/spring-framework#33091 if you'd like to test the fix once it's in SNAPSHOTs.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2024
@bclozel bclozel added status: duplicate A duplicate of another issue 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 status: feedback-provided Feedback has been provided labels Jun 28, 2024
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: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants