Jakub Gwóźdź
10/09/2023, 6:36 AMfun 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?Michael Strasser
10/09/2023, 11:18 AMsuspend
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.)taer
10/10/2023, 2:13 PMJakub Gwóźdź
10/10/2023, 2:15 PMtaer
10/10/2023, 2:20 PMJakub Gwóźdź
10/10/2023, 2:29 PMtaer
10/10/2023, 4:28 PM