Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed feign-micrometer exception handling. #2644

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import feign.Capability;
import feign.Client;
import feign.FeignException;
import feign.Request;
import feign.Response;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
Expand Down Expand Up @@ -57,9 +58,12 @@ public Client enrich(Client client) {

try {
Response response = client.execute(request, options);

throwExceptionOnErrorStatusCode(response, request);

finalizeObservation(feignContext, observation, null, response);
return response;
} catch (FeignException ex) {
} catch (Exception ex) {
finalizeObservation(feignContext, observation, ex, null);
throw ex;
}
Expand All @@ -83,8 +87,16 @@ public AsyncClient<Object> enrich(AsyncClient<Object> client) {
try {
return client
.execute(feignContext.getCarrier(), options, context)
.whenComplete((r, ex) -> finalizeObservation(feignContext, observation, ex, r));
} catch (FeignException ex) {
.whenComplete(
(r, ex) -> {
try {
throwExceptionOnErrorStatusCode(r, request);
finalizeObservation(feignContext, observation, ex, r);
} catch (FeignException statusCodeException) {
finalizeObservation(feignContext, observation, statusCodeException, null);
}
});
} catch (Exception ex) {
finalizeObservation(feignContext, observation, ex, null);

throw ex;
Expand All @@ -100,4 +112,10 @@ private void finalizeObservation(
}
observation.stop();
}

private void throwExceptionOnErrorStatusCode(Response response, Request request) {
if (response.status() >= 400 && response.status() < 600) {
throw FeignException.errorStatus(request.requestTemplate().method(), response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package feign.micrometer;

import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.badRequest;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
Expand All @@ -29,6 +30,7 @@
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import feign.AsyncFeign;
import feign.Feign;
import feign.FeignException;
import feign.Param;
import feign.Request;
import feign.RequestLine;
Expand Down Expand Up @@ -81,6 +83,22 @@ void getTemplatedPathForUri(WireMockRuntimeInfo wmRuntimeInfo) {
assertTags();
}

@Test
void getTemplatedPathForUriWithException(WireMockRuntimeInfo wmRuntimeInfo) {
stubFor(get(anyUrl()).willReturn(badRequest()));

TestClient testClient = clientInstrumentedWithObservations(wmRuntimeInfo.getHttpBaseUrl());

try {
testClient.templated("1", "2");
} catch (FeignException e) {
assertThat(e).isInstanceOf(FeignException.BadRequest.class);
}

assertThat(meterRegistry.get(METER_NAME).meter().getId().getTag("error"))
.isEqualTo("BadRequest");
}

@Test
void getTemplatedPathForUriForAsync(WireMockRuntimeInfo wmRuntimeInfo)
throws ExecutionException, InterruptedException {
Expand All @@ -98,6 +116,21 @@ void getTemplatedPathForUriForAsync(WireMockRuntimeInfo wmRuntimeInfo)
assertTags();
}

@Test
void getTemplatedPathForUriForAsyncWithException(WireMockRuntimeInfo wmRuntimeInfo) {
stubFor(get(anyUrl()).willReturn(badRequest()));

AsyncTestClient testClient =
asyncClientInstrumentedWithObservations(wmRuntimeInfo.getHttpBaseUrl());
testClient
.templated("1", "2")
.whenComplete(
(s, throwable) -> {
assertThat(meterRegistry.get(METER_NAME).meter().getId().getTag("error"))
.isEqualTo("BadRequest");
});
}

private void assertTags() {
Id requestsId = meterRegistry.get(METER_NAME).meter().getId();

Expand Down