Firefly v4.8.1 is released

12 Jun 2018, Alvin Qiu

Firefly v4.8.1 adds Coroutine MDC adapter and fixes the AsynchronousTcpSession cannot write big data on Windows system.

The Firefly HTTP server is asynchronous. An HTTP request passes many threads. The default MDC saves data in the ThreadLocal. It means the default MDC can not track the user request. We add a new CoroutineMappedDiagnosticContext that can save data through the whole HTTP request. The CoroutineMappedDiagnosticContext uses the coroutine interceptor mechanism. You can only use it in Firefly HTTP server Kotlin version.

For example, the first, we need to create a new Java ServiceLoader configuration to replace the default MDC implementation. Create a new file in the classpath:

classpath:/META-INF/services/com.firefly.utils.log.MappedDiagnosticContext

Add a new MDC class name in this file:

com.firefly.kotlin.ext.log.CoroutineMappedDiagnosticContext

Initialize the CoroutineMappedDiagnosticContext:

@Inject
private lateinit var requestCtx: CoroutineLocal<RoutingContext>

@InitialMethod
fun init() {
    val mdc = MappedDiagnosticContextFactory.getInstance()
            .mappedDiagnosticContext as CoroutineMappedDiagnosticContext
    mdc.setRequestCtx(requestCtx)
}

Then we can use the MDC APIs in the Firefly HTTP server Kotlin version. Such as add a tracing id:

val mdc = MDC.putCloseable("tracingId", UUID.randomUUID().toString().replace("-", ""))

If you start a coroutine in a new context, you need to combine the new context and the current request context. We provide the method asyncTraceable.

fun <T> asyncTraceable(context: ContinuationInterceptor = Unconfined, block: suspend CoroutineScope.() -> T): Deferred<T> = asyncTraceable(getRequestCtx(), context, block)

Combine the new coroutine context to the current request context. Such as:

val data = asyncTraceable(ioBlocking) {
    fileInputStream.use {
        `$`.io.readBytes(it)
    }
}.await()

Update log:

  1. Add CoroutineMappedDiagnosticContext.
  2. Fix the AsynchronousTcpSession can not write big data on windows system.