Skip to content

Commit 7861fbc

Browse files
DarkAtravelo
andauthored
Fix exception thrown when using a custom error decoder with metrics (#1468)
* #1466: fixed metered feign client throwing IllegalArgumentException when using custom ErrorDecoder * #1466: add missing license header * Fold MeteredFeignClientTest into AbstractMetricsTestBase Co-authored-by: Marvin Froeder <marvin.froeder@dovetailstudios.com>
1 parent 3fa3e1a commit 7861fbc

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

dropwizard-metrics4/src/test/java/feign/metrics4/Metrics4CapabilityTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ protected Metric getMetric(String suffix, String... tags) {
7171
}
7272

7373
for (int i = 0; i < tags.length; i += 2) {
74-
if (!name.contains(tags[i]) && !name.contains(tags[i] + 1)) {
74+
// metrics 4 doesn't support tags, for that reason we don't include tag name
75+
if (!name.contains(tags[i + 1])) {
7576
return false;
7677
}
7778
}

micrometer/src/main/java/feign/micrometer/MeteredInvocationHandleFactory.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,15 @@ public InvocationHandler create(Target target, Map<Method, MethodHandler> dispat
6767
final Timer.Sample sample = Timer.start(meterRegistry);
6868
Timer timer = null;
6969
try {
70-
try {
71-
final Object invoke = invocationHandle.invoke(proxy, method, args);
72-
timer = createTimer(target, method, args, null);
73-
return invoke;
74-
} catch (Exception e) {
75-
throw e;
76-
} catch (Throwable e) {
77-
throw new Exception(e);
78-
}
70+
final Object invoke = invocationHandle.invoke(proxy, method, args);
71+
timer = createTimer(target, method, args, null);
72+
return invoke;
7973
} catch (final FeignException e) {
8074
timer = createTimer(target, method, args, e);
8175
createFeignExceptionCounter(target, method, args, e).increment();
8276
throw e;
8377
} catch (final Throwable e) {
8478
timer = createTimer(target, method, args, e);
85-
createExceptionCounter(target, method, args, e).increment();
8679
throw e;
8780
} finally {
8881
if (timer == null) {
@@ -99,15 +92,6 @@ protected Timer createTimer(Target target, Method method, Object[] args, Throwab
9992
return meterRegistry.timer(metricName.name(e), allTags);
10093
}
10194

102-
protected Counter createExceptionCounter(Target target,
103-
Method method,
104-
Object[] args,
105-
Throwable e) {
106-
final Tag[] extraTags = extraTags(target, method, args, e);
107-
final Tags allTags = metricTagResolver.tag(target.type(), method, target.url(), e, extraTags);
108-
return meterRegistry.counter(metricName.name(e), allTags);
109-
}
110-
11195
protected Counter createFeignExceptionCounter(Target target,
11296
Method method,
11397
Object[] args,

micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
import static org.hamcrest.MatcherAssert.assertThat;
1717
import static org.hamcrest.Matchers.aMapWithSize;
18+
import static org.hamcrest.Matchers.equalTo;
1819
import static org.hamcrest.Matchers.notNullValue;
1920
import static org.junit.Assert.assertSame;
21+
import static org.junit.Assert.assertThrows;
2022
import static org.junit.Assert.fail;
2123
import java.util.Map;
2224
import java.util.concurrent.atomic.AtomicReference;
23-
import org.hamcrest.Matchers;
2425
import org.junit.Before;
2526
import org.junit.Test;
2627
import feign.Capability;
@@ -123,12 +124,30 @@ public void decoderPropagatesUncheckedException() {
123124
.addCapability(createMetricCapability())
124125
.target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class));
125126

126-
try {
127-
source.get("0x3456789");
128-
fail("Should throw NotFound exception");
129-
} catch (FeignException.NotFound e) {
130-
assertSame(notFound.get(), e);
131-
}
127+
FeignException.NotFound thrown =
128+
assertThrows(FeignException.NotFound.class, () -> source.get("0x3456789"));
129+
assertSame(notFound.get(), thrown);
130+
132131
}
133132

133+
134+
@Test
135+
public void shouldMetricCollectionWithCustomException() {
136+
final SimpleSource source = Feign.builder()
137+
.client((request, options) -> {
138+
throw new RuntimeException("Test error");
139+
})
140+
.addCapability(createMetricCapability())
141+
.target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class));
142+
143+
RuntimeException thrown = assertThrows(RuntimeException.class, () -> source.get("0x3456789"));
144+
assertThat(thrown.getMessage(), equalTo("Test error"));
145+
146+
assertThat(
147+
getMetric("exception", "exception_name", "RuntimeException", "method", "get"),
148+
notNullValue());
149+
}
150+
151+
152+
134153
}

0 commit comments

Comments
 (0)