-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds additional operations for working with MDC in suspending
and non-suspending contexts GitOrigin-RevId: 2f1a75b27aa07cf9508a61ae9e10f60370aa8e3d
- Loading branch information
1 parent
57778b6
commit b9426c8
Showing
12 changed files
with
173 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
package misk.logging | ||
|
||
typealias MdcContextMap = Map<String, String> | ||
|
||
interface Mdc { | ||
fun put(key: String, value: String?) | ||
|
||
fun get(key: String): String? | ||
|
||
fun clear() | ||
|
||
fun setContextMap(context: MdcContextMap) | ||
|
||
fun getCopyOfContextMap(): MdcContextMap? | ||
|
||
} | ||
|
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package misk.logging | ||
|
||
import org.slf4j.MDC | ||
|
||
/** | ||
* Adds the given key, value pair to the MDC for the duration of the block. | ||
*/ | ||
inline fun Mdc.withMdc(key: String, value: String, block: () -> Unit) = | ||
withMdc(key to value, block = block) | ||
|
||
/** | ||
* Adds the given tags to the MDC for the duration of the block. | ||
*/ | ||
inline fun Mdc.withMdc(vararg tags: Pair<String, String>, block: () -> Unit) { | ||
val oldState = getCopyOfContextMap() | ||
return try { | ||
tags.forEach { (key, value) -> put(key, value) } | ||
block() | ||
} finally { | ||
oldState?.let { setContextMap(it) } ?: clear() | ||
} | ||
} | ||
|
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,35 @@ | ||
package misk.logging.coroutines | ||
|
||
import kotlinx.coroutines.slf4j.MDCContext | ||
import kotlinx.coroutines.withContext | ||
import misk.logging.Mdc | ||
import mu.KotlinLogging | ||
import wisp.logging.getLogger | ||
import kotlin.coroutines.coroutineContext | ||
|
||
/** | ||
* Adds the given key, value pair to the MDC for the duration of the block. | ||
* This is coroutine safe, so the additions will be added to the coroutine context | ||
*/ | ||
suspend inline fun Mdc.withMdc(key: String, value: String, crossinline block: suspend () -> Unit) = | ||
withMdc(key to value, block = block) | ||
|
||
/** | ||
* Adds the given tags to the MDC for the duration of the block. | ||
* This is coroutine safe, so the additions will be added to the coroutine context | ||
*/ | ||
suspend inline fun Mdc.withMdc( | ||
vararg tags: Pair<String, String>, | ||
crossinline block: suspend () -> Unit | ||
) { | ||
if(coroutineContext[MDCContext] == null) { | ||
KotlinLogging.logger("misk.logging.coroutines.ScopedMdc").warn { | ||
"MDCContext is not present in the coroutine context, this is required to restore the previous MDC state" | ||
} | ||
} | ||
tags.forEach { (key, value) -> put(key, value) } | ||
return withContext(MDCContext()) { | ||
block() | ||
} | ||
} | ||
|
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
48 changes: 0 additions & 48 deletions
48
misk/src/test/kotlin/misk/logging/DynamicMdcContextTest.kt
This file was deleted.
Oops, something went wrong.
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,79 @@ | ||
package misk.logging | ||
|
||
import jakarta.inject.Inject | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.slf4j.MDCContext | ||
import kotlinx.coroutines.test.runTest | ||
import misk.MiskTestingServiceModule | ||
import misk.testing.MiskTest | ||
import misk.testing.MiskTestModule | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNull | ||
import misk.logging.coroutines.withMdc as withMdcCoroutines | ||
|
||
@MiskTest(startService = false) | ||
internal class ScopedMdcKtTest { | ||
@MiskTestModule | ||
val module = MiskTestingServiceModule() | ||
|
||
@Inject | ||
lateinit var mdc: Mdc | ||
|
||
@Test | ||
fun `test withMdc in a coroutine for key value pairs`() = runTest(MDCContext()) { | ||
val tags = (1..3).map { "key$it" to "value$it" }.toTypedArray() | ||
mdc.withMdcCoroutines(*tags) { | ||
tags.assertTags() | ||
delay(100) | ||
tags.assertTags() | ||
} | ||
tags.asserMissingTags() | ||
} | ||
|
||
@Test | ||
fun `test withMdc for key value pairs`() { | ||
val tags = (1..3).map { "key$it" to "value$it" }.toTypedArray() | ||
mdc.withMdc(*tags) { | ||
tags.assertTags() | ||
} | ||
tags.asserMissingTags() | ||
} | ||
|
||
@Test | ||
fun `test withMdc in a coroutine for key value pair overrides`() = runTest(MDCContext()) { | ||
val tags = (1..3).map { "key$it" to "value$it" }.toTypedArray() | ||
mdc.withMdcCoroutines(*tags) { | ||
tags.assertTags() | ||
delay(100) | ||
tags.assertTags() | ||
val updatedTags = tags.map { if (it.first == "key1"){it.first to it.second+"00"} else {it} }.toTypedArray() | ||
mdc.withMdcCoroutines(*updatedTags) { | ||
updatedTags.assertTags() | ||
delay(100) | ||
updatedTags.assertTags() | ||
} | ||
tags.forEach { it.asserTag() } | ||
} | ||
tags.asserMissingTags() | ||
} | ||
|
||
@Test | ||
fun `test withMdc for key value pair overrides`() { | ||
val tags = (1..3).map { "key$it" to "value$it" }.toTypedArray() | ||
mdc.withMdc(*tags) { | ||
tags.assertTags() | ||
val updatedTags = tags.map { if (it.first == "key1"){it.first to it.second+"00"} else {it} }.toTypedArray() | ||
mdc.withMdc(*updatedTags) { | ||
updatedTags.assertTags() | ||
} | ||
tags.forEach { it.asserTag() } | ||
} | ||
tags.asserMissingTags() | ||
} | ||
|
||
fun Pair<String, String>.asserTag() = assertEquals(second, mdc.get(first)) | ||
fun Array<Pair<String, String>>.assertTags() = forEach { it.asserTag() } | ||
fun Pair<String, String>.asserMissingTag() = assertNull( mdc.get(first)) | ||
fun Array<Pair<String, String>>.asserMissingTags() = forEach { it.asserMissingTag() } | ||
} |