Restrict "uri" KeyValue for client observations

Prior to this commit, the "uri" KeyValue for low cardinality metadata
would contain the entire uri template given to the HTTP client when
creating the request. This was a breaking change for existing metrics
dashboards, as previous support was removing the protocol, host and port
parts of the URI.
Indeed, this information is available in the "client.name" and
"http.uri" KayValue.

This commit parses and removes the protocol+host+port information from
the uri template for the "uri" KeyValue.

Fixes gh-29885
This commit is contained in:
Brian Clozel
2023-01-30 10:09:45 +01:00
parent b267547fb4
commit 5dfa61eb0b
8 changed files with 45 additions and 7 deletions

View File

@@ -67,6 +67,7 @@ public enum ClientHttpObservationDocumentation implements ObservationDocumentati
/**
* URI template used for HTTP request, or {@value KeyValue#NONE_VALUE} if none was provided.
* Only the path part of the URI is considered.
*/
URI {
@Override
@@ -123,7 +124,7 @@ public enum ClientHttpObservationDocumentation implements ObservationDocumentati
public enum HighCardinalityKeyNames implements KeyName {
/**
* HTTP request URI.
* The full HTTP request URI.
*/
HTTP_URL {
@Override

View File

@@ -17,6 +17,7 @@
package org.springframework.web.reactive.function.client;
import java.io.IOException;
import java.util.regex.Pattern;
import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
@@ -40,6 +41,8 @@ public class DefaultClientRequestObservationConvention implements ClientRequestO
private static final String ROOT_PATH = "/";
private static final Pattern PATTERN_BEFORE_PATH = Pattern.compile("^https?://[^/]+/");
private static final KeyValue URI_NONE = KeyValue.of(LowCardinalityKeyNames.URI, KeyValue.NONE_VALUE);
private static final KeyValue METHOD_NONE = KeyValue.of(LowCardinalityKeyNames.METHOD, KeyValue.NONE_VALUE);
@@ -94,7 +97,7 @@ public class DefaultClientRequestObservationConvention implements ClientRequestO
protected KeyValue uri(ClientRequestObservationContext context) {
if (context.getUriTemplate() != null) {
return KeyValue.of(LowCardinalityKeyNames.URI, context.getUriTemplate());
return KeyValue.of(LowCardinalityKeyNames.URI, extractPath(context.getUriTemplate()));
}
ClientRequest request = context.getRequest();
if (request != null && ROOT_PATH.equals(request.url().getPath())) {
@@ -103,6 +106,11 @@ public class DefaultClientRequestObservationConvention implements ClientRequestO
return URI_NONE;
}
private static String extractPath(String uriTemplate) {
String path = PATTERN_BEFORE_PATH.matcher(uriTemplate).replaceFirst("");
return (path.startsWith("/") ? path : "/" + path);
}
protected KeyValue method(ClientRequestObservationContext context) {
if (context.getRequest() != null) {
return KeyValue.of(LowCardinalityKeyNames.METHOD, context.getRequest().method().name());

View File

@@ -114,6 +114,14 @@ class DefaultClientRequestObservationConventionTests {
assertThat(this.observationConvention.getLowCardinalityKeyValues(context)).contains(KeyValue.of("uri", "/"));
}
@Test
void shouldOnlyConsiderPathForUriKeyValue() {
ClientRequestObservationContext context = createContext(ClientRequest.create(HttpMethod.GET, URI.create("https://example.org/resource/42")));
context.setUriTemplate("https://example.org/resource/{id}");
context.setRequest(context.getCarrier().build());
assertThat(this.observationConvention.getLowCardinalityKeyValues(context)).contains(KeyValue.of("uri", "/resource/{id}"));
}
private ClientRequestObservationContext createContext(ClientRequest.Builder request) {
ClientRequestObservationContext context = new ClientRequestObservationContext();
context.setCarrier(request);