Skip to content

Commit 50e2733

Browse files
Tolerate failures when recording WebClient metrics
Fixes gh-30978
1 parent cc2d689 commit 50e2733

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientFilterFunction.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121

2222
import io.micrometer.core.instrument.MeterRegistry;
2323
import io.micrometer.core.instrument.Tag;
24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
2426
import reactor.core.publisher.Mono;
2527
import reactor.core.publisher.SignalType;
2628
import reactor.util.context.Context;
@@ -38,13 +40,16 @@
3840
*
3941
* @author Brian Clozel
4042
* @author Tadaya Tsuyukubo
43+
* @author Scott Frederick
4144
* @since 2.1.0
4245
*/
4346
public class MetricsWebClientFilterFunction implements ExchangeFilterFunction {
4447

4548
private static final String METRICS_WEBCLIENT_START_TIME = MetricsWebClientFilterFunction.class.getName()
4649
+ ".START_TIME";
4750

51+
private static final Log logger = LogFactory.getLog(MetricsWebClientFilterFunction.class);
52+
4853
private final MeterRegistry meterRegistry;
4954

5055
private final WebClientExchangeTagsProvider tagProvider;
@@ -83,20 +88,25 @@ private Mono<ClientResponse> instrumentResponse(ClientRequest request, Mono<Clie
8388
return Mono.deferContextual((ctx) -> responseMono.doOnEach((signal) -> {
8489
if (signal.isOnNext() || signal.isOnError()) {
8590
responseReceived.set(true);
86-
Iterable<Tag> tags = this.tagProvider.tags(request, signal.get(), signal.getThrowable());
87-
recordTimer(tags, getStartTime(ctx));
91+
recordTimer(request, signal.get(), signal.getThrowable(), getStartTime(ctx));
8892
}
8993
}).doFinally((signalType) -> {
9094
if (!responseReceived.get() && SignalType.CANCEL.equals(signalType)) {
91-
Iterable<Tag> tags = this.tagProvider.tags(request, null, null);
92-
recordTimer(tags, getStartTime(ctx));
95+
recordTimer(request, null, null, getStartTime(ctx));
9396
}
9497
}));
9598
}
9699

97-
private void recordTimer(Iterable<Tag> tags, Long startTime) {
98-
this.autoTimer.builder(this.metricName).tags(tags).description("Timer of WebClient operation")
99-
.register(this.meterRegistry).record(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
100+
private void recordTimer(ClientRequest request, ClientResponse response, Throwable error, Long startTime) {
101+
try {
102+
Iterable<Tag> tags = this.tagProvider.tags(request, response, error);
103+
this.autoTimer.builder(this.metricName).tags(tags).description("Timer of WebClient operation")
104+
.register(this.meterRegistry).record(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
105+
}
106+
catch (Exception ex) {
107+
logger.warn("Failed to record timer metrics", ex);
108+
// Allow request-response exchange to continue, unaffected by metrics problem
109+
}
100110
}
101111

102112
private Long getStartTime(ContextView context) {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientFilterFunctionTests.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,9 +20,11 @@
2020
import java.net.URI;
2121
import java.time.Duration;
2222
import java.util.concurrent.TimeUnit;
23+
import java.util.concurrent.atomic.AtomicBoolean;
2324

2425
import io.micrometer.core.instrument.MeterRegistry;
2526
import io.micrometer.core.instrument.MockClock;
27+
import io.micrometer.core.instrument.Tag;
2628
import io.micrometer.core.instrument.Timer;
2729
import io.micrometer.core.instrument.search.MeterNotFoundException;
2830
import io.micrometer.core.instrument.simple.SimpleConfig;
@@ -49,6 +51,7 @@
4951
* Tests for {@link MetricsWebClientFilterFunction}
5052
*
5153
* @author Brian Clozel
54+
* @author Scott Frederick
5255
*/
5356
class MetricsWebClientFilterFunctionTests {
5457

@@ -58,15 +61,17 @@ class MetricsWebClientFilterFunctionTests {
5861

5962
private MetricsWebClientFilterFunction filterFunction;
6063

64+
private final FaultyTagsProvider tagsProvider = new FaultyTagsProvider();
65+
6166
private ClientResponse response;
6267

6368
private ExchangeFunction exchange;
6469

6570
@BeforeEach
6671
void setup() {
6772
this.registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
68-
this.filterFunction = new MetricsWebClientFilterFunction(this.registry,
69-
new DefaultWebClientExchangeTagsProvider(), "http.client.requests", AutoTimer.ENABLED);
73+
this.filterFunction = new MetricsWebClientFilterFunction(this.registry, this.tagsProvider,
74+
"http.client.requests", AutoTimer.ENABLED);
7075
this.response = mock(ClientResponse.class);
7176
this.exchange = (r) -> Mono.just(this.response);
7277
}
@@ -159,4 +164,31 @@ void filterWhenExceptionAndRetryShouldNotAccumulateRecordTime() {
159164
assertThat(timer.max(TimeUnit.MILLISECONDS)).isLessThan(2000);
160165
}
161166

167+
@Test
168+
void whenMetricsRecordingFailsThenFilteringSucceeds() {
169+
ClientRequest request = ClientRequest
170+
.create(HttpMethod.GET, URI.create("https://example.com/projects/spring-boot")).build();
171+
given(this.response.rawStatusCode()).willReturn(HttpStatus.OK.value());
172+
this.tagsProvider.failOnce();
173+
this.filterFunction.filter(request, this.exchange).block(Duration.ofSeconds(5));
174+
}
175+
176+
static class FaultyTagsProvider extends DefaultWebClientExchangeTagsProvider {
177+
178+
private final AtomicBoolean fail = new AtomicBoolean(false);
179+
180+
@Override
181+
public Iterable<Tag> tags(ClientRequest request, ClientResponse response, Throwable throwable) {
182+
if (this.fail.compareAndSet(true, false)) {
183+
throw new RuntimeException();
184+
}
185+
return super.tags(request, response, throwable);
186+
}
187+
188+
void failOnce() {
189+
this.fail.set(true);
190+
}
191+
192+
}
193+
162194
}

0 commit comments

Comments
 (0)