From 852e018a350f6c4736c56f0089b545c6682b25ce Mon Sep 17 00:00:00 2001 From: Phil Winder Date: Wed, 15 Mar 2017 13:27:07 +0000 Subject: [PATCH] Match request to prevent metric for each ID. --- .../middleware/HTTPMonitoringInterceptor.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/works/weave/socks/orders/middleware/HTTPMonitoringInterceptor.java b/src/main/java/works/weave/socks/orders/middleware/HTTPMonitoringInterceptor.java index e8c5298..6d1f6a0 100644 --- a/src/main/java/works/weave/socks/orders/middleware/HTTPMonitoringInterceptor.java +++ b/src/main/java/works/weave/socks/orders/middleware/HTTPMonitoringInterceptor.java @@ -1,12 +1,17 @@ package works.weave.socks.orders.middleware; 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.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.Map; public class HTTPMonitoringInterceptor implements HandlerInterceptor { static final Histogram requestLatency = Histogram.build() @@ -20,6 +25,9 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor { @Value("${spring.application.name:orders}") private String serviceName; + @Autowired + private RequestMappingHandlerMapping requestMappingHandlerMapping; + @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { @@ -36,7 +44,7 @@ public void postHandle(HttpServletRequest httpServletRequest, HttpServletRespons requestLatency.labels( serviceName, httpServletRequest.getMethod(), - httpServletRequest.getServletPath(), + getMatchingURLPattern(httpServletRequest), Integer.toString(httpServletResponse.getStatus()) ).observe(seconds); } @@ -45,4 +53,21 @@ public void postHandle(HttpServletRequest httpServletRequest, HttpServletRespons public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } + + 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(); + break; + } + } + return res; + } }