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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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,9 +19,12 @@
import feign.Capability;
import feign.Client;
import feign.FeignException;
import feign.Request;
import feign.Response;
import feign.Util;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import java.io.IOException;

/** Wrap feign {@link Client} with metrics. */
public class MicrometerObservationCapability implements Capability {
Expand Down Expand Up @@ -57,9 +60,13 @@ public Client enrich(Client client) {

try {
Response response = client.execute(request, options);
finalizeObservation(feignContext, observation, null, response);
FeignException exception = null;

exception = getFeignExceptionOnErrorStatusCode(response, request);

finalizeObservation(feignContext, observation, exception, response);
return response;
} catch (FeignException ex) {
} catch (Exception ex) {
finalizeObservation(feignContext, observation, ex, null);
throw ex;
}
Expand All @@ -83,8 +90,15 @@ 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(
(response, ex) -> {
FeignException exception = null;

exception = getFeignExceptionOnErrorStatusCode(response, request);

finalizeObservation(feignContext, observation, exception, response);
});
} catch (Exception ex) {
finalizeObservation(feignContext, observation, ex, null);

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

private boolean isErrorStatusCode(int statusCode) {
return statusCode >= 400 && statusCode < 600;
}

private FeignException getFeignExceptionOnErrorStatusCode(Response response, Request request) {
try {
if (isErrorStatusCode(response.status())) {

if (response.body() != null) {
response =
response.toBuilder().body(Util.toByteArray(response.body().asInputStream())).build();
}

return FeignException.errorStatus(request.requestTemplate().method(), response);
}
} catch (IOException ignored) { // NOPMD
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
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;
import static com.github.tomakehurst.wiremock.client.WireMock.noContent;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.serverError;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
Expand All @@ -29,6 +32,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 +85,53 @@ 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 getTemplatedPathForUriWithExceptionBody(WireMockRuntimeInfo wmRuntimeInfo) {
stubFor(get(anyUrl()).willReturn(serverError().withBody("\"error\": \"Server Error\"")));

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

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

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

@Test
void getTemplatedPathForUriWithNoContent(WireMockRuntimeInfo wmRuntimeInfo) {
stubFor(get(anyUrl()).willReturn(noContent()));

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("none");
}

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

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

AsyncTestClient testClient =
asyncClientInstrumentedWithObservations(wmRuntimeInfo.getHttpBaseUrl());

try {
testClient.templated("1", "2").get();
} catch (ExecutionException e) {
assertThat(meterRegistry.get(METER_NAME).meter().getId().getTag("error"))
.isEqualTo("BadRequest");
}
}

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

Expand Down