line / armeria

Your go-to microservice framework for any situation, from the creator of Netty et al. You can build any type of microservice leveraging your favorite technologies, including gRPC, Thrift, Kotlin, Retrofit, Reactive Streams, Spring Boot and Dropwizard.
https://armeria.dev
Apache License 2.0
4.78k stars 905 forks source link

Support micrometer-context-propagation #5145

Open be-hase opened 1 year ago

be-hase commented 1 year ago

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...

jrhee17 commented 1 year ago

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

trustin commented 5 months ago

@chickenchickenlove will work on it :smile:

be-hase commented 5 months ago

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

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