-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Coroutine, Reactor 관련 MDC 설정 추가
- Loading branch information
Showing
10 changed files
with
118 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/kotlin/com/balancemania/api/config/reactor/MdcContextLifterConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.balancemania.api.config.reactor | ||
|
||
import com.balancemania.api.extension.copyToMdc | ||
import jakarta.annotation.PostConstruct | ||
import jakarta.annotation.PreDestroy | ||
import org.reactivestreams.Subscription | ||
import org.springframework.context.annotation.Configuration | ||
import reactor.core.CoreSubscriber | ||
import reactor.core.publisher.Hooks | ||
import reactor.core.publisher.Operators | ||
import reactor.util.context.Context | ||
|
||
/** | ||
* 리액터 쓰레드 변경시 MDC 값 전파하기 위해서 사용합니다. | ||
* @link https://www.novatec-gmbh.de/en/blog/how-can-the-mdc-context-be-used-in-the-reactive-spring-applications/ | ||
*/ | ||
@Configuration | ||
class MdcContextLifterConfiguration { | ||
companion object { | ||
val MDC_CONTEXT_REACTOR_KEY: String = MdcContextLifterConfiguration::class.java.name | ||
} | ||
|
||
@PostConstruct | ||
fun contextOperatorHook() { | ||
Hooks.onEachOperator( | ||
MDC_CONTEXT_REACTOR_KEY, | ||
Operators.lift { _, subscriber -> | ||
MdcContextLifter<Any?>(subscriber) | ||
} | ||
) | ||
} | ||
|
||
@PreDestroy | ||
fun cleanupHook() { | ||
Hooks.resetOnEachOperator(MDC_CONTEXT_REACTOR_KEY) | ||
} | ||
} | ||
|
||
/** | ||
* Helper that copies the state of Reactor [Context] to MDC on the #onNext function. | ||
*/ | ||
class MdcContextLifter<T>( | ||
private val coreSubscriber: CoreSubscriber<T>, | ||
) : CoreSubscriber<T> { | ||
override fun onSubscribe(subscription: Subscription) { | ||
coreSubscriber.onSubscribe(subscription) | ||
} | ||
|
||
override fun onNext(t: T) { | ||
coreSubscriber.currentContext().copyToMdc() | ||
coreSubscriber.onNext(t) | ||
} | ||
|
||
override fun onError(throwable: Throwable) { | ||
coreSubscriber.onError(throwable) | ||
} | ||
|
||
override fun onComplete() { | ||
coreSubscriber.onComplete() | ||
} | ||
|
||
override fun currentContext(): Context { | ||
return coreSubscriber.currentContext() | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/com/balancemania/api/extension/ContextExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.balancemania.api.extension | ||
|
||
import com.balancemania.api.common.MDC_KEY_TRACE_ID | ||
import org.slf4j.MDC | ||
import reactor.util.context.Context | ||
|
||
/** | ||
* Extension function for the Reactor [Context]. Copies the current context to the MDC, if context is empty clears the MDC. | ||
* State of the MDC after calling this method should be same as Reactor [Context] state. | ||
* One thread-local access only. | ||
*/ | ||
fun Context.copyToMdc() { | ||
if (!this.isEmpty) { | ||
val map = this.toMap() | ||
MDC.setContextMap(map) | ||
} else { | ||
MDC.clear() | ||
} | ||
} | ||
|
||
private fun Context.toMap(): Map<String, String> = this.stream() | ||
.map { ctx -> ctx.key.toString() to ctx.value.toString() } | ||
.toList().toMap() | ||
|
||
fun Context.insertMDC(): Context { | ||
val traceId = MDC.get(MDC_KEY_TRACE_ID) | ||
val ctxMap = this.toMap().toMutableMap() | ||
ctxMap[MDC_KEY_TRACE_ID] = traceId | ||
return Context.of(ctxMap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/balancemania/api/extension/ReactorExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.balancemania.api.extension | ||
|
||
import kotlinx.coroutines.reactive.awaitSingle | ||
import reactor.core.publisher.Mono | ||
|
||
suspend fun <T : Any> Mono<T>.awaitSingleWithMdc(): T { | ||
return this.contextWrite { it.insertMDC() } | ||
.awaitSingle() | ||
} |