commit
						c88dcc25f7
					
				| @ -0,0 +1,105 @@ | ||||
| package com.gitee.sop.gatewaycommon.interceptor; | ||||
| 
 | ||||
| import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil; | ||||
| import com.gitee.sop.gatewaycommon.param.ApiParam; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||||
| import org.springframework.cloud.client.ServiceInstance; | ||||
| import org.springframework.cloud.gateway.route.Route; | ||||
| import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; | ||||
| import org.springframework.stereotype.Component; | ||||
| import org.springframework.web.server.ServerWebExchange; | ||||
| 
 | ||||
| import java.net.URI; | ||||
| import java.util.Iterator; | ||||
| import java.util.LinkedHashSet; | ||||
| 
 | ||||
| /** | ||||
|  * . | ||||
|  * | ||||
|  * @author linzhiqiang | ||||
|  * @version Revision 1.0.0 | ||||
|  * @版权: 版权所有 (c) 2011 | ||||
|  * @see: | ||||
|  * @创建日期: 2020/9/11 | ||||
|  * @功能说明: | ||||
|  */ | ||||
| @Component | ||||
| @ConditionalOnProperty(prefix = "sop.route.logger", name = "enabled", havingValue = "true", matchIfMissing = true) | ||||
| public class LoggerRouteInterceptor implements RouteInterceptor { | ||||
| 
 | ||||
|     private Logger logger = LoggerFactory.getLogger("ROUTELOGGER"); | ||||
| 
 | ||||
|     @Override | ||||
|     public void preRoute(RouteInterceptorContext context) { | ||||
|         ApiParam apiParam = context.getApiParam(); | ||||
|         ServerWebExchange serverWebExchange = (ServerWebExchange) context.getRequestContext(); | ||||
|         String logId = serverWebExchange.getAttribute(ServerWebExchange.LOG_ID_ATTRIBUTE); | ||||
|         logger.info( | ||||
|             "requestId_#$#_{}_|$|_requestIp_#$#_{}_|$|_interface_#$#_{}_|$|_requestParam_#$#_{}_|$|_requestHeaders_#$#_{}", | ||||
|             logId, apiParam.fetchIp(), apiParam.fetchNameVersion(), apiParam.toJSONString(), | ||||
|             serverWebExchange.getRequest().getHeaders().toString()); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void afterRoute(RouteInterceptorContext context) { | ||||
|         ApiParam apiParam = context.getApiParam(); | ||||
|         ServerWebExchange serverWebExchange = (ServerWebExchange) context.getRequestContext(); | ||||
|         String logId = serverWebExchange.getAttribute(ServerWebExchange.LOG_ID_ATTRIBUTE); | ||||
|         String routeId = apiParam.fetchNameVersion(); | ||||
|         long spendTime = context.getFinishTimeMillis() - context.getBeginTimeMillis(); | ||||
|         ServerWebExchange exchange = (ServerWebExchange) context.getRequestContext(); | ||||
|         Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); | ||||
|         URI uri = null; | ||||
|         LinkedHashSet<URI> hashSet = (LinkedHashSet<URI>) exchange | ||||
|             .getAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR); | ||||
|         if (hashSet != null) { | ||||
|             Iterator<URI> it = hashSet.iterator(); | ||||
|             if (it.hasNext()) { | ||||
|                 uri = it.next(); | ||||
|             } | ||||
|         } | ||||
|         String oriUrl = StringUtils.trim(uri == null ? "" : uri.toString()); | ||||
| 
 | ||||
|         URI dest = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR); | ||||
|         String destUrl = "unknown"; | ||||
|         if (dest != null) { | ||||
|             destUrl = dest.toString(); | ||||
|             if (destUrl.indexOf("?") > 0) { | ||||
|                 destUrl = destUrl.substring(0, destUrl.indexOf("?")); | ||||
|             } | ||||
|         } | ||||
|         String gRouteId = "unknown"; | ||||
|         String routeUri = "unknown"; | ||||
|         if (route != null) { | ||||
|             gRouteId = route.getId(); | ||||
|             routeUri = route.getUri().toString(); | ||||
|         } | ||||
|         ServiceInstance serviceInstance = context.getServiceInstance(); | ||||
|         String serviceId = "unknown"; | ||||
|         String instanceId = "unknown"; | ||||
|         if (serviceInstance != null) { | ||||
|             serviceId = context.getServiceInstance().getServiceId(); | ||||
|             instanceId = context.getServiceInstance().getInstanceId(); | ||||
|         } | ||||
|         Throwable throwable = ServerWebExchangeUtil.getThrowable(exchange); | ||||
|         String exceptionMsg = ""; | ||||
|         if (throwable != null) { | ||||
|             exceptionMsg = throwable.getMessage(); | ||||
|         } | ||||
|         String errorMsg = context.getServiceErrorMsg(); | ||||
|         logger.info( | ||||
|             "requestId_#$#_{}_|$|_requestResult_#$#_{}_|$|_coastTime_#$#_{}_|$|_interface_#$#_{}_|$|_gatewayRouteId_#$#_{}_|$|_oriUrl_#$#_{}_|$|_destUrl_#$#_{}_|$|_routeUri_#$#_{}_|$|_serviceId_#$#_{}_|$|_instanceId_#$#_{}_|$|_responeStatus_#$#_{}_|$|_responeResult_#$#_{}_|$|_responseHeaders_#$#_{}_|$|_error_#$#_{}", | ||||
|             logId, context.isSuccessRequest(), spendTime, routeId, gRouteId, oriUrl, destUrl, | ||||
|             routeUri, serviceId, instanceId, context.getResponseStatus(), | ||||
|             context.getServiceResult(), serverWebExchange.getResponse().getHeaders().toString(), | ||||
|             StringUtils.isNotBlank(exceptionMsg) ? exceptionMsg : StringUtils.isEmpty(errorMsg) ? "" : errorMsg); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public int getOrder() { | ||||
|         return 0; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,124 @@ | ||||
| package com.gitee.sop.gatewaycommon.interceptor; | ||||
| 
 | ||||
| import com.gitee.sop.gatewaycommon.param.ApiParam; | ||||
| import io.micrometer.core.instrument.DistributionSummary; | ||||
| import io.micrometer.core.instrument.MeterRegistry; | ||||
| import io.micrometer.core.instrument.Timer; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.cloud.gateway.route.Route; | ||||
| import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; | ||||
| import org.springframework.stereotype.Component; | ||||
| import org.springframework.web.server.ServerWebExchange; | ||||
| 
 | ||||
| import java.net.URI; | ||||
| import java.util.Iterator; | ||||
| import java.util.LinkedHashSet; | ||||
| import java.util.Objects; | ||||
| import java.util.concurrent.TimeUnit; | ||||
| 
 | ||||
| /** | ||||
|  * . | ||||
|  * | ||||
|  * @author linzhiqiang | ||||
|  * @version Revision 1.0.0 | ||||
|  * @版权: 版权所有 (c) 2011 | ||||
|  * @see: | ||||
|  * @创建日期: 2020/9/11 | ||||
|  * @功能说明: | ||||
|  */ | ||||
| @Component | ||||
| //@ConditionalOnClass(name = {"io.micrometer.core.instrument.MeterRegistry"})
 | ||||
| public class MetricRouteInterceptor implements RouteInterceptor { | ||||
| 
 | ||||
|     private Logger        logger = LoggerFactory.getLogger(MetricRouteInterceptor.class); | ||||
|     @Autowired | ||||
|     private MeterRegistry meterRegistry; | ||||
| 
 | ||||
|     public MetricRouteInterceptor() { | ||||
|         logger.info("初始化MetricRouteInterceptor"); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void preRoute(RouteInterceptorContext context) { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void afterRoute(RouteInterceptorContext context) { | ||||
|         ApiParam apiParam = context.getApiParam(); | ||||
|         String routeId = apiParam.fetchNameVersion(); | ||||
|         long spendTime = context.getFinishTimeMillis() - context.getBeginTimeMillis(); | ||||
|         ServerWebExchange exchange = (ServerWebExchange) context.getRequestContext(); | ||||
|         Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); | ||||
|         URI uri = null; | ||||
|         LinkedHashSet<URI> hashSet = (LinkedHashSet<URI>) exchange | ||||
|             .getAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR); | ||||
|         if (hashSet != null) { | ||||
|             Iterator<URI> it = hashSet.iterator(); | ||||
|             if (it.hasNext()) { | ||||
|                 uri = it.next(); | ||||
|             } | ||||
|         } | ||||
|         String oriUrl = StringUtils.trim(uri == null ? "" : uri.toString()); | ||||
|         if (oriUrl.indexOf("?") > 0) { | ||||
|             oriUrl = oriUrl.substring(0, oriUrl.indexOf("?")); | ||||
|         } | ||||
| 
 | ||||
|         URI dest = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR); | ||||
|         String destUrl = dest.toString(); | ||||
|         if (destUrl.indexOf("?") > 0) { | ||||
|             destUrl = destUrl.substring(0, destUrl.indexOf("?")); | ||||
|         } | ||||
|         String gRouteId = route.getId(); | ||||
|         String routeUri = route.getUri().toString(); | ||||
|         boolean isSuc = context.isSuccessRequest(); | ||||
|         if (meterRegistry != null) { | ||||
|             Timer timer = Timer.builder("sop.route.timer").description("SOP路由耗时统计") | ||||
|                 .tag("requestUri", exchange.getRequest().getURI().getRawPath()) | ||||
|                 .tag("routeId", routeId).tag("gRouteId", gRouteId).tag("routeUri", routeUri) | ||||
|                 .tag("oriUrl", oriUrl).tag("destUrl", destUrl).tag("serviceId", | ||||
|                     context.getServiceInstance() != null ? | ||||
|                         context.getServiceInstance().getServiceId() : | ||||
|                         "").tag("instanceId", context.getServiceInstance() != null ? | ||||
|                     context.getServiceInstance().getInstanceId() : | ||||
|                     "").tag("status", Objects.toString(context.getResponseStatus())) | ||||
|                 .tag("isSuccess", Objects.toString(isSuc)).publishPercentileHistogram() | ||||
|                 .register(meterRegistry); | ||||
|             timer.record(spendTime, TimeUnit.MILLISECONDS); | ||||
| 
 | ||||
|             DistributionSummary request = DistributionSummary.builder("sop.route.request.size") | ||||
|                 .scale(100).tag("requestUri", exchange.getRequest().getURI().getRawPath()) | ||||
|                 .tag("routeId", routeId).tag("gRouteId", gRouteId).tag("routeUri", routeUri) | ||||
|                 .tag("oriUrl", oriUrl).tag("destUrl", destUrl).tag("serviceId", | ||||
|                     context.getServiceInstance() != null ? | ||||
|                         context.getServiceInstance().getServiceId() : | ||||
|                         "").tag("instanceId", context.getServiceInstance() != null ? | ||||
|                     context.getServiceInstance().getInstanceId() : | ||||
|                     "").tag("status", Objects.toString(context.getResponseStatus())) | ||||
|                 .tag("isSuccess", Objects.toString(isSuc)).register(meterRegistry); | ||||
|             request.record(context.getRequestDataSize()); | ||||
| 
 | ||||
|             DistributionSummary response = DistributionSummary.builder("sop.route.response.size") | ||||
|                 .scale(100).tag("requestUri", exchange.getRequest().getURI().getRawPath()) | ||||
|                 .tag("routeId", routeId).tag("gRouteId", gRouteId).tag("routeUri", routeUri) | ||||
|                 .tag("oriUrl", oriUrl).tag("destUrl", destUrl).tag("serviceId", | ||||
|                     context.getServiceInstance() != null ? | ||||
|                         context.getServiceInstance().getServiceId() : | ||||
|                         "").tag("instanceId", context.getServiceInstance() != null ? | ||||
|                     context.getServiceInstance().getInstanceId() : | ||||
|                     "").tag("status", Objects.toString(context.getResponseStatus())) | ||||
|                 .tag("isSuccess", Objects.toString(isSuc)).publishPercentileHistogram() | ||||
|                 .register(meterRegistry); | ||||
|             response.record(context.getResponseDataSize()); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public int getOrder() { | ||||
|         return -2; | ||||
|     } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue