Adapt to HTTP Observation changes in Framework

This commit adapts to the renames/package changes in the HTTP
Observation support in Spring Framework.

See spring-projects/spring-framework#29334
pull/33505/head
Brian Clozel 2 years ago
parent ed1695f109
commit cbca457eff

@ -21,17 +21,17 @@ import io.micrometer.core.instrument.Tag;
import io.micrometer.observation.Observation;
import org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTagsProvider;
import org.springframework.http.client.observation.ClientHttpObservationContext;
import org.springframework.http.client.observation.ClientHttpObservationConvention;
import org.springframework.http.client.observation.ClientRequestObservationContext;
import org.springframework.http.client.observation.ClientRequestObservationConvention;
/**
* Adapter class that applies {@link RestTemplateExchangeTagsProvider} tags as a
* {@link ClientHttpObservationConvention}.
* {@link ClientRequestObservationConvention}.
*
* @author Brian Clozel
*/
@SuppressWarnings({ "removal" })
class ClientHttpObservationConventionAdapter implements ClientHttpObservationConvention {
class ClientHttpObservationConventionAdapter implements ClientRequestObservationConvention {
private final String metricName;
@ -44,12 +44,12 @@ class ClientHttpObservationConventionAdapter implements ClientHttpObservationCon
@Override
public boolean supportsContext(Observation.Context context) {
return context instanceof ClientHttpObservationContext;
return context instanceof ClientRequestObservationContext;
}
@Override
@SuppressWarnings("deprecation")
public KeyValues getLowCardinalityKeyValues(ClientHttpObservationContext context) {
public KeyValues getLowCardinalityKeyValues(ClientRequestObservationContext context) {
KeyValues keyValues = KeyValues.empty();
Iterable<Tag> tags = this.tagsProvider.getTags(context.getUriTemplate(), context.getCarrier(),
context.getResponse());
@ -60,7 +60,7 @@ class ClientHttpObservationConventionAdapter implements ClientHttpObservationCon
}
@Override
public KeyValues getHighCardinalityKeyValues(ClientHttpObservationContext context) {
public KeyValues getHighCardinalityKeyValues(ClientRequestObservationContext context) {
return KeyValues.empty();
}

@ -21,19 +21,19 @@ import io.micrometer.core.instrument.Tag;
import io.micrometer.observation.Observation;
import org.springframework.boot.actuate.metrics.web.reactive.client.WebClientExchangeTagsProvider;
import org.springframework.web.reactive.function.client.ClientObservationContext;
import org.springframework.web.reactive.function.client.ClientObservationConvention;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientRequestObservationContext;
import org.springframework.web.reactive.function.client.ClientRequestObservationConvention;
import org.springframework.web.reactive.function.client.WebClient;
/**
* Adapter class that applies {@link WebClientExchangeTagsProvider} tags as a
* {@link ClientObservationConvention}.
* {@link ClientRequestObservationConvention}.
*
* @author Brian Clozel
*/
@SuppressWarnings({ "deprecation", "removal" })
class ClientObservationConventionAdapter implements ClientObservationConvention {
class ClientObservationConventionAdapter implements ClientRequestObservationConvention {
private static final String URI_TEMPLATE_ATTRIBUTE = WebClient.class.getName() + ".uriTemplate";
@ -48,11 +48,11 @@ class ClientObservationConventionAdapter implements ClientObservationConvention
@Override
public boolean supportsContext(Observation.Context context) {
return context instanceof ClientObservationContext;
return context instanceof ClientRequestObservationContext;
}
@Override
public KeyValues getLowCardinalityKeyValues(ClientObservationContext context) {
public KeyValues getLowCardinalityKeyValues(ClientRequestObservationContext context) {
KeyValues keyValues = KeyValues.empty();
mutateClientRequest(context);
Iterable<Tag> tags = this.tagsProvider.tags(context.getCarrier(), context.getResponse(), context.getError());
@ -66,14 +66,14 @@ class ClientObservationConventionAdapter implements ClientObservationConvention
* {@link WebClientExchangeTagsProvider} relies on a request attribute to get the URI
* template, we need to adapt to that.
*/
private static void mutateClientRequest(ClientObservationContext context) {
private static void mutateClientRequest(ClientRequestObservationContext context) {
ClientRequest clientRequest = ClientRequest.from(context.getCarrier())
.attribute(URI_TEMPLATE_ATTRIBUTE, context.getUriTemplate()).build();
context.setCarrier(clientRequest);
}
@Override
public KeyValues getHighCardinalityKeyValues(ClientObservationContext context) {
public KeyValues getHighCardinalityKeyValues(ClientRequestObservationContext context) {
return KeyValues.empty();
}

@ -28,8 +28,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.observation.ClientHttpObservationConvention;
import org.springframework.http.client.observation.DefaultClientHttpObservationConvention;
import org.springframework.http.client.observation.ClientRequestObservationConvention;
import org.springframework.http.client.observation.DefaultClientRequestObservationConvention;
import org.springframework.web.client.RestTemplate;
/**
@ -51,9 +51,9 @@ class RestTemplateObservationConfiguration {
String observationName = observationProperties.getHttp().getClient().getRequests().getName();
String name = (observationName != null) ? observationName : metricName;
RestTemplateExchangeTagsProvider tagsProvider = optionalTagsProvider.getIfAvailable();
ClientHttpObservationConvention observationConvention = (tagsProvider != null)
ClientRequestObservationConvention observationConvention = (tagsProvider != null)
? new ClientHttpObservationConventionAdapter(name, tagsProvider)
: new DefaultClientHttpObservationConvention(name);
: new DefaultClientRequestObservationConvention(name);
return new ObservationRestTemplateCustomizer(observationRegistry, observationConvention);
}

@ -26,8 +26,8 @@ import org.springframework.boot.actuate.metrics.web.reactive.client.WebClientExc
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.ClientObservationConvention;
import org.springframework.web.reactive.function.client.DefaultClientObservationConvention;
import org.springframework.web.reactive.function.client.ClientRequestObservationConvention;
import org.springframework.web.reactive.function.client.DefaultClientRequestObservationConvention;
import org.springframework.web.reactive.function.client.WebClient;
/**
@ -48,9 +48,9 @@ class WebClientObservationConfiguration {
String observationName = observationProperties.getHttp().getClient().getRequests().getName();
String name = (observationName != null) ? observationName : metricName;
WebClientExchangeTagsProvider tagsProvider = optionalTagsProvider.getIfAvailable();
ClientObservationConvention observationConvention = (tagsProvider != null)
ClientRequestObservationConvention observationConvention = (tagsProvider != null)
? new ClientObservationConventionAdapter(name, tagsProvider)
: new DefaultClientObservationConvention(name);
: new DefaultClientRequestObservationConvention(name);
return new ObservationWebClientCustomizer(observationRegistry, observationConvention);
}

@ -28,7 +28,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.observation.ClientHttpObservationContext;
import org.springframework.http.client.observation.ClientRequestObservationContext;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpResponse;
@ -51,11 +51,11 @@ class ClientHttpObservationConventionAdapterTests {
private ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.OK);
private ClientHttpObservationContext context;
private ClientRequestObservationContext context;
@BeforeEach
void setup() {
this.context = new ClientHttpObservationContext(this.request);
this.context = new ClientRequestObservationContext(this.request);
this.context.setResponse(this.response);
this.context.setUriTemplate("/resource/{name}");
}

@ -26,8 +26,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.metrics.web.reactive.client.DefaultWebClientExchangeTagsProvider;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.ClientObservationContext;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientRequestObservationContext;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
@ -49,11 +49,11 @@ class ClientObservationConventionAdapterTests {
private ClientResponse response = ClientResponse.create(HttpStatus.OK).body("foo").build();
private ClientObservationContext context;
private ClientRequestObservationContext context;
@BeforeEach
void setup() {
this.context = new ClientObservationContext();
this.context = new ClientRequestObservationContext();
this.context.setCarrier(this.request);
this.context.setResponse(this.response);
this.context.setUriTemplate("/resource/{name}");

@ -22,7 +22,6 @@ import io.micrometer.core.instrument.Tag;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.observation.DefaultClientHttpObservationConvention;
import org.springframework.util.StringUtils;
/**
@ -32,7 +31,7 @@ import org.springframework.util.StringUtils;
* @author Nishant Raut
* @since 2.0.0
* @deprecated since 3.0.0 for removal in 3.2.0 in favor of
* {@link DefaultClientHttpObservationConvention}
* {@link org.springframework.http.client.observation.DefaultClientRequestObservationConvention}
*/
@Deprecated(since = "3.0.0", forRemoval = true)
@SuppressWarnings({ "deprecation", "removal" })

@ -19,7 +19,7 @@ package org.springframework.boot.actuate.metrics.web.client;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.http.client.observation.ClientHttpObservationConvention;
import org.springframework.http.client.observation.ClientRequestObservationConvention;
import org.springframework.web.client.RestTemplate;
/**
@ -33,7 +33,7 @@ public class ObservationRestTemplateCustomizer implements RestTemplateCustomizer
private final ObservationRegistry observationRegistry;
private final ClientHttpObservationConvention observationConvention;
private final ClientRequestObservationConvention observationConvention;
/**
* Create a new {@code ObservationRestTemplateCustomizer}.
@ -41,7 +41,7 @@ public class ObservationRestTemplateCustomizer implements RestTemplateCustomizer
* @param observationRegistry the observation registry
*/
public ObservationRestTemplateCustomizer(ObservationRegistry observationRegistry,
ClientHttpObservationConvention observationConvention) {
ClientRequestObservationConvention observationConvention) {
this.observationConvention = observationConvention;
this.observationRegistry = observationRegistry;
}

@ -25,7 +25,7 @@ import io.micrometer.core.instrument.Tag;
import org.springframework.boot.actuate.metrics.http.Outcome;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.observation.DefaultClientHttpObservationConvention;
import org.springframework.http.client.observation.DefaultClientRequestObservationConvention;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
@ -39,7 +39,7 @@ import org.springframework.web.client.RestTemplate;
* @author Brian Clozel
* @since 2.0.0
* @deprecated since 3.0.0 for removal in 3.2.0 in favor of
* {@link DefaultClientHttpObservationConvention}
* {@link DefaultClientRequestObservationConvention}
*/
@Deprecated(since = "3.0.0", forRemoval = true)
public final class RestTemplateExchangeTags {

@ -20,7 +20,7 @@ import io.micrometer.core.instrument.Tag;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.observation.ClientHttpObservationConvention;
import org.springframework.http.client.observation.ClientRequestObservationConvention;
import org.springframework.web.client.RestTemplate;
/**
@ -30,7 +30,7 @@ import org.springframework.web.client.RestTemplate;
* @author Andy Wilkinson
* @since 2.0.0
* @deprecated since 3.0.0 for removal in 3.2.0 in favor of
* {@link ClientHttpObservationConvention}
* {@link ClientRequestObservationConvention}
*/
@FunctionalInterface
@Deprecated(since = "3.0.0", forRemoval = true)

@ -19,7 +19,7 @@ package org.springframework.boot.actuate.metrics.web.reactive.client;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.web.reactive.function.client.ClientObservationConvention;
import org.springframework.web.reactive.function.client.ClientRequestObservationConvention;
import org.springframework.web.reactive.function.client.WebClient;
/**
@ -33,7 +33,7 @@ public class ObservationWebClientCustomizer implements WebClientCustomizer {
private final ObservationRegistry observationRegistry;
private final ClientObservationConvention observationConvention;
private final ClientRequestObservationConvention observationConvention;
/**
* Create a new {@code ObservationWebClientCustomizer} that will configure the
@ -42,7 +42,7 @@ public class ObservationWebClientCustomizer implements WebClientCustomizer {
* @param observationConvention the convention to use to populate observations
*/
public ObservationWebClientCustomizer(ObservationRegistry observationRegistry,
ClientObservationConvention observationConvention) {
ClientRequestObservationConvention observationConvention) {
this.observationRegistry = observationRegistry;
this.observationConvention = observationConvention;
}

@ -20,7 +20,7 @@ import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.http.client.observation.DefaultClientHttpObservationConvention;
import org.springframework.http.client.observation.DefaultClientRequestObservationConvention;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
@ -39,14 +39,14 @@ class ObservationRestTemplateCustomizerTests {
private RestTemplate restTemplate = new RestTemplate();
private ObservationRestTemplateCustomizer customizer = new ObservationRestTemplateCustomizer(
this.observationRegistry, new DefaultClientHttpObservationConvention(TEST_METRIC_NAME));
this.observationRegistry, new DefaultClientRequestObservationConvention(TEST_METRIC_NAME));
@Test
void shouldCustomizeObservationConfiguration() {
this.customizer.customize(this.restTemplate);
assertThat(this.restTemplate).hasFieldOrPropertyWithValue("observationRegistry", this.observationRegistry);
assertThat(this.restTemplate).extracting("observationConvention")
.isInstanceOf(DefaultClientHttpObservationConvention.class)
.isInstanceOf(DefaultClientRequestObservationConvention.class)
.hasFieldOrPropertyWithValue("name", TEST_METRIC_NAME);
}

@ -19,8 +19,8 @@ package org.springframework.boot.actuate.metrics.web.reactive.client;
import io.micrometer.observation.tck.TestObservationRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.web.reactive.function.client.ClientObservationConvention;
import org.springframework.web.reactive.function.client.DefaultClientObservationConvention;
import org.springframework.web.reactive.function.client.ClientRequestObservationConvention;
import org.springframework.web.reactive.function.client.DefaultClientRequestObservationConvention;
import org.springframework.web.reactive.function.client.WebClient;
import static org.assertj.core.api.Assertions.assertThat;
@ -37,7 +37,7 @@ class ObservationWebClientCustomizerTests {
private TestObservationRegistry observationRegistry = TestObservationRegistry.create();
private ClientObservationConvention observationConvention = new DefaultClientObservationConvention(
private ClientRequestObservationConvention observationConvention = new DefaultClientRequestObservationConvention(
TEST_METRIC_NAME);
private ObservationWebClientCustomizer customizer = new ObservationWebClientCustomizer(this.observationRegistry,
@ -50,7 +50,7 @@ class ObservationWebClientCustomizerTests {
this.customizer.customize(this.clientBuilder);
assertThat(this.clientBuilder).hasFieldOrPropertyWithValue("observationRegistry", this.observationRegistry);
assertThat(this.clientBuilder).extracting("observationConvention")
.isInstanceOf(DefaultClientObservationConvention.class)
.isInstanceOf(DefaultClientRequestObservationConvention.class)
.hasFieldOrPropertyWithValue("name", TEST_METRIC_NAME);
}

Loading…
Cancel
Save