Commit d0a55c9f authored by Phil Winder's avatar Phil Winder Committed by GitHub

Merge pull request #30 from microservices-demo/fix/monitor-all-endpoints

Obtain url matchers for both spring and spring-boot endpoints.
parents 620e51b5 63173045
...@@ -3,15 +3,20 @@ package works.weave.socks.cart.middleware; ...@@ -3,15 +3,20 @@ 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.Autowired;
import org.springframework.beans.factory.annotation.Value; 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.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.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; 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; import java.util.HashSet;
import java.util.Set;
public class HTTPMonitoringInterceptor implements HandlerInterceptor { public class HTTPMonitoringInterceptor implements HandlerInterceptor {
static final Histogram requestLatency = Histogram.build() static final Histogram requestLatency = Histogram.build()
...@@ -21,12 +26,19 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor { ...@@ -21,12 +26,19 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
.register(); .register();
private static final String startTimeKey = "startTime"; private static final String startTimeKey = "startTime";
@Value("${spring.application.name:carts}")
private String serviceName;
@Autowired @Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping; ResourceMappings mappings;
@Autowired
JpaHelper jpaHelper;
@Autowired
RepositoryRestConfiguration repositoryConfiguration;
@Autowired
ApplicationContext applicationContext;
@Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping;
private Set<PatternsRequestCondition> urlPatterns;
@Value("${spring.application.name:orders}")
private String serviceName;
@Override @Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse
...@@ -41,12 +53,15 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor { ...@@ -41,12 +53,15 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
long start = (long) httpServletRequest.getAttribute(startTimeKey); long start = (long) httpServletRequest.getAttribute(startTimeKey);
long elapsed = System.nanoTime() - start; long elapsed = System.nanoTime() - start;
double seconds = (double) elapsed / 1000000000.0; double seconds = (double) elapsed / 1000000000.0;
requestLatency.labels( String matchedUrl = getMatchingURLPattern(httpServletRequest);
serviceName, if (!matchedUrl.equals("")) {
httpServletRequest.getMethod(), requestLatency.labels(
getMatchingURLPattern(httpServletRequest), serviceName,
Integer.toString(httpServletResponse.getStatus()) httpServletRequest.getMethod(),
).observe(seconds); matchedUrl,
Integer.toString(httpServletResponse.getStatus())
).observe(seconds);
}
} }
@Override @Override
...@@ -55,19 +70,31 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor { ...@@ -55,19 +70,31 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
} }
private String getMatchingURLPattern(HttpServletRequest httpServletRequest) { private String getMatchingURLPattern(HttpServletRequest httpServletRequest) {
String res = httpServletRequest.getServletPath(); String res = "";
for (PatternsRequestCondition pattern : getUrlPatterns()) {
for (Map.Entry<RequestMappingInfo, HandlerMethod> item : requestMappingHandlerMapping if (pattern.getMatchingCondition(httpServletRequest) != null &&
.getHandlerMethods().entrySet()) { !httpServletRequest.getServletPath().equals("/error")) {
RequestMappingInfo mapping = item.getKey(); res = pattern.getMatchingCondition(httpServletRequest).getPatterns().iterator()
if (mapping.getPatternsCondition().getMatchingCondition(httpServletRequest) != null && .next();
mapping.getMethodsCondition().getMatchingCondition(httpServletRequest) !=
null) {
res = mapping.getPatternsCondition().getMatchingCondition(httpServletRequest)
.getPatterns().iterator().next();
break; break;
} }
} }
return res; return res;
} }
private Set<PatternsRequestCondition> 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;
}
} }
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