diff --git a/src/main/java/works/weave/socks/shipping/middleware/HTTPMonitoringInterceptor.java b/src/main/java/works/weave/socks/shipping/middleware/HTTPMonitoringInterceptor.java index 935606d..fc6d0f8 100644 --- a/src/main/java/works/weave/socks/shipping/middleware/HTTPMonitoringInterceptor.java +++ b/src/main/java/works/weave/socks/shipping/middleware/HTTPMonitoringInterceptor.java @@ -3,15 +3,20 @@ import io.prometheus.client.Histogram; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.web.method.HandlerMethod; +import org.springframework.context.ApplicationContext; +import org.springframework.data.rest.core.config.RepositoryRestConfiguration; +import org.springframework.data.rest.core.mapping.ResourceMappings; +import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping; +import org.springframework.data.rest.webmvc.support.JpaHelper; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.mvc.method.RequestMappingInfo; +import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.Map; +import java.util.HashSet; +import java.util.Set; public class HTTPMonitoringInterceptor implements HandlerInterceptor { static final Histogram requestLatency = Histogram.build() @@ -21,13 +26,20 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor { .register(); private static final String startTimeKey = "startTime"; - + @Autowired + ResourceMappings mappings; + @Autowired + JpaHelper jpaHelper; + @Autowired + RepositoryRestConfiguration repositoryConfiguration; + @Autowired + ApplicationContext applicationContext; + @Autowired + RequestMappingHandlerMapping requestMappingHandlerMapping; + private Set urlPatterns; @Value("${spring.application.name:orders}") private String serviceName; - @Autowired - private RequestMappingHandlerMapping requestMappingHandlerMapping; - @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { @@ -41,12 +53,15 @@ public void postHandle(HttpServletRequest httpServletRequest, HttpServletRespons long start = (long) httpServletRequest.getAttribute(startTimeKey); long elapsed = System.nanoTime() - start; double seconds = (double) elapsed / 1000000000.0; - requestLatency.labels( - serviceName, - httpServletRequest.getMethod(), - getMatchingURLPattern(httpServletRequest), - Integer.toString(httpServletResponse.getStatus()) - ).observe(seconds); + String matchedUrl = getMatchingURLPattern(httpServletRequest); + if (!matchedUrl.equals("")) { + requestLatency.labels( + serviceName, + httpServletRequest.getMethod(), + matchedUrl, + Integer.toString(httpServletResponse.getStatus()) + ).observe(seconds); + } } @Override @@ -55,19 +70,31 @@ public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletRe } private String getMatchingURLPattern(HttpServletRequest httpServletRequest) { - String res = httpServletRequest.getServletPath(); - - for (Map.Entry item : requestMappingHandlerMapping - .getHandlerMethods().entrySet()) { - RequestMappingInfo mapping = item.getKey(); - if (mapping.getPatternsCondition().getMatchingCondition(httpServletRequest) != null && - mapping.getMethodsCondition().getMatchingCondition(httpServletRequest) != - null) { - res = mapping.getPatternsCondition().getMatchingCondition(httpServletRequest) - .getPatterns().iterator().next(); + String res = ""; + for (PatternsRequestCondition pattern : getUrlPatterns()) { + if (pattern.getMatchingCondition(httpServletRequest) != null && + !httpServletRequest.getServletPath().equals("/error")) { + res = pattern.getMatchingCondition(httpServletRequest).getPatterns().iterator() + .next(); break; } } return res; } + + private Set getUrlPatterns() { + if (this.urlPatterns == null) { + this.urlPatterns = new HashSet<>(); + requestMappingHandlerMapping.getHandlerMethods().forEach((mapping, handlerMethod) -> + urlPatterns.add(mapping.getPatternsCondition())); + RepositoryRestHandlerMapping repositoryRestHandlerMapping = new + RepositoryRestHandlerMapping(mappings, repositoryConfiguration); + repositoryRestHandlerMapping.setJpaHelper(jpaHelper); + repositoryRestHandlerMapping.setApplicationContext(applicationContext); + repositoryRestHandlerMapping.afterPropertiesSet(); + repositoryRestHandlerMapping.getHandlerMethods().forEach((mapping, handlerMethod) -> + urlPatterns.add(mapping.getPatternsCondition())); + } + return this.urlPatterns; + } }