https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Jakub Gwóźdź

10/09/2023, 6:36 AM
Hi there. I wonder if there is anything in Flow API that would allow passing some correlation_id in logger’s MDC along the items? Suppose I have my
fun emitter() = flow {... }
, some
.onEach {}
calls and
.collect()
at the end and I’d like to have consistent correlation_id in logs in MDC for that. Problem is, I cannot use
Copy code
private fun emitter() = flow {
        (1..3).forEach {
            withContext(MDCContext(mapOf("correlation_id" to UUID.randomUUID().toString()))) {
                logger.info { "Emitting $it" }
                emit(it)
                logger.info { "Emitted $it" }
            }
        }
    }
because, well, it’s rightfully forbidden to emit in different context than the collecting.
.flowOn(…)
also won’t solve the problem as it is one context per flow, not per item. Best I can do is to emit
Pair<UUID, Int>
instead of just
Int
and unpack it on each step, but maybe there is better solution in API?
m

Michael Strasser

10/09/2023, 11:18 AM
Hi @Jakub Gwóźdź I haven’t tried it with Flows, but maybe the Klogging library might work. Klogging works with Spring Boot and Ktor as a drop-in replacement for Logback for existing logging. After that you can use it directly in
suspend
functions to store context information like correlation IDs. You store context information in coroutine scope that is inherited by child scopes. (I am the creator of Klogging.)
t

taer

10/10/2023, 2:13 PM
We've run into the same thing. No solution yet. 🙂
j

Jakub Gwóźdź

10/10/2023, 2:15 PM
I ended up with passing the correlation id along the item, regular way 🙂
t

taer

10/10/2023, 2:20 PM
yeah. we kinda did the same. Increase the payload and manually set the MDC directly
if your operations are simple enough, setting the MDC direct works. problem is if you don't then you end up w/ sometimes old stale things. Manual was the only way we could pull it off
j

Jakub Gwóźdź

10/10/2023, 2:29 PM
Well, I don't set mdc directly, as it is lost during coroutines context switching. But there is mdccontext somewhere in coroutines api that takes care of it.
t

taer

10/10/2023, 4:28 PM
yeah.. I think you need to be slightly careful. The withContext using the MDCContext(mapOfThingsToSet) will only apply the MDC things given on a switch. I'm not sure it takes effect immediately.