|
@@ -1,117 +1,120 @@
|
|
|
package cn.com.qmth.examcloud.web.interceptor;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
-
|
|
|
+import cn.com.qmth.examcloud.web.actuator.MetricNames;
|
|
|
+import cn.com.qmth.examcloud.web.actuator.MetricRegistryHolder;
|
|
|
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
+import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
|
|
|
+import cn.com.qmth.examcloud.web.support.ApiInfo;
|
|
|
import com.codahale.metrics.Histogram;
|
|
|
import com.codahale.metrics.Meter;
|
|
|
import com.codahale.metrics.MetricRegistry;
|
|
|
import com.codahale.metrics.Timer;
|
|
|
import com.codahale.metrics.Timer.Context;
|
|
|
+import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
-import cn.com.qmth.examcloud.web.actuator.MetricNames;
|
|
|
-import cn.com.qmth.examcloud.web.actuator.MetricRegistryHolder;
|
|
|
-import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
-import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
|
|
|
-import cn.com.qmth.examcloud.web.support.ApiInfo;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
/**
|
|
|
* API统计拦截器<br>
|
|
|
* Timer 和 Histogram 只能使用一个<br>
|
|
|
* Timer 为最近耗时<br>
|
|
|
* Histogram 为全局耗时<br>
|
|
|
- *
|
|
|
*
|
|
|
* @author WANGWEI
|
|
|
* @date 2019年7月24日
|
|
|
* @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
*/
|
|
|
+@Deprecated
|
|
|
public class ApiStatisticInterceptor implements HandlerInterceptor {
|
|
|
|
|
|
- private static String algorithm;
|
|
|
-
|
|
|
- {
|
|
|
- // 耗时直方图算法:U:uniform ; B:biased
|
|
|
- algorithm = PropertyHolder.getString("$API.statistic.histograms.algorithm", "B");
|
|
|
- if (!(algorithm.equals("B") || algorithm.equals("U"))) {
|
|
|
- throw new RuntimeException("histograms algorithm must be 'U' or 'B'");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
|
|
- Object handler) throws Exception {
|
|
|
-
|
|
|
- ApiInfo apiInfo = (ApiInfo) request
|
|
|
- .getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
|
|
|
-
|
|
|
- if (null == apiInfo) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- // Timer 统计
|
|
|
- if (algorithm.equals("B")) {
|
|
|
- Timer timer = MetricRegistryHolder.getDefalut()
|
|
|
- .timer(MetricRegistry.name(MetricNames.API_TIMER.name(), apiInfo.getMapping()));
|
|
|
- Context ctx = timer.time();
|
|
|
- request.setAttribute(HttpServletRequestAttribute.$_METRICS_TIMER_CTX.name(), ctx);
|
|
|
- }
|
|
|
-
|
|
|
- if (algorithm.equals("U")) {
|
|
|
- request.setAttribute(
|
|
|
- HttpServletRequestAttribute.API_STATISTIC_INTERCEPTOR_START_TIME.name(),
|
|
|
- System.currentTimeMillis());
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
|
|
- Object handler, Exception ex) throws Exception {
|
|
|
-
|
|
|
- ApiInfo apiInfo = (ApiInfo) request
|
|
|
- .getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
|
|
|
-
|
|
|
- if (null == apiInfo) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // Timer 统计
|
|
|
- if (algorithm.equals("B")) {
|
|
|
- Context ctx = (Context) request
|
|
|
- .getAttribute(HttpServletRequestAttribute.$_METRICS_TIMER_CTX.name());
|
|
|
- if (null != ctx) {
|
|
|
- ctx.stop();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Histogram 统计
|
|
|
- if (algorithm.equals("U")) {
|
|
|
- Histogram histogram = MetricRegistryHolder.getDefalut().histogram(
|
|
|
- MetricRegistry.name(MetricNames.API_HISTOGRAM.name(), apiInfo.getMapping()));
|
|
|
- Long startTime = (Long) request.getAttribute(
|
|
|
- HttpServletRequestAttribute.API_STATISTIC_INTERCEPTOR_START_TIME.name());
|
|
|
- histogram.update(System.currentTimeMillis() - startTime);
|
|
|
- }
|
|
|
-
|
|
|
- // 失败速率
|
|
|
- Boolean hasException = (Boolean) request
|
|
|
- .getAttribute(HttpServletRequestAttribute.$_EXCEPTION_HAPPENED.name());
|
|
|
-
|
|
|
- if (null != hasException && hasException) {
|
|
|
- Meter apiErrorMeter = MetricRegistryHolder.getDefalut().meter(
|
|
|
- MetricRegistry.name(MetricNames.API_ERROR_METER.name(), apiInfo.getMapping()));
|
|
|
- apiErrorMeter.mark();
|
|
|
- }
|
|
|
-
|
|
|
- // 请求速率
|
|
|
- Meter apiMeter = MetricRegistryHolder.getDefalut()
|
|
|
- .meter(MetricRegistry.name(MetricNames.API_METER.name(), apiInfo.getMapping()));
|
|
|
- apiMeter.mark();
|
|
|
-
|
|
|
- }
|
|
|
+ private static boolean enable = false;
|
|
|
+
|
|
|
+ private static String algorithm;
|
|
|
+
|
|
|
+ static {
|
|
|
+ // init();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void init() {
|
|
|
+ // 耗时直方图算法:U:uniform ; B:biased
|
|
|
+ algorithm = PropertyHolder.getString("$API.statistic.histograms.algorithm", "B");
|
|
|
+ if (!(algorithm.equals("B") || algorithm.equals("U"))) {
|
|
|
+ throw new RuntimeException("histograms algorithm must be 'U' or 'B'");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
+ if (!enable) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ ApiInfo apiInfo = (ApiInfo) request
|
|
|
+ .getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
|
|
|
+
|
|
|
+ if (null == apiInfo) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Timer 统计
|
|
|
+ if (algorithm.equals("B")) {
|
|
|
+ Timer timer = MetricRegistryHolder.getDefalut()
|
|
|
+ .timer(MetricRegistry.name(MetricNames.API_TIMER.name(), apiInfo.getMapping()));
|
|
|
+ Context ctx = timer.time();
|
|
|
+ request.setAttribute(HttpServletRequestAttribute.$_METRICS_TIMER_CTX.name(), ctx);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (algorithm.equals("U")) {
|
|
|
+ request.setAttribute(
|
|
|
+ HttpServletRequestAttribute.API_STATISTIC_INTERCEPTOR_START_TIME.name(),
|
|
|
+ System.currentTimeMillis());
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
|
|
+ Object handler, Exception ex) throws Exception {
|
|
|
+ if (!enable) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ApiInfo apiInfo = (ApiInfo) request.getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
|
|
|
+ if (null == apiInfo) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Timer 统计
|
|
|
+ if (algorithm.equals("B")) {
|
|
|
+ Context ctx = (Context) request.getAttribute(HttpServletRequestAttribute.$_METRICS_TIMER_CTX.name());
|
|
|
+ if (null != ctx) {
|
|
|
+ ctx.stop();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Histogram 统计
|
|
|
+ if (algorithm.equals("U")) {
|
|
|
+ Histogram histogram = MetricRegistryHolder.getDefalut().histogram(MetricRegistry.name(MetricNames.API_HISTOGRAM.name(), apiInfo.getMapping()));
|
|
|
+ Long startTime = (Long) request.getAttribute(HttpServletRequestAttribute.API_STATISTIC_INTERCEPTOR_START_TIME.name());
|
|
|
+ histogram.update(System.currentTimeMillis() - startTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 失败速率
|
|
|
+ Boolean hasException = (Boolean) request.getAttribute(HttpServletRequestAttribute.$_EXCEPTION_HAPPENED.name());
|
|
|
+
|
|
|
+ if (null != hasException && hasException) {
|
|
|
+ Meter apiErrorMeter = MetricRegistryHolder.getDefalut().meter(
|
|
|
+ MetricRegistry.name(MetricNames.API_ERROR_METER.name(), apiInfo.getMapping()));
|
|
|
+ apiErrorMeter.mark();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 请求速率
|
|
|
+ Meter apiMeter = MetricRegistryHolder.getDefalut()
|
|
|
+ .meter(MetricRegistry.name(MetricNames.API_METER.name(), apiInfo.getMapping()));
|
|
|
+ apiMeter.mark();
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|