armeria icon indicating copy to clipboard operation
armeria copied to clipboard

Support micrometer-context-propagation

Open be-hase opened this issue 2 years ago • 3 comments

micrometer-context-propagation is now available. https://github.com/micrometer-metrics/context-propagation

And the reactor supports micrometer-context-propagation. https://spring.io/blog/2023/03/30/context-propagation-with-project-reactor-3-unified-bridging-between-reactive

Currently, Armeria provides RequestContextHooks, which could be replaced by providing an extension to micrometer-context-propagation.

like this:

// Sorry, I wrote it in kotlin.

class ArmeriaRequestContextAccessor : ThreadLocalAccessor<RequestContext> {
    override fun key(): Any {
        return KEY
    }

    override fun getValue(): RequestContext? {
        return RequestContextUtil.get()
    }

    override fun setValue(value: RequestContext) {
        RequestContextUtil.getAndSet<RequestContext>(value)
    }

    override fun setValue() {
        // NOOP
    }

    override fun restore(previousValue: RequestContext) {
        RequestContextUtil.getAndSet<RequestContext>(previousValue)
    }

    override fun restore() {
        RequestContextUtil.pop()
    }

    companion object {
        val KEY = ArmeriaRequestContextAccessor::class.java
    }
}

Then, users can execute the following code.

fun main(args: Array<String>) {
    ContextRegistry.getInstance().registerThreadLocalAccessor(ArmeriaRequestContextAccessor())
    Hooks.enableAutomaticContextPropagation()
    
    // run application
}

From the above article, it seems to me that the performance aspect has been devised. ( I haven't measured it but...

be-hase avatar Aug 24 '23 08:08 be-hase

Took a quick look through the API/source and looks promising! 👍
If I understood correctly, this can be a much simpler solution over RequestContextHooks

jrhee17 avatar Aug 24 '23 09:08 jrhee17

@chickenchickenlove will work on it :smile:

trustin avatar Apr 03 '24 05:04 trustin

wow, sounds good. I think it will probably improve performance.

( Sorry I didn't contribute with a pull request. haha

be-hase avatar Apr 04 '24 13:04 be-hase