Commit 620e51b5 authored by Phil Winder's avatar Phil Winder Committed by GitHub

Merge pull request #29 from microservices-demo/monitoring/match-request

Match request to prevent metric for each ID.
parents 1ef22f59 abd7e71f
package works.weave.socks.cart.middleware; package works.weave.socks.cart.middleware;
import io.prometheus.client.Histogram; import io.prometheus.client.Histogram;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.Map;
public class HTTPMonitoringInterceptor implements HandlerInterceptor { public class HTTPMonitoringInterceptor implements HandlerInterceptor {
static final Histogram requestLatency = Histogram.build() static final Histogram requestLatency = Histogram.build()
...@@ -20,6 +25,9 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor { ...@@ -20,6 +25,9 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
@Value("${spring.application.name:carts}") @Value("${spring.application.name:carts}")
private String serviceName; private String serviceName;
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Override @Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse, Object o) throws Exception { httpServletResponse, Object o) throws Exception {
...@@ -36,7 +44,7 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor { ...@@ -36,7 +44,7 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
requestLatency.labels( requestLatency.labels(
serviceName, serviceName,
httpServletRequest.getMethod(), httpServletRequest.getMethod(),
httpServletRequest.getServletPath(), getMatchingURLPattern(httpServletRequest),
Integer.toString(httpServletResponse.getStatus()) Integer.toString(httpServletResponse.getStatus())
).observe(seconds); ).observe(seconds);
} }
...@@ -45,4 +53,21 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor { ...@@ -45,4 +53,21 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse, Object o, Exception e) throws Exception { httpServletResponse, Object o, Exception e) throws Exception {
} }
private String getMatchingURLPattern(HttpServletRequest httpServletRequest) {
String res = httpServletRequest.getServletPath();
for (Map.Entry<RequestMappingInfo, HandlerMethod> 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;
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment