PlaytikaOSS / feign-reactive

Reactive Feign client inspired by https://github.com/OpenFeign project
Apache License 2.0
610 stars 125 forks source link

Use unexpanded URI for Webclient metrics #474

Closed ben-r closed 2 years ago

ben-r commented 2 years ago

Client metrics are recorded based on expanded URI templates which creates a new time series for every new value encountered (cardinality explosion), which is harmful for time series based databases like Prometheus.

Example

@RequestLine("GET /api/test/{id}/response")
fun getRequest(@Param("id") id: String, @QueryMap params: Query): Mono<String> // id = 1; params = {"param": "hello"}

should produce a metric like http_client_requests_seconds_count{uri="/api/test/{id}/response}

Instead it produces http_client_requests_seconds_count{uri="/api/test/1/response?param=hello}

which makes it impossible to group metrics for this endpoint.

I didn't dig deeper into the code, but I'm pretty sure it goes into the same direction as this PR in the OpenFeign project: https://github.com/OpenFeign/feign/pull/1493

The recommended way to build the request to achieve correct metrics looks like this

val id = 1
webClient
        .get()
        .uri("/api/test/{id}/response") { uriBuilder ->
            uriBuilder
                .queryParam("param", "hello")
                .build(id)

You can find a sample project showcasing the issue here: https://github.com/ben-r/reactive-feign-metrics-example

kptfh commented 2 years ago

Here is the micrometer metrics autoconfiguration that should fit you

@Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass(name = "io.micrometer.core.instrument.MeterRegistry")
    @ConditionalOnProperty(name = "reactive.feign.metrics.enabled", havingValue = "true")
    public MicrometerReactiveLogger metricsReactiveLogger() {
        return MicrometerReactiveLogger.basicTimer();
    }

Check MicrometerReactiveLogger and MetricsTag for available tags

kptfh commented 2 years ago

Feel free to reopen issue if we need to tune MicrometerReactiveLogger