Любопытно, если кто-то может поделиться некоторыми отзывами о том, почему HttpServletRequest может не иметь определенных атрибутов, установленных в Spring Framework.Мы используем атрибут, отключенный от HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE.(https://docs.spring.io/spring-framework/docs/4.3.4.RELEASE/javadoc-api/org/springframework/web/servlet/HandlerMapping.html)
Пример кода находится в OncePerRequestFilter.doFilterInternal () после вызова цепочки.
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
String path = (new UrlPathHelper()).getPathWithinApplication(request);
chain.doFilter(request, response);
String scrubbedPath = scrubPath(request, path);
}
private String scrubPath(HttpServletRequest request, String path) {
String message = "Response Metric Info";
String scrubbedPath = path;
Object bestMatchingPattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
if (bestMatchingPattern != null) {
//This changes path from .../collection/{alias} to ../collection/alias
scrubbedPath = fixSpecialCharacters(bestMatchingPattern.toString());
} else {
// this is temporary to help with understanding why we get multiple metric names for the same path
message = "BEST_MATCHING_PATTERN_ATTRIBUTE null";
}
logHandlerInfo(message, request, path);
return scrubbedPath;
}
// temp helper method to understand why some metrics are being formatted differently for the same path
// causing multiple metrics for the same path
private void logHandlerInfo(String message, HttpServletRequest request, String path) {
final String LINE_SEPARATOR = System.lineSeparator();
StringBuilder metricInfoBuilder = new StringBuilder();
metricInfoBuilder.append(message);
metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("Input Path=%s", path));
metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.Path=%s", request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)));
metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.Best=%s", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)));
metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.Introspect=%s", request.getAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING)));
metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.UriTemplate=%s", request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)));
metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.Matrix=%s", request.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE)));
metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.MediaTypes=%s", request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE)));
log.info(metricInfoBuilder.toString());
}
Вот примеры журналов.
2018-10-08 20:51:58:300 [qtp1346560976-53746897] [] INFO c.e.m.e.metrics.MetricsFilter - Response Metric Info
Input Path=/v1/group/test/collection/test-dev/status
HandlerMapping.Path=/v1/group/test/collection/test-dev/status
HandlerMapping.Best=/v1/group/{groupAlias}/collection/{alias}/status
HandlerMapping.Introspect=null
HandlerMapping.UriTemplate={groupAlias=test, alias=test-dev}
2018-10-08 20:46:57:745 [qtp1346560976-53746897] [] INFO c.e.m.e.metrics.MetricsFilter - BEST_MATCHING_PATTERN_ATTRIBUTE null
Input Path=/v1/group/test/collection/test-dev/status
HandlerMapping.Path=null
HandlerMapping.Best=null
HandlerMapping.Introspect=null
HandlerMapping.UriTemplate=null