|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 |
|
22 | 22 | import io.micrometer.core.instrument.MeterRegistry;
|
23 | 23 | import io.micrometer.core.instrument.Tag;
|
| 24 | +import org.apache.commons.logging.Log; |
| 25 | +import org.apache.commons.logging.LogFactory; |
24 | 26 | import reactor.core.publisher.Mono;
|
25 | 27 | import reactor.core.publisher.SignalType;
|
26 | 28 | import reactor.util.context.Context;
|
|
38 | 40 | *
|
39 | 41 | * @author Brian Clozel
|
40 | 42 | * @author Tadaya Tsuyukubo
|
| 43 | + * @author Scott Frederick |
41 | 44 | * @since 2.1.0
|
42 | 45 | */
|
43 | 46 | public class MetricsWebClientFilterFunction implements ExchangeFilterFunction {
|
44 | 47 |
|
45 | 48 | private static final String METRICS_WEBCLIENT_START_TIME = MetricsWebClientFilterFunction.class.getName()
|
46 | 49 | + ".START_TIME";
|
47 | 50 |
|
| 51 | + private static final Log logger = LogFactory.getLog(MetricsWebClientFilterFunction.class); |
| 52 | + |
48 | 53 | private final MeterRegistry meterRegistry;
|
49 | 54 |
|
50 | 55 | private final WebClientExchangeTagsProvider tagProvider;
|
@@ -83,20 +88,25 @@ private Mono<ClientResponse> instrumentResponse(ClientRequest request, Mono<Clie
|
83 | 88 | return Mono.deferContextual((ctx) -> responseMono.doOnEach((signal) -> {
|
84 | 89 | if (signal.isOnNext() || signal.isOnError()) {
|
85 | 90 | 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)); |
88 | 92 | }
|
89 | 93 | }).doFinally((signalType) -> {
|
90 | 94 | 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)); |
93 | 96 | }
|
94 | 97 | }));
|
95 | 98 | }
|
96 | 99 |
|
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 | + } |
100 | 110 | }
|
101 | 111 |
|
102 | 112 | private Long getStartTime(ContextView context) {
|
|
0 commit comments