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;
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,12 +26,19 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
.register();
private static final String startTimeKey = "startTime";
@Value("${spring.application.name:carts}")
private String serviceName;
@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
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse
......@@ -41,12 +53,15 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
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 class HTTPMonitoringInterceptor implements HandlerInterceptor {
}
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();
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<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