Polish HTTP client observability changes

See gh-32484
See gh-32518

Closes gh-32636
This commit is contained in:
Johnny Lim
2022-10-08 01:04:05 +09:00
committed by Brian Clozel
parent 23a9818e0d
commit 73fd760137
8 changed files with 15 additions and 20 deletions

View File

@@ -58,7 +58,7 @@ public class HttpClientObservationsAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MeterRegistry.class)
@ConditionalOnBean(MeterRegistry.class)
class MeterFilterConfiguration {
static class MeterFilterConfiguration {
@Bean
@Order(0)

View File

@@ -44,7 +44,7 @@ import org.springframework.web.client.RestTemplate;
class RestTemplateObservationConfiguration {
@Bean
ObservationRestTemplateCustomizer metricsRestTemplateCustomizer(ObservationRegistry observationRegistry,
ObservationRestTemplateCustomizer observationRestTemplateCustomizer(ObservationRegistry observationRegistry,
ObservationProperties observationProperties, MetricsProperties metricsProperties,
ObjectProvider<RestTemplateExchangeTagsProvider> optionalTagsProvider) {
String metricName = metricsProperties.getWeb().getClient().getRequest().getMetricName();

View File

@@ -41,7 +41,7 @@ import org.springframework.web.reactive.function.client.WebClient;
class WebClientObservationConfiguration {
@Bean
ObservationWebClientCustomizer metricsWebClientCustomizer(ObservationRegistry observationRegistry,
ObservationWebClientCustomizer observationWebClientCustomizer(ObservationRegistry observationRegistry,
ObservationProperties observationProperties,
ObjectProvider<WebClientExchangeTagsProvider> optionalTagsProvider, MetricsProperties metricsProperties) {
String metricName = metricsProperties.getWeb().getClient().getRequest().getMetricName();

View File

@@ -65,7 +65,7 @@ class ClientObservationConventionAdapterTests {
}
@Test
void shouldOnlySupportClientHttpObservationContext() {
void shouldOnlySupportClientObservationContext() {
assertThat(this.convention.supportsContext(this.context)).isTrue();
assertThat(this.convention.supportsContext(new OtherContext())).isFalse();
}

View File

@@ -139,7 +139,8 @@ class RestTemplateObservationConfigurationTests {
@Test
void backsOffWhenRestTemplateBuilderIsMissing() {
new ApplicationContextRunner().with(MetricsRun.simple())
.withConfiguration(AutoConfigurations.of(HttpClientObservationsAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(ObservationAutoConfiguration.class,
HttpClientObservationsAutoConfiguration.class))
.run((context) -> assertThat(context).doesNotHaveBean(DefaultRestTemplateExchangeTagsProvider.class)
.doesNotHaveBean(ObservationRestTemplateCustomizer.class));
}

View File

@@ -16,13 +16,11 @@
package org.springframework.boot.actuate.metrics.web.client;
import io.micrometer.observation.ObservationConvention;
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.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
@@ -46,11 +44,10 @@ class ObservationRestTemplateCustomizerTests {
@Test
void shouldCustomizeObservationConfiguration() {
this.customizer.customize(this.restTemplate);
assertThat((ObservationRegistry) ReflectionTestUtils.getField(this.restTemplate, "observationRegistry"))
.isEqualTo(this.observationRegistry);
assertThat((ObservationConvention<?>) ReflectionTestUtils.getField(this.restTemplate, "observationConvention"))
.isInstanceOf(DefaultClientHttpObservationConvention.class).extracting("name")
.isEqualTo(TEST_METRIC_NAME);
assertThat(this.restTemplate).hasFieldOrPropertyWithValue("observationRegistry", this.observationRegistry);
assertThat(this.restTemplate).extracting("observationConvention")
.isInstanceOf(DefaultClientHttpObservationConvention.class)
.hasFieldOrPropertyWithValue("name", TEST_METRIC_NAME);
}
}

View File

@@ -16,12 +16,9 @@
package org.springframework.boot.actuate.metrics.web.reactive.client;
import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.reactive.function.client.ClientObservationConvention;
import org.springframework.web.reactive.function.client.DefaultClientObservationConvention;
import org.springframework.web.reactive.function.client.WebClient;
@@ -51,10 +48,10 @@ class ObservationWebClientCustomizerTests {
@Test
void shouldCustomizeObservationConfiguration() {
this.customizer.customize(this.clientBuilder);
assertThat((ObservationRegistry) ReflectionTestUtils.getField(this.clientBuilder, "observationRegistry"))
.isEqualTo(this.observationRegistry);
assertThat((ObservationConvention<?>) ReflectionTestUtils.getField(this.clientBuilder, "observationConvention"))
.isInstanceOf(DefaultClientObservationConvention.class).extracting("name").isEqualTo(TEST_METRIC_NAME);
assertThat(this.clientBuilder).hasFieldOrPropertyWithValue("observationRegistry", this.observationRegistry);
assertThat(this.clientBuilder).extracting("observationConvention")
.isInstanceOf(DefaultClientObservationConvention.class)
.hasFieldOrPropertyWithValue("name", TEST_METRIC_NAME);
}
}

View File

@@ -862,7 +862,7 @@ For that, you have to inject the auto-configured builder and use it to create in
* `RestTemplateBuilder` for `RestTemplate`
* `WebClient.Builder` for `WebClient`
You can also manually apply the customizers responsible for this instrumentation, namely `ObservationRestTemplateCustomizer` and `MetricsWebClientCustomizer`.
You can also manually apply the customizers responsible for this instrumentation, namely `ObservationRestTemplateCustomizer` and `ObservationWebClientCustomizer`.
By default, metrics are generated with the name, `http.client.requests`.
You can customize the name by setting the configprop:management.observations.http.client.requests.name[] property.