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

Match request to prevent metric for each ID. #46

Merged
merged 1 commit into from
Mar 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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 {
Expand All @@ -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);
}
Expand All @@ -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<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;
}
}