title: Spring HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE not always set
body: <p>Curious if someone can share some feedback on why a HttpServletRequest may not have specific attributes set from the Spring Framework. We are using the attribute keyed off of HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE. (<a href="https://docs.spring.io/spring-framework/docs/4.3.4.RELEASE/javadoc-api/org/springframework/web/servlet/HandlerMapping.html" rel="noreferrer">https://docs.spring.io/spring-framework/docs/4.3.4.RELEASE/javadoc-api/org/springframework/web/servlet/HandlerMapping.html</a>)</p>
  
  <p>The example code is within a OncePerRequestFilter.doFilterInternal() after calling the chain.</p>
  
  <pre><code>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());
  }
  </code></pre>
  
  <p>Here is the sample logs.</p>
  
  <pre><code>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
  </code></pre>