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

[Question] How to intercept suspend methods in Kotlin and provide a custom coroutine context? #1779

Open
slawa4s opened this issue Mar 9, 2025 · 1 comment
Assignees
Labels

Comments

@slawa4s
Copy link

slawa4s commented Mar 9, 2025

Hi! I need to intercept suspend methods and execute them within a custom coroutine context. I want to ensure that the method runs asynchronously with the custom context, while maintaining context propagation for OpenTelemetry (such as parent-child spans and tracing metadata).

I want something like this:

suspend fun intercept(originalMethod: Callable<*>, method: Method, args: Array<Any?>): Any? {
    val customContext = Dispatchers.IO  // Example of custom context
    return withContext(customContext) {
        // Call the original suspend method
        originalMethod.call()
    }
}
@raphw
Copy link
Owner

raphw commented Mar 10, 2025

I do not know how those get compiled, so the first thing you'd need to check would be to look into javap of a Kotlin class. From there, you can intercept the byte code as it is shown by javap.

@raphw raphw self-assigned this Mar 10, 2025
@raphw raphw added the question label Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants