feat: ログのMDCをkotlinx.coroutineに対応

This commit is contained in:
usbharu 2023-10-24 20:05:53 +09:00
parent 63c15b35d1
commit a98af0185c
3 changed files with 7 additions and 4 deletions

View File

@ -137,6 +137,7 @@ dependencies {
implementation("software.amazon.awssdk:s3:2.20.157")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:1.7.3")
implementation("dev.usbharu:http-signature:1.0.0")
implementation("io.ktor:ktor-client-logging-jvm:$ktor_version")

View File

@ -29,6 +29,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.slf4j.MDCContext
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
@ -43,8 +44,8 @@ interface APNoteService {
@Cacheable("fetchNote")
fun fetchNoteAsync(url: String, targetActor: String? = null): Deferred<Note> {
return CoroutineScope(Dispatchers.IO).async {
newSuspendedTransaction {
return CoroutineScope(Dispatchers.IO + MDCContext()).async {
newSuspendedTransaction(MDCContext()) {
fetchNote(url, targetActor)
}
}

View File

@ -1,18 +1,19 @@
package dev.usbharu.hideout.service.core
import kotlinx.coroutines.slf4j.MDCContext
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.springframework.stereotype.Service
@Service
class ExposedTransaction : Transaction {
override suspend fun <T> transaction(block: suspend () -> T): T {
return newSuspendedTransaction {
return newSuspendedTransaction(MDCContext()) {
block()
}
}
override suspend fun <T> transaction(transactionLevel: Int, block: suspend () -> T): T {
return newSuspendedTransaction(transactionIsolation = transactionLevel) {
return newSuspendedTransaction(MDCContext(), transactionIsolation = transactionLevel) {
block()
}
}