Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #47 from microservices-demo/fix/monitoring-all-end…
Browse files Browse the repository at this point in the history
…points

Obtain url matchers for both spring and spring-boot endpoints.
  • Loading branch information
Phil Winder authored Mar 16, 2017
2 parents ff17627 + 1a62049 commit e97ec65
Showing 1 changed file with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<PatternsRequestCondition> 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 {
Expand All @@ -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
Expand All @@ -55,19 +70,30 @@ public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletRe
}

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;
}
}

0 comments on commit e97ec65

Please # to comment.