Skip to content

Commit

Permalink
v2.0.0-RC0
Browse files Browse the repository at this point in the history
  • Loading branch information
Numichi committed Feb 16, 2022
1 parent cec06c1 commit 58995b7
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 77 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=2.0.0-ALPHA
version=2.0.0-RC0
group=io.github.numichi
developerId=numichi
developerName=Donát Csongor
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/io/github/numichi/reactive/logger/ICore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.numichi.reactive.logger

import io.github.numichi.reactive.logger.coroutine.readMDCResult
import io.github.numichi.reactive.logger.exception.ContextNotExistException
import io.github.numichi.reactive.logger.exception.InvalidContextDataException
import io.github.numichi.reactive.logger.reactor.MDCSnapshot
import reactor.core.scheduler.Scheduler
import reactor.util.context.ContextView
import java.util.*

interface ICore {
val isEnableError: Boolean
val mdcContextKey: String
val scheduler: Scheduler
val name: String

fun readMDC(context: ContextView): Optional<Map<String, String>> {
return Optional.ofNullable(readMDCResult(context, mdcContextKey).getOrNull())
}

@Throws(ContextNotExistException::class, InvalidContextDataException::class)
fun takeMDCSnapshot(context: ContextView): MDCSnapshot {
val result = readMDCResult(context, mdcContextKey)

return when {
result.isSuccess -> MDCSnapshot.of(result.getOrNull())
result.isFailure && !isEnableError -> MDCSnapshot.empty()
else -> throw result.exceptionOrNull()!!
}
}
}
29 changes: 0 additions & 29 deletions src/main/java/io/github/numichi/reactive/logger/abstracts/ICore.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package io.github.numichi.reactive.logger.abstracts
package io.github.numichi.reactive.logger.coroutine

import io.github.numichi.reactive.logger.coroutine.CCElement
import io.github.numichi.reactive.logger.coroutine.CCKey
import io.github.numichi.reactive.logger.coroutine.CCResolveFn
import io.github.numichi.reactive.logger.coroutine.ICoroutineCore
import io.github.numichi.reactive.logger.reactor.IReactorLogger
import org.slf4j.Logger
import reactor.core.scheduler.Scheduler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.numichi.reactive.logger.coroutine

import io.github.numichi.reactive.logger.DefaultValues
import io.github.numichi.reactive.logger.abstracts.ACoroutine
import io.github.numichi.reactive.logger.reactor.IReactorKLogger
import io.github.numichi.reactive.logger.reactor.ReactiveKLogger
import io.github.numichi.reactive.logger.reactor.ReactiveLogger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.numichi.reactive.logger.coroutine

import io.github.numichi.reactive.logger.DefaultValues
import io.github.numichi.reactive.logger.abstracts.ACoroutine
import io.github.numichi.reactive.logger.reactor.IReactorLogger
import io.github.numichi.reactive.logger.reactor.ReactiveLogger
import kotlinx.coroutines.reactor.ReactorContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package io.github.numichi.reactive.logger.coroutine

import io.github.numichi.reactive.logger.MDC
import io.github.numichi.reactive.logger.abstracts.ICore
import io.github.numichi.reactive.logger.ICore
import io.github.numichi.reactive.logger.exception.ContextNotExistException
import io.github.numichi.reactive.logger.exception.InvalidContextDataException
import io.github.numichi.reactive.logger.reactor.IReactorLogger
import kotlinx.coroutines.reactor.ReactorContext
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import reactor.core.publisher.Mono
import reactor.util.context.Context
import reactor.util.context.ContextView
import kotlin.coroutines.coroutineContext

interface ICoroutineCore<T : IReactorLogger> : ICore {
Expand All @@ -18,7 +21,8 @@ interface ICoroutineCore<T : IReactorLogger> : ICore {
override val name: String
get() = reactorLogger.name

suspend fun snapshot(context: Context? = null): MDC? {
@Throws(ContextNotExistException::class, InvalidContextDataException::class)
suspend fun snapshot(context: ContextView? = null): MDC? {
val ctx = context ?: coroutineContext[ReactorContext]?.context
return ctx?.let { reactorLogger.snapshot(it).awaitSingleOrNull() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,59 @@ package io.github.numichi.reactive.logger.coroutine

import io.github.numichi.reactive.logger.DefaultValues
import io.github.numichi.reactive.logger.MDC
import io.github.numichi.reactive.logger.exception.ContextNotExistException
import io.github.numichi.reactive.logger.exception.InvalidContextDataException
import reactor.util.context.ContextView

suspend fun readMDC(): MDC {
return readMDC(DefaultValues.getInstance().defaultReactorContextMdcKey)
@Throws(ContextNotExistException::class, InvalidContextDataException::class)
suspend fun readMdc(): MDC {
return readMdc(DefaultValues.getInstance().defaultReactorContextMdcKey)
}

suspend fun readMDC(mdcContextKey: String): MDC {
return readMDC(rectorContext(), mdcContextKey)
@Throws(ContextNotExistException::class, InvalidContextDataException::class)
suspend fun readMdc(mdcContextKey: String): MDC {
return readMdc(rectorContext(), mdcContextKey)
}

fun readMDC(contextView: ContextView?): MDC {
return readMDC(contextView, DefaultValues.getInstance().defaultReactorContextMdcKey)
@Throws(ContextNotExistException::class, InvalidContextDataException::class)
fun readMdc(contextView: ContextView?): MDC {
return readMdc(contextView, DefaultValues.getInstance().defaultReactorContextMdcKey)
}

fun readMDC(contextView: ContextView?, mdcContextKey: String): MDC {
@Throws(ContextNotExistException::class, InvalidContextDataException::class)
fun readMdc(contextView: ContextView?, mdcContextKey: String): MDC {
requireNotNull(contextView) { "contentView must not be null" }

val mdc = MDC(mdcContextKey)

try {
val map: Map<String, String> = contextView.get(mdcContextKey)
mdc.putAll(map)
} catch (noSuchException: NoSuchElementException) {
throw ContextNotExistException("\"$mdcContextKey\" context not found", noSuchException)
} catch (exception: Exception) {
throw InvalidContextDataException(exception)
}

return mdc
}

suspend fun readMdcOrNull(): MDC? {
return readMdcOrNull(DefaultValues.getInstance().defaultReactorContextMdcKey)
}

suspend fun readMdcOrNull(mdcContextKey: String): MDC? {
return readMdcOrNull(rectorContext(), mdcContextKey)
}

fun readMdcOrNull(contextView: ContextView?): MDC? {
return readMdcOrNull(contextView, DefaultValues.getInstance().defaultReactorContextMdcKey)
}

fun readMdcOrNull(contextView: ContextView?, mdcContextKey: String): MDC? {
return readMDCResult(contextView, mdcContextKey).getOrNull()
}

fun readMDCResult(contextView: ContextView?, mdcContextKey: String): Result<MDC> {
return runCatching { readMdc(contextView, mdcContextKey) }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.github.numichi.reactive.logger.exception;

class ContextNotExistException(message: String) : RuntimeException(message)
class ContextNotExistException(message: String, throwable: Throwable) : RuntimeException(message, throwable)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.numichi.reactive.logger.abstracts
package io.github.numichi.reactive.logger.reactor

import io.github.numichi.reactive.logger.reactor.IReactorCore
import org.slf4j.Logger
import reactor.core.scheduler.Scheduler

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.numichi.reactive.logger.reactor

import io.github.numichi.reactive.logger.MDC
import io.github.numichi.reactive.logger.abstracts.ICore
import io.github.numichi.reactive.logger.ICore
import io.github.numichi.reactive.logger.exception.ContextNotExistException
import org.slf4j.Logger
import reactor.core.publisher.Mono
Expand All @@ -14,7 +14,7 @@ interface IReactorCore : ICore {
override val name: String
get() = logger.name

fun snapshot(context: Context): Mono<MDC> {
fun snapshot(context: ContextView): Mono<MDC> {
return try {
var mdc: MDC
takeMDCSnapshot(context).use {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.numichi.reactive.logger.reactor

import io.github.numichi.reactive.logger.DefaultValues
import io.github.numichi.reactive.logger.abstracts.AReactive
import mu.KLogger
import mu.KotlinLogging
import org.slf4j.LoggerFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.numichi.reactive.logger.reactor

import io.github.numichi.reactive.logger.DefaultValues
import io.github.numichi.reactive.logger.abstracts.AReactive
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import reactor.core.scheduler.Scheduler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package io.github.numichi.reactive.logger.coroutine

import io.github.numichi.reactive.logger.MDC
import io.github.numichi.reactive.logger.exception.ContextNotExistException
import io.github.numichi.reactive.logger.exception.InvalidContextDataException
import io.github.numichi.reactive.logger.reactor.MDCContext
import io.github.numichi.reactive.logger.reactor.MDCSnapshot
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.reactor.ReactorContext
import kotlinx.coroutines.reactor.mono
import kotlinx.coroutines.test.runTest
import mu.KLogger
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertSame
import org.junit.jupiter.api.Test
import org.slf4j.Logger
import reactor.core.publisher.Mono
import reactor.core.scheduler.Schedulers
import reactor.test.StepVerifier
import kotlin.math.log

@ExperimentalCoroutinesApi
internal class CoroutineOtherTest {


@Test
fun `test11`() = runTest {
fun `configuration with chain`() = runTest {
val mockKLogger: KLogger = mockk(relaxed = true)
val loggerK = CoroutineKLogger.reactorBuilder()
.withContextKey(ReactorContext)
Expand Down Expand Up @@ -49,7 +57,7 @@ internal class CoroutineOtherTest {
}

@Test
fun `test12`() = runTest {
fun `configuration with attributes`() = runTest {
val mockKLogger: KLogger = mockk(relaxed = true)
val kBuilder = CoroutineKLogger.reactorBuilder()
kBuilder.contextKey = ReactorContext
Expand Down Expand Up @@ -92,4 +100,99 @@ internal class CoroutineOtherTest {
assertSame(Schedulers.boundedElastic(), loggerL.scheduler)
assertEquals(mockLogger, loggerL.logger)
}

@Test
fun `should get MDC data from snapshot (KLogger)`() {
val logger = CoroutineKLogger.reactorBuilder()
.withLogger(mockk(relaxed = true))
.build()

val x = mono { logger.snapshot()?.get("foo") }
.contextWrite {
val mdc = MDC()
mdc["foo"] = "bar"
MDCContext.put(it, mdc)
}

StepVerifier.create(x)
.expectNext("bar")
.verifyComplete()

}

@Test
fun `should get MDC data from snapshot (Logger)`() {
val logger = CoroutineLogger.reactorBuilder()
.withLogger(mockk(relaxed = true))
.build()

val x = mono { logger.snapshot()?.get("foo") }
.contextWrite {
val mdc = MDC()
mdc["foo"] = "bar"
MDCContext.put(it, mdc)
}

StepVerifier.create(x)
.expectNext("bar")
.verifyComplete()

}

@Test
fun `snapshot and direct export matching`() {
val logger = CoroutineLogger.reactorBuilder()
.withLogger(mockk(relaxed = true))
.build()

val mdcContextKey = logger.mdcContextKey;

val mdc = MDC()
mdc["foo"] = "bar"

val x = mono { logger.snapshot() }
.contextWrite { MDCContext.put(it, mdc) }

val y = Mono.deferContextual { context ->
var mdcData: MDC? = null
try {
val temp: MDCSnapshot? = context.getOrEmpty<Map<String, String>>(mdcContextKey)
.map { MDCSnapshot.of(it) }
.orElse(null)

temp?.use {
mdcData = MDC(mdcContextKey, it.copyOfContextMap)
}

return@deferContextual Mono.justOrEmpty(mdcData)
} catch (exception: Exception) {
return@deferContextual Mono.error<MDC>(exception)
}
}.contextWrite { MDCContext.put(it, mdc) }

StepVerifier.create(Mono.zip(x, y))
.expectNextMatches { it.t1 == it.t2 }
.verifyComplete()
}

@Test
fun `should throw exception from snapshot`() {
val logger = CoroutineLogger.reactorBuilder()
.withEnableError(true)
.withLogger(mockk(relaxed = true))
.build()

val contextNotExistException = mono { logger.snapshot() }

StepVerifier.create(contextNotExistException)
.expectError(ContextNotExistException::class.java)
.verify()

val invalidContextDataException = mono { logger.snapshot() }
.contextWrite { it.put(logger.mdcContextKey, 10) }

StepVerifier.create(invalidContextDataException)
.expectError(InvalidContextDataException::class.java)
.verify()
}
}
Loading

0 comments on commit 58995b7

Please sign in to comment.