-
Notifications
You must be signed in to change notification settings - Fork 40.8k
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
MetricsFilter may create an unbounded number of metrics for requests with a templated URI that are not handled by Spring MVC #5875
Comments
The behaviour you've described isn't intended, and it doesn't match what's tested. In one of the tests there's a request mapping like this: Can you please provide a sample or testcase that illustrates the behaviour you have described? |
Then, perhaps, it is a consequence of using Jersey with SpringBoot (don't get me started on why we're using both...):
|
Thanks. That'll be it. We need to use Jersey's equivalent of Spring MVC's best matching pattern attribute (assuming it has such a thing). |
So does that mean https://github.com/spring-projects/spring-boot/blob/master/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricsFilter.java#L104 needs to become pluggable? |
Ok so we just need to make sure things are putting that attribute in, this same issue effect Grails 3 as well. So that way anything built on boot the metrics will work for assuming the attribute is set. |
Hi, |
I just ran into this with jersey as well. It didn't saturate our jvm. Just filled up 380GB on our graphite server. I'm disabling the filter using:
|
Adding the following bean to an application will correct the metrics for requests handled by Jersey: @Bean
public ResourceConfigCustomizer bestMatchingPatternResourceConfigCustomizer(
final JerseyProperties jerseyProperties) {
return new ResourceConfigCustomizer() {
@Override
public void customize(ResourceConfig config) {
String applicationPath = determineApplicationPath(jerseyProperties,
config);
config.register(new BestMatchingPatternContainerRequestFilter(
applicationPath));
}
private String determineApplicationPath(JerseyProperties jerseyProperties,
ResourceConfig config) {
if (StringUtils.hasText(jerseyProperties.getApplicationPath())) {
return jerseyProperties.getApplicationPath();
}
ApplicationPath applicationPath = AnnotationUtils
.findAnnotation(config.getClass(), ApplicationPath.class);
return applicationPath == null ? "" : applicationPath.value();
}
};
}
/**
* A {@link ContainerRequestFilter} that sets the
* {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} request attribute.
*/
private static final class BestMatchingPatternContainerRequestFilter
implements ContainerRequestFilter {
private final String applicationPath;
private BestMatchingPatternContainerRequestFilter(String applicationPath) {
this.applicationPath = applicationPath.startsWith("/") ? applicationPath
: "/" + applicationPath;
}
@Override
public void filter(ContainerRequestContext requestContext)
throws IOException {
String matchingPattern = extractMatchingPattern(requestContext);
requestContext.setProperty(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,
matchingPattern);
}
private String extractMatchingPattern(
ContainerRequestContext requestContext) {
ExtendedUriInfo extendedUriInfo = (ExtendedUriInfo) requestContext
.getUriInfo();
List<String> templates = new ArrayList<String>();
for (UriTemplate matchedTemplate : extendedUriInfo
.getMatchedTemplates()) {
String template = matchedTemplate.getTemplate();
templates.add(template.startsWith("/") ? template : "/" + template);
}
if (StringUtils.hasText(this.applicationPath)) {
templates.add(this.applicationPath);
}
Collections.reverse(templates);
return StringUtils.collectionToDelimitedString(templates, "");
}
} Some caveats:
|
The same happens for almost every HttpServlet used in Spring Boot application. MetricsFilter should create new keys only if it knows what mapping was used. |
@wilkinsona Just as a comment. I used this bean and it indeed solved the problem, but i got strange gauge metrics a la The problem is probably on our side since the |
The title of this issue should be changed as this also affects Apache CXF and other frameworks. |
The planned move to Micrometer-based metrics will get rid of the metrics filter. |
We've decided to align 1.5.x with 2.0's current behaviour and fallback to |
Great improvement for those who still stick to that version. Thanks! |
The MetricsFilter is unsuitable for REST microservices that are backing a large number of entities. In our case, it was trying to generate gauges and counters for /widget/1 to /widget/60000000. It eventually soaks the heap and causes the server to slow to a crawl as it spends most of its CPU cycles in GC. There should be a big red "caveat" bubble in the documentation that details this scenario.
Ideally, a Spring MVC controller metrics controller should be implemented that works similar to the DropWizard Jersey 2 implementation (plus some RequestMapping introspection), then the MetricsFilter could be disabled by default.
The text was updated successfully, but these errors were encountered: