We have noticed that the webclient is forcing us to set the URI_TEMPLATE_ATTRIBUTE attribute to be able to extract the uri https://github.com/spring-projects/spring-framework/blob/3024c6efa96aa4b8651d6f11cdd08d3bb07ce136/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java#L456

Even if the attribute is not set, it compares the path with a single slash '/' which is wrong in our case. Path can be anything like "/foo/bar"

https://github.com/spring-projects/spring-framework/blob/3024c6efa96aa4b8651d6f11cdd08d3bb07ce136/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientRequestObservationConvention.java#L101C1-L110C3

    protected KeyValue uri(ClientRequestObservationContext context) {
        if (context.getUriTemplate() != null) {
            return KeyValue.of(LowCardinalityKeyNames.URI, extractPath(context.getUriTemplate()));
        }
        ClientRequest request = context.getRequest();
        if (request != null && ROOT_PATH.equals(request.url().getPath())) {
            return URI_ROOT;
        }
        return URI_NONE;
    }

as a work around we are proposing to get the uri from the path like this

  if (context.getRequest() != null && context.getRequest().url().getPath() != null) {
                    return KeyValue.of(ClientHttpObservationDocumentation.LowCardinalityKeyNames.URI, context.getRequest().url().getPath());
                }

Comment From: bclozel

I'm not sure I'm following.

The code change you're suggesting here would use "/" as the "uri" keyvalue but would get it from the request instead of using the static KeyValue as it's done in the current code. This creates more allocations and is decreasing performance at runtime for this case.

// this condition checks that the current path is "/"
if (request != null && ROOT_PATH.equals(request.url().getPath())) {
   return KeyValue.of(ClientHttpObservationDocumentation.LowCardinalityKeyNames.URI, context.getRequest().url().getPath());
}

Can you elaborate? Can you explain what are you trying to achieve, what are you getting and what would you like to get instead?

Comment From: amrFathyEldefrawy

sorry wrong copy paste, fixed the paragraph. I want to see the uri tag getting published and not as none.

For me to see the uri getting published, I have to force my webclient request to set URI_TEMPLATE_ATTRIBUTE attribute.

Comment From: bclozel

I understand now, thanks. With metrics, low cardinality must avoid cardinality explosion - the total number of possible values must be bounded. If not, memory grows infinitely to track all dimensions and this can lead to out of memory errors. If we did change the implementation like suggested, this could create a denial of service vulnerability in Spring. This could be the case in your application already.

See the "tag values" section of the micrometer documentation.

I'm closing this issue as we can't apply the suggestion here.

Comment From: amrFathyEldefrawy

By the way, we were able to solve the problem by setting the atttribute (URI_TEMPLATE_ATTRIBUTE, ourpath) on every request we do. as by default the constructor uri (Uri uri) does not it set it

Comment From: bclozel

@amrFathyEldefrawy as stated above, doing so is creating a denial of service vulnerability for your application.

Comment From: amrFathyEldefrawy

well, the dependencies we call have static urls (e.g., /foo/bar). we just need to track their our interactions with them. @bclozel

Comment From: bclozel

Noted. I just think that using the templated URI API variants gives you metrics already and it's safer than assuming that no developer in your team will ever introduce a call to /user/42 or /user?id=42. The only way to find out about this problem when it will happen is to face memory issues with your apps or problems with your metrics dashboard.

Comment From: amrFathyEldefrawy

yeah I agree, it make sense to disable it by default as it leads to cardinality explosion in dynamic urls. but we are going to enable it only on the these static urls. Thanks.

Comment From: amrFathyEldefrawy

@bclozel is there a way we can make URI_TEMPLATE_ATTRIBUTE attribute public ? we had to create our own copy of it internally. I think for better maintenance, we would like this to be visible from spring.

Comment From: bclozel

It's not meant to be used publicly and we won't provide any guarantee about it. But we're unlikely to change its value so it should be safe to copy it in your codebase. Contributing this information manually is not a supported case and you've chosen this solution after considering the tradeoffs. This is one of them.