Skip to content

Commit

Permalink
Restore configuration property for http requests names when using Web…
Browse files Browse the repository at this point in the history
…Flux

Closes gh-39083
  • Loading branch information
mhalbritter committed Jan 11, 2024
1 parent a5d3fb5 commit a424ba2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,10 +30,13 @@
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.observation.DefaultServerRequestObservationConvention;
import org.springframework.http.server.reactive.observation.ServerRequestObservationConvention;

/**
* {@link EnableAutoConfiguration Auto-configuration} for instrumentation of Spring
Expand All @@ -42,6 +45,7 @@
* @author Brian Clozel
* @author Jon Schneider
* @author Dmytro Nosan
* @author Moritz Halbritter
* @since 3.0.0
*/
@AutoConfiguration(after = { SimpleMetricsExportAutoConfiguration.class, ObservationAutoConfiguration.class })
Expand All @@ -51,15 +55,27 @@
@EnableConfigurationProperties({ MetricsProperties.class, ObservationProperties.class })
public class WebFluxObservationAutoConfiguration {

private final ObservationProperties observationProperties;

WebFluxObservationAutoConfiguration(ObservationProperties observationProperties) {
this.observationProperties = observationProperties;
}

@Bean
@Order(0)
MeterFilter metricsHttpServerUriTagFilter(MetricsProperties metricsProperties,
ObservationProperties observationProperties) {
String name = observationProperties.getHttp().getServer().getRequests().getName();
MeterFilter metricsHttpServerUriTagFilter(MetricsProperties metricsProperties) {
String name = this.observationProperties.getHttp().getServer().getRequests().getName();
MeterFilter filter = new OnlyOnceLoggingDenyMeterFilter(
() -> "Reached the maximum number of URI tags for '%s'.".formatted(name));
return MeterFilter.maximumAllowableTags(name, "uri", metricsProperties.getWeb().getServer().getMaxUriTags(),
filter);
}

@Bean
@ConditionalOnMissingBean(ServerRequestObservationConvention.class)
DefaultServerRequestObservationConvention defaultServerRequestObservationConvention() {
return new DefaultServerRequestObservationConvention(
this.observationProperties.getHttp().getServer().getRequests().getName());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,15 +33,19 @@
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.http.server.reactive.observation.DefaultServerRequestObservationConvention;
import org.springframework.http.server.reactive.observation.ServerRequestObservationConvention;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link WebFluxObservationAutoConfiguration}
*
* @author Brian Clozel
* @author Dmytro Nosan
* @author Madhura Bhave
* @author Moritz Halbritter
*/
@ExtendWith(OutputCaptureExtension.class)
class WebFluxObservationAutoConfigurationTests {
Expand Down Expand Up @@ -91,6 +95,28 @@ void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput output) {
});
}

@Test
void shouldSupplyDefaultServerRequestObservationConvention() {
this.contextRunner.withPropertyValues("management.observations.http.server.requests.name=some-other-name")
.run((context) -> {
assertThat(context).hasSingleBean(DefaultServerRequestObservationConvention.class);
DefaultServerRequestObservationConvention bean = context
.getBean(DefaultServerRequestObservationConvention.class);
assertThat(bean.getName()).isEqualTo("some-other-name");
});
}

@Test
void shouldBackOffOnCustomServerRequestObservationConvention() {
this.contextRunner
.withBean("customServerRequestObservationConvention", ServerRequestObservationConvention.class,
() -> mock(ServerRequestObservationConvention.class))
.run((context) -> {
assertThat(context).hasBean("customServerRequestObservationConvention");
assertThat(context).hasSingleBean(ServerRequestObservationConvention.class);
});
}

private MeterRegistry getInitializedMeterRegistry(AssertableReactiveWebApplicationContext context) {
return getInitializedMeterRegistry(context, "http.server.requests");
}
Expand Down

0 comments on commit a424ba2

Please sign in to comment.