-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
set root cause as the exception_name in micrometer tags #1883
Changes from 8 commits
c989a17
c57a599
9841388
85066f2
6244dc3
0a67e8a
4fcc79e
d92d77b
23cb5ed
73a1cc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2012-2022 The Feign Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package feign.utils; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class ExceptionUtils { | ||
/** | ||
* Introspects the {@link Throwable} to obtain the root cause. | ||
* | ||
* <p> | ||
* This method walks through the exception chain to the last element, "root" of the tree, using | ||
* {@link Throwable#getCause()}, and returns that exception. | ||
* | ||
* @param throwable the throwable to get the root cause for, may be null | ||
* @return the root cause of the {@link Throwable}, {@code null} if null throwable input | ||
*/ | ||
public static Throwable getRootCause(Throwable throwable) { | ||
if (throwable == null) { | ||
return null; | ||
} | ||
Throwable rootCause = throwable; | ||
// this is to avoid infinite loops for recursive cases | ||
final Set<Throwable> seenThrowables = new HashSet<>(); | ||
seenThrowables.add(rootCause); | ||
while ((rootCause.getCause() != null && !seenThrowables.contains(rootCause.getCause()))) { | ||
seenThrowables.add(rootCause.getCause()); | ||
rootCause = rootCause.getCause(); | ||
} | ||
return rootCause; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2012-2022 The Feign Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package feign.utils; | ||
|
||
import org.junit.Test; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ExceptionUtilsTest { | ||
@Test | ||
public void rootCauseOfNullIsNull() { | ||
Throwable e = null; | ||
Throwable rootCause = ExceptionUtils.getRootCause(e); | ||
assertThat(rootCause).isNull(); | ||
} | ||
|
||
@Test | ||
public void rootCauseIsSelf() { | ||
Throwable e = new Exception(); | ||
Throwable rootCause = ExceptionUtils.getRootCause(e); | ||
assertThat(rootCause).isSameAs(e); | ||
} | ||
|
||
@Test | ||
public void rootCauseIsDifferent() { | ||
Throwable rootCause = new Exception(); | ||
Throwable e = new Exception(rootCause); | ||
Throwable actualRootCause = ExceptionUtils.getRootCause(e); | ||
assertThat(actualRootCause).isSameAs(rootCause); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import feign.Response; | ||
import feign.codec.DecodeException; | ||
import feign.codec.Decoder; | ||
import feign.utils.ExceptionUtils; | ||
import io.dropwizard.metrics5.MetricRegistry; | ||
import io.dropwizard.metrics5.Timer.Context; | ||
|
||
|
@@ -63,14 +64,14 @@ public Object decode(Response response, Type type) | |
} catch (IOException | RuntimeException e) { | ||
metricRegistry.meter( | ||
metricName.metricName(template.methodMetadata(), template.feignTarget(), "error_count") | ||
.tagged("exception_name", e.getClass().getSimpleName()) | ||
.tagged("exception_name", ExceptionUtils.getRootCause(e).getClass().getSimpleName()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking a bit more about this.... we are breaking the existing contract with this change. Can we leave Cheers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that makes more sense, made the same change in 23cb5ed , can you take a look at it again? |
||
.tagged("uri", template.methodMetadata().template().path()), | ||
metricSuppliers.meters()).mark(); | ||
throw e; | ||
} catch (Exception e) { | ||
metricRegistry.meter( | ||
metricName.metricName(template.methodMetadata(), template.feignTarget(), "error_count") | ||
.tagged("exception_name", e.getClass().getSimpleName()) | ||
.tagged("exception_name", ExceptionUtils.getRootCause(e).getClass().getSimpleName()) | ||
.tagged("uri", template.methodMetadata().template().path()), | ||
metricSuppliers.meters()).mark(); | ||
throw new IOException(e); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getCause
can return null, in that case would cause NPEThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as e is not null, getRootCause won’t return null as we can see here
https://github.com/OpenFeign/feign/pull/1883/files#diff-3bfdf878b3ea38d5268a9d72389c1f54ebabf1d76ad9a308e1eed93d95ed0048R38
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ow, awesome, great!