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
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?