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

@@ -70,6 +70,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

View File

@@ -17,6 +17,7 @@
package org.springframework.http.client.observation;
import java.io.IOException;
import java.util.regex.Pattern;
import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
@@ -39,6 +40,8 @@ public class DefaultClientRequestObservationConvention implements ClientRequestO
private static final String DEFAULT_NAME = "http.client.requests";
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);
@@ -92,11 +95,16 @@ 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()));
}
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.getCarrier() != null) {
return KeyValue.of(LowCardinalityKeyNames.METHOD, context.getCarrier().getMethod().name());