|
@@ -1,18 +1,16 @@
|
|
package cn.com.qmth.examcloud.web.interceptor;
|
|
package cn.com.qmth.examcloud.web.interceptor;
|
|
|
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
-
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
-import com.codahale.metrics.ConsoleReporter;
|
|
|
|
import com.codahale.metrics.Meter;
|
|
import com.codahale.metrics.Meter;
|
|
import com.codahale.metrics.MetricRegistry;
|
|
import com.codahale.metrics.MetricRegistry;
|
|
import com.codahale.metrics.Timer;
|
|
import com.codahale.metrics.Timer;
|
|
import com.codahale.metrics.Timer.Context;
|
|
import com.codahale.metrics.Timer.Context;
|
|
|
|
|
|
|
|
+import cn.com.qmth.examcloud.web.actuator.MetricRegistryHolder;
|
|
import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
|
|
import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
|
|
import cn.com.qmth.examcloud.web.support.ApiInfo;
|
|
import cn.com.qmth.examcloud.web.support.ApiInfo;
|
|
|
|
|
|
@@ -25,15 +23,6 @@ import cn.com.qmth.examcloud.web.support.ApiInfo;
|
|
*/
|
|
*/
|
|
public class StatisticInterceptor implements HandlerInterceptor {
|
|
public class StatisticInterceptor implements HandlerInterceptor {
|
|
|
|
|
|
- private MetricRegistry metricRegistry = new MetricRegistry();
|
|
|
|
-
|
|
|
|
- ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry)
|
|
|
|
- .convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
|
|
|
|
-
|
|
|
|
- public StatisticInterceptor() {
|
|
|
|
- reporter.start(1, TimeUnit.SECONDS);// 1s报道一次
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
|
Object handler) throws Exception {
|
|
Object handler) throws Exception {
|
|
@@ -45,7 +34,7 @@ public class StatisticInterceptor implements HandlerInterceptor {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- Timer timer = metricRegistry
|
|
|
|
|
|
+ Timer timer = MetricRegistryHolder.getDefalut()
|
|
.timer(MetricRegistry.name(apiInfo.getMapping(), apiInfo.getDescription()));
|
|
.timer(MetricRegistry.name(apiInfo.getMapping(), apiInfo.getDescription()));
|
|
Context ctx = timer.time();
|
|
Context ctx = timer.time();
|
|
request.setAttribute(HttpServletRequestAttribute.$_METRICS_TIMER_CTX.name(), ctx);
|
|
request.setAttribute(HttpServletRequestAttribute.$_METRICS_TIMER_CTX.name(), ctx);
|
|
@@ -69,7 +58,7 @@ public class StatisticInterceptor implements HandlerInterceptor {
|
|
.getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
|
|
.getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
|
|
|
|
|
|
if (null != apiInfo) {
|
|
if (null != apiInfo) {
|
|
- Meter meter = metricRegistry.meter(apiInfo.getMapping());
|
|
|
|
|
|
+ Meter meter = MetricRegistryHolder.getDefalut().meter(apiInfo.getMapping());
|
|
meter.mark();
|
|
meter.mark();
|
|
}
|
|
}
|
|
}
|
|
}
|