|
@@ -1,35 +1,20 @@
|
|
|
package com.qmth.boot.api.interceptor.impl;
|
|
|
|
|
|
-import com.qmth.boot.api.config.AacConfig;
|
|
|
-import com.qmth.boot.api.config.ApiConfigService;
|
|
|
-import com.qmth.boot.api.config.ApiProperties;
|
|
|
import com.qmth.boot.api.constant.ApiConstant;
|
|
|
import com.qmth.boot.api.interceptor.AbstractInterceptor;
|
|
|
import com.qmth.boot.api.utils.RequestUtil;
|
|
|
import com.qmth.boot.core.logger.constant.LoggerConstant;
|
|
|
-import com.qmth.boot.core.metrics.service.MetricsService;
|
|
|
import com.qmth.boot.tools.uuid.FastUUID;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.MDC;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
@Component
|
|
|
public class MetricsInterceptor extends AbstractInterceptor implements ApiConstant, LoggerConstant {
|
|
|
|
|
|
- @Resource
|
|
|
- private ApiProperties apiProperties;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private ApiConfigService apiConfigService;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private MetricsService metricsService;
|
|
|
-
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
|
|
// 优先从header读取traceId,否则自动生成
|
|
@@ -47,12 +32,7 @@ public class MetricsInterceptor extends AbstractInterceptor implements ApiConsta
|
|
|
@Override
|
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
Exception ex) {
|
|
|
- AacConfig config = getAacConfig(apiProperties, apiConfigService, request, handler);
|
|
|
long timecost = getTimecost(request);
|
|
|
- // 接口开启统计且响应不为404时,提交记录
|
|
|
- if (config.isMetrics() && response.getStatus() != HttpStatus.NOT_FOUND.value()) {
|
|
|
- metricsService.record(request.getServletPath(), response.getStatus(), timecost);
|
|
|
- }
|
|
|
// 记录日志
|
|
|
MDC.put(MDC_CALLER, RequestUtil.getAttribute(request, ATTRIBUTE_CALLER, "-"));
|
|
|
log.info("{} {} {} {}ms", request.getMethod().toUpperCase(), request.getServletPath(), response.getStatus(),
|