Skip to content

Commit

Permalink
Polish HTTP client observability changes
Browse files Browse the repository at this point in the history
See gh-32484
See gh-32518

Closes gh-32636
  • Loading branch information
izeye authored and bclozel committed Oct 7, 2022
1 parent 23a9818 commit 73fd760
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class HttpClientObservationsAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MeterRegistry.class)
@ConditionalOnBean(MeterRegistry.class)
class MeterFilterConfiguration {
static class MeterFilterConfiguration {

@Bean
@Order(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void shouldUseConfiguredName() {
}

@Test
void shouldOnlySupportClientHttpObservationContext() {
void shouldOnlySupportClientObservationContext() {
assertThat(this.convention.supportsContext(this.context)).isTrue();
assertThat(this.convention.supportsContext(new OtherContext())).isFalse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) {
@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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 73fd760

Please sign in to comment.