I have a flow of "messages" each message has an ID...
# coroutines
t
I have a flow of "messages" each message has an ID. I'd love to set an MDCContext with that ID, so that logs will have the ID in it.. I currently collect the message at the end and set the MDC there, and it's working exactly as I desire. The problem is all the mutations earlier on in the flow(map, transform, etc) happen outside the context. One ugly solution would be to put the withContext in all the
map
calls, but that's ugly. flowOn smells like it's the right thing, but it doesn't get "each message". I tried this:
Copy code
val mdcFlowOfMessages = messageFlow.transform {
            withContext(MDCContext(mapOf("sqs-message-id" to it.messageId()))) {
                emit(it)
            }
        }
and it failed like it should - Flow invariant is violated - because I'm emmitting from a different context. Any suggestions on ways to accomplish this? For now, my 2 different
map
calls are extracted to a function that does a
withContext
at the start. I know I could probably use a channel flow for the above code, but that smells heavy handed.