From 96fcbc6decddfedfc34f52ac43c55b2ad55e5531 Mon Sep 17 00:00:00 2001 From: lilai Date: Fri, 24 Mar 2023 11:00:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=90=84=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E8=AF=B7=E6=B1=82=E5=89=8D=E5=90=8E=E7=9A=84?= =?UTF-8?q?finest=E7=BA=A7=E5=88=AB=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sermant/core/utils/LogUtils.java | 96 +++++++++++++++++++ .../flowcontrol/AlibabaDubboInterceptor.java | 4 + .../flowcontrol/ApacheDubboInterceptor.java | 4 + .../DispatcherServletInterceptor.java | 4 + .../interceptor/AlibabaDubboInterceptor.java | 6 ++ .../interceptor/ApacheDubboInterceptor.java | 6 ++ .../DispatcherServletInterceptor.java | 6 ++ .../AbstractDirectoryInterceptor.java | 3 + .../ClientHttpRequestInterceptor.java | 4 + .../interceptor/FeignClientInterceptor.java | 4 + .../HttpAsyncClient4xInterceptor.java | 4 + .../interceptor/HttpClient4xInterceptor.java | 4 + .../HttpUrlConnectionConnectInterceptor.java | 4 + .../interceptor/OkHttp3ClientInterceptor.java | 4 + .../interceptor/OkHttpClientInterceptor.java | 4 + .../interceptor/RestTemplateInterceptor.java | 4 + .../interceptors/FeignInvokeInterceptor.java | 16 ++-- .../OkHttp3ClientInterceptor.java | 4 + .../interceptors/OkHttpClientInterceptor.java | 12 ++- .../interceptors/RestTemplateInterceptor.java | 14 ++- .../HttpAsyncClient4xInterceptor.java | 4 + .../httpclient/HttpClient4xInterceptor.java | 4 + ...rlConnectionResponseStreamInterceptor.java | 10 +- 23 files changed, 207 insertions(+), 18 deletions(-) create mode 100644 sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/LogUtils.java diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/LogUtils.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/LogUtils.java new file mode 100644 index 0000000000..538e891f57 --- /dev/null +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/LogUtils.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.sermant.core.utils; + +import com.huaweicloud.sermant.core.common.LoggerFactory; +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; + +import java.util.Locale; +import java.util.logging.Logger; + +/** + * 日志工具类 + * + * @author lilai + * @since 2023-03-24 + */ +public class LogUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(); + + private LogUtils() { + } + + /** + * 打印Dubbo请求拦截Before切面日志 + * + * @param context 拦截上下文 + */ + public static void printDubboRequestBeforePoint(ExecuteContext context) { + LOGGER.finest(() -> String.format(Locale.ROOT, "[Request Intercepted by Sermant][Before] Dubbo request: %s", + context)); + } + + /** + * 打印Dubbo请求拦截After切面日志 + * + * @param context 拦截上下文 + */ + public static void printDubboRequestAfterPoint(ExecuteContext context) { + LOGGER.finest(() -> String.format(Locale.ROOT, "[Request Intercepted by Sermant][After] Dubbo request: %s", + context)); + } + + /** + * 打印Dubbo请求拦截OnThrow切面日志 + * + * @param context 拦截上下文 + */ + public static void printDubboRequestOnThrowPoint(ExecuteContext context) { + LOGGER.finest(String.format(Locale.ROOT, "[Request Intercepted by Sermant][OnThrow] Dubbo request: %s", + context)); + } + + /** + * 打印HTTP请求拦截Before切面日志 + * + * @param context 拦截上下文 + */ + public static void printHttpRequestBeforePoint(ExecuteContext context) { + LOGGER.finest(String.format(Locale.ROOT, "[Request Intercepted by Sermant][Before] HTTP request: %s", + context)); + } + + /** + * 打印HTTP请求拦截After切面日志 + * + * @param context 拦截上下文 + */ + public static void printHttpRequestAfterPoint(ExecuteContext context) { + LOGGER.finest(String.format(Locale.ROOT, "[Request Intercepted by Sermant][After] HTTP request: %s", + context)); + } + + /** + * 打印HTTP请求拦截OnThrow切面日志 + * + * @param context 拦截上下文 + */ + public static void printHttpRequestOnThrowPoint(ExecuteContext context) { + LOGGER.finest(String.format(Locale.ROOT, "[Request Intercepted by Sermant][OnThrow] HTTP request: %s", + context)); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/AlibabaDubboInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/AlibabaDubboInterceptor.java index 1460d6ed07..b8a31a311a 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/AlibabaDubboInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/AlibabaDubboInterceptor.java @@ -28,6 +28,7 @@ import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.rpc.Invocation; @@ -97,6 +98,7 @@ private String getApplication(URL url, String interfaceName, boolean isProvider) @Override protected final ExecuteContext doBefore(ExecuteContext context) { + LogUtils.printDubboRequestBeforePoint(context); final Object[] allArguments = context.getArguments(); final FlowControlResult result = new FlowControlResult(); if (allArguments[1] instanceof Invocation) { @@ -156,12 +158,14 @@ protected final ExecuteContext doAfter(ExecuteContext context) { if (isProvider) { FlowControlContext.INSTANCE.clear(); } + LogUtils.printDubboRequestAfterPoint(context); return context; } @Override protected final ExecuteContext doThrow(ExecuteContext context) { chooseDubboService().onThrow(className, context.getThrowable(), isProvider(context)); + LogUtils.printDubboRequestOnThrowPoint(context); return context; } diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/ApacheDubboInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/ApacheDubboInterceptor.java index 258e9b1cf7..6de1f19755 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/ApacheDubboInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/ApacheDubboInterceptor.java @@ -28,6 +28,7 @@ import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.ReflectUtils; import org.apache.dubbo.common.URL; @@ -105,6 +106,7 @@ private String getApplication(URL url, String interfaceName, boolean isProvider) @Override protected final ExecuteContext doBefore(ExecuteContext context) { + LogUtils.printDubboRequestBeforePoint(context); final Object[] allArguments = context.getArguments(); if (allArguments[1] instanceof Invocation) { final FlowControlResult result = new FlowControlResult(); @@ -197,12 +199,14 @@ protected final ExecuteContext doAfter(ExecuteContext context) { if (isProvider) { FlowControlContext.INSTANCE.clear(); } + LogUtils.printDubboRequestAfterPoint(context); return context; } @Override protected final ExecuteContext doThrow(ExecuteContext context) { chooseDubboService().onThrow(className, context.getThrowable(), isProvider(context)); + LogUtils.printDubboRequestOnThrowPoint(context); return context; } diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/DispatcherServletInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/DispatcherServletInterceptor.java index 11fbf132cf..e3d47021f5 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/DispatcherServletInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/DispatcherServletInterceptor.java @@ -23,6 +23,7 @@ import com.huawei.flowcontrol.service.InterceptorSupporter; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.LogUtils; import java.util.Collections; import java.util.Enumeration; @@ -80,6 +81,7 @@ private Map getHeaders(HttpServletRequest request) { @Override protected final ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); final Object[] allArguments = context.getArguments(); final HttpServletRequest argument = (HttpServletRequest) allArguments[0]; final FlowControlResult result = new FlowControlResult(); @@ -102,12 +104,14 @@ protected final ExecuteContext doBefore(ExecuteContext context) throws Exception @Override protected final ExecuteContext doAfter(ExecuteContext context) { chooseHttpService().onAfter(className, context.getResult()); + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override protected final ExecuteContext doThrow(ExecuteContext context) { chooseHttpService().onThrow(className, context.getThrowable()); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } } diff --git a/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/AlibabaDubboInterceptor.java b/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/AlibabaDubboInterceptor.java index 8df9e5fdbb..5e268d479c 100644 --- a/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/AlibabaDubboInterceptor.java +++ b/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/AlibabaDubboInterceptor.java @@ -23,6 +23,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.alibaba.dubbo.rpc.Invocation; import com.alibaba.dubbo.rpc.Invoker; @@ -43,6 +44,7 @@ public class AlibabaDubboInterceptor extends AbstractInterceptor { @Override public ExecuteContext before(ExecuteContext context) { + LogUtils.printDubboRequestBeforePoint(context); if (context == null || context.getArguments() == null || context.getArguments().length < 1) { return context; } @@ -65,6 +67,7 @@ public ExecuteContext before(ExecuteContext context) { @Override public ExecuteContext after(ExecuteContext context) { if (context.getExtMemberFieldValue(MONITOR_NAME) == null) { + LogUtils.printDubboRequestAfterPoint(context); return context; } String name = (String) context.getExtMemberFieldValue(MONITOR_NAME); @@ -73,18 +76,21 @@ public ExecuteContext after(ExecuteContext context) { long startTime = (Long) context.getExtMemberFieldValue(START_TIME); metricCalEntity.getConsumeReqTimeNum().addAndGet(System.currentTimeMillis() - startTime); metricCalEntity.getSuccessFulReqNum().incrementAndGet(); + LogUtils.printDubboRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) { if (context.getExtMemberFieldValue(MONITOR_NAME) == null) { + LogUtils.printDubboRequestOnThrowPoint(context); return context; } String name = (String) context.getExtMemberFieldValue(MONITOR_NAME); MetricCalEntity metricCalEntity = MonitorCacheUtil.getMetricCalEntity(name); metricCalEntity.getFailedReqNum().incrementAndGet(); metricCalEntity.getReqNum().incrementAndGet(); + LogUtils.printDubboRequestOnThrowPoint(context); return context; } diff --git a/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/ApacheDubboInterceptor.java b/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/ApacheDubboInterceptor.java index 35aeee16e7..0bc1fe8e42 100644 --- a/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/ApacheDubboInterceptor.java +++ b/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/ApacheDubboInterceptor.java @@ -23,6 +23,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.utils.LogUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -43,6 +44,7 @@ public class ApacheDubboInterceptor extends AbstractInterceptor { @Override public ExecuteContext before(ExecuteContext context) { + LogUtils.printDubboRequestBeforePoint(context); if (context == null || context.getArguments() == null || context.getArguments().length < 1) { return context; } @@ -65,6 +67,7 @@ public ExecuteContext before(ExecuteContext context) { @Override public ExecuteContext after(ExecuteContext context) { if (context.getExtMemberFieldValue(MONITOR_NAME) == null) { + LogUtils.printDubboRequestAfterPoint(context); return context; } String name = (String) context.getExtMemberFieldValue(MONITOR_NAME); @@ -73,18 +76,21 @@ public ExecuteContext after(ExecuteContext context) { long startTime = (Long) context.getExtMemberFieldValue(START_TIME); metricCalEntity.getConsumeReqTimeNum().addAndGet(System.currentTimeMillis() - startTime); metricCalEntity.getSuccessFulReqNum().incrementAndGet(); + LogUtils.printDubboRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) { if (context.getExtMemberFieldValue(MONITOR_NAME) == null) { + LogUtils.printDubboRequestOnThrowPoint(context); return context; } String name = (String) context.getExtMemberFieldValue(MONITOR_NAME); MetricCalEntity metricCalEntity = MonitorCacheUtil.getMetricCalEntity(name); metricCalEntity.getFailedReqNum().incrementAndGet(); metricCalEntity.getReqNum().incrementAndGet(); + LogUtils.printDubboRequestOnThrowPoint(context); return context; } diff --git a/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/DispatcherServletInterceptor.java b/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/DispatcherServletInterceptor.java index e1416bf996..513f89da91 100644 --- a/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/DispatcherServletInterceptor.java +++ b/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/com/huawei/monitor/interceptor/DispatcherServletInterceptor.java @@ -22,6 +22,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.utils.LogUtils; import javax.servlet.http.HttpServletRequest; @@ -36,6 +37,7 @@ public class DispatcherServletInterceptor extends AbstractInterceptor { @Override public ExecuteContext before(ExecuteContext context) { + LogUtils.printHttpRequestBeforePoint(context); if (checkContext(context)) { return context; } @@ -46,6 +48,7 @@ public ExecuteContext before(ExecuteContext context) { @Override public ExecuteContext after(ExecuteContext context) { if (checkContext(context) || context.getExtMemberFieldValue(START_TIME) == null) { + LogUtils.printHttpRequestAfterPoint(context); return context; } HttpServletRequest request = (HttpServletRequest) context.getArguments()[0]; @@ -54,6 +57,7 @@ public ExecuteContext after(ExecuteContext context) { long startTime = (Long) context.getExtMemberFieldValue(START_TIME); metricCalEntity.getConsumeReqTimeNum().addAndGet(System.currentTimeMillis() - startTime); metricCalEntity.getSuccessFulReqNum().incrementAndGet(); + LogUtils.printHttpRequestAfterPoint(context); return context; } @@ -64,12 +68,14 @@ private static boolean checkContext(ExecuteContext context) { @Override public ExecuteContext onThrow(ExecuteContext context) { if (checkContext(context)) { + LogUtils.printHttpRequestOnThrowPoint(context); return context; } HttpServletRequest request = (HttpServletRequest) context.getArguments()[0]; MetricCalEntity metricCalEntity = MonitorCacheUtil.getMetricCalEntity(request.getRequestURI()); metricCalEntity.getReqNum().incrementAndGet(); metricCalEntity.getFailedReqNum().incrementAndGet(); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } } diff --git a/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/AbstractDirectoryInterceptor.java b/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/AbstractDirectoryInterceptor.java index c091a4d6c8..ec0b66e9cc 100644 --- a/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/AbstractDirectoryInterceptor.java +++ b/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/AbstractDirectoryInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; import com.huaweicloud.sermant.core.service.ServiceManager; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.router.dubbo.service.AbstractDirectoryService; /** @@ -39,6 +40,7 @@ public AbstractDirectoryInterceptor() { @Override public ExecuteContext before(ExecuteContext context) { + LogUtils.printDubboRequestBeforePoint(context); return context; } @@ -46,6 +48,7 @@ public ExecuteContext before(ExecuteContext context) { public ExecuteContext after(ExecuteContext context) { context.changeResult(abstractDirectoryService.selectInvokers(context.getObject(), context.getArguments(), context.getResult())); + LogUtils.printDubboRequestAfterPoint(context); return context; } } \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/ClientHttpRequestInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/ClientHttpRequestInterceptor.java index 5f6089bac4..bd687f7d5c 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/ClientHttpRequestInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/ClientHttpRequestInterceptor.java @@ -18,6 +18,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.router.common.request.RequestData; import com.huaweicloud.sermant.router.common.request.RequestTag; import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils; @@ -39,6 +40,7 @@ public class ClientHttpRequestInterceptor extends AbstractInterceptor { @Override public ExecuteContext before(ExecuteContext context) { + LogUtils.printHttpRequestBeforePoint(context); Object obj = context.getObject(); if (obj instanceof HttpRequest) { HttpRequest request = (HttpRequest) obj; @@ -53,12 +55,14 @@ public ExecuteContext before(ExecuteContext context) { @Override public ExecuteContext after(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java index 1a69f362fc..6286feb25f 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java @@ -18,6 +18,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.request.RequestData; import com.huaweicloud.sermant.router.common.request.RequestTag; @@ -60,6 +61,7 @@ public FeignClientInterceptor() { @Override public ExecuteContext before(ExecuteContext context) { + LogUtils.printHttpRequestBeforePoint(context); Object argument = context.getArguments()[0]; if (argument instanceof Request) { Request request = (Request) argument; @@ -74,12 +76,14 @@ public ExecuteContext before(ExecuteContext context) { @Override public ExecuteContext after(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpAsyncClient4xInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpAsyncClient4xInterceptor.java index a6f11a79da..cd17e64f1b 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpAsyncClient4xInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpAsyncClient4xInterceptor.java @@ -17,6 +17,7 @@ package com.huaweicloud.sermant.router.spring.interceptor; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.request.RequestData; import com.huaweicloud.sermant.router.common.utils.FlowContextUtils; @@ -47,6 +48,7 @@ public class HttpAsyncClient4xInterceptor extends MarkInterceptor { */ @Override public ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); Object httpAsyncRequestProducerArgument = context.getArguments()[0]; if (httpAsyncRequestProducerArgument instanceof HttpAsyncRequestProducer) { HttpAsyncRequestProducer httpAsyncRequestProducer @@ -85,12 +87,14 @@ private void parseTags(HttpContext httpContext, HttpRequest httpRequest) { */ @Override public ExecuteContext after(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpClient4xInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpClient4xInterceptor.java index d023b0d357..3d298069b1 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpClient4xInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpClient4xInterceptor.java @@ -17,6 +17,7 @@ package com.huaweicloud.sermant.router.spring.interceptor; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.request.RequestData; import com.huaweicloud.sermant.router.common.utils.FlowContextUtils; @@ -48,6 +49,7 @@ public class HttpClient4xInterceptor extends MarkInterceptor { */ @Override public ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); Object httpRequestObject = context.getArguments()[1]; if (httpRequestObject instanceof HttpRequest) { final HttpRequest httpRequest = (HttpRequest) httpRequestObject; @@ -86,12 +88,14 @@ public ExecuteContext doBefore(ExecuteContext context) throws Exception { @Override public ExecuteContext after(ExecuteContext context) throws Exception { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpUrlConnectionConnectInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpUrlConnectionConnectInterceptor.java index 9ba025d403..2066d6bc1d 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpUrlConnectionConnectInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/HttpUrlConnectionConnectInterceptor.java @@ -18,6 +18,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.ReflectUtils; import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.request.RequestData; @@ -42,6 +43,7 @@ public class HttpUrlConnectionConnectInterceptor extends AbstractInterceptor { @Override public ExecuteContext before(ExecuteContext context) { + LogUtils.printHttpRequestBeforePoint(context); if (context.getObject() instanceof HttpURLConnection) { HttpURLConnection connection = (HttpURLConnection) context.getObject(); Optional requests = ReflectUtils.getFieldValue(connection, "requests"); @@ -77,12 +79,14 @@ private String getPath(HttpURLConnection connection) { @Override public ExecuteContext after(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) throws Exception { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestOnThrowPoint(context); return super.onThrow(context); } } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/OkHttp3ClientInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/OkHttp3ClientInterceptor.java index 3e11ff65ab..4181f6eaa8 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/OkHttp3ClientInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/OkHttp3ClientInterceptor.java @@ -17,6 +17,7 @@ package com.huaweicloud.sermant.router.spring.interceptor; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.ReflectUtils; import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.request.RequestData; @@ -49,6 +50,7 @@ public class OkHttp3ClientInterceptor extends MarkInterceptor { */ @Override public ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); final Optional rawRequest = getRequest(context); if (!rawRequest.isPresent() || StringUtils.isBlank(FlowContextUtils.getTagName())) { return context; @@ -74,12 +76,14 @@ public ExecuteContext doBefore(ExecuteContext context) throws Exception { @Override public ExecuteContext after(ExecuteContext context) throws Exception { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/OkHttpClientInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/OkHttpClientInterceptor.java index c4c9790ca1..f4a00fdcde 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/OkHttpClientInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/OkHttpClientInterceptor.java @@ -17,6 +17,7 @@ package com.huaweicloud.sermant.router.spring.interceptor; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.ReflectUtils; import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.request.RequestData; @@ -49,6 +50,7 @@ public class OkHttpClientInterceptor extends MarkInterceptor { */ @Override public ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); final Optional rawRequest = getRequest(context); if (!rawRequest.isPresent() || StringUtils.isBlank(FlowContextUtils.getTagName())) { return context; @@ -74,12 +76,14 @@ public ExecuteContext doBefore(ExecuteContext context) throws Exception { @Override public ExecuteContext after(ExecuteContext context) throws Exception { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/RestTemplateInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/RestTemplateInterceptor.java index 31508d915b..6d5daa7cd0 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/RestTemplateInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/RestTemplateInterceptor.java @@ -17,6 +17,7 @@ package com.huaweicloud.sermant.router.spring.interceptor; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.request.RequestData; import com.huaweicloud.sermant.router.common.utils.FlowContextUtils; @@ -42,6 +43,7 @@ public class RestTemplateInterceptor extends MarkInterceptor { @Override public ExecuteContext doBefore(ExecuteContext context) { + LogUtils.printHttpRequestBeforePoint(context); Object[] arguments = context.getArguments(); if (arguments != null && arguments.length > CALLBACK_ARG_LENGTH) { @@ -84,12 +86,14 @@ private RequestData getRequestData(Map> tags, Object url, O @Override public ExecuteContext after(ExecuteContext context) throws Exception { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) throws Exception { ThreadLocalUtils.removeRequestData(); + LogUtils.printHttpRequestOnThrowPoint(context); return context; } } diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java index 66d23623f4..308d47bf1f 100644 --- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java +++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java @@ -25,6 +25,7 @@ import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; +import com.huaweicloud.sermant.core.utils.LogUtils; import feign.Request; @@ -45,6 +46,7 @@ public class FeignInvokeInterceptor extends MarkInterceptor { @Override protected ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); final InvokerService invokerService = PluginServiceManager.getPluginService(InvokerService.class); Request request = (Request) context.getArguments()[0]; Map urlInfo = RequestInterceptorUtils.recoverUrl(request.url()); @@ -54,9 +56,9 @@ protected ExecuteContext doBefore(ExecuteContext context) throws Exception { } RequestInterceptorUtils.printRequestLog("feign", urlInfo); Optional result = invokerService.invoke( - buildInvokerFunc(context, request, urlInfo), - ex -> ex, - urlInfo.get(HttpConstants.HTTP_URI_SERVICE)); + buildInvokerFunc(context, request, urlInfo), + ex -> ex, + urlInfo.get(HttpConstants.HTTP_URI_SERVICE)); if (result.isPresent()) { Object obj = result.get(); if (obj instanceof Exception) { @@ -70,22 +72,24 @@ protected ExecuteContext doBefore(ExecuteContext context) throws Exception { } private Function buildInvokerFunc(ExecuteContext context, Request request, - Map urlInfo) { + Map urlInfo) { return invokerContext -> { context.getArguments()[0] = Request.create(request.method(), - RequestInterceptorUtils.buildUrl(urlInfo, invokerContext.getServiceInstance()), - request.headers(), request.body(), request.charset()); + RequestInterceptorUtils.buildUrl(urlInfo, invokerContext.getServiceInstance()), + request.headers(), request.body(), request.charset()); return RequestInterceptorUtils.buildFunc(context, invokerContext).get(); }; } @Override public ExecuteContext after(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestOnThrowPoint(context); return context; } } diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java index dc1ae2da61..f356c03cc5 100644 --- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java +++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java @@ -26,6 +26,7 @@ import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.ReflectUtils; import okhttp3.OkHttpClient; @@ -57,6 +58,7 @@ public class OkHttp3ClientInterceptor extends MarkInterceptor { @Override protected ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); final InvokerService invokerService = PluginServiceManager.getPluginService(InvokerService.class); final Optional rawRequest = getRequest(context); if (!rawRequest.isPresent()) { @@ -156,11 +158,13 @@ private Object buildErrorResponse(Throwable ex, Request request) { @Override public ExecuteContext after(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestOnThrowPoint(context); return context; } } diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java index ebcee18e22..6110e9b0f5 100644 --- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java +++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java @@ -26,6 +26,7 @@ import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; +import com.huaweicloud.sermant.core.utils.LogUtils; import com.huaweicloud.sermant.core.utils.ReflectUtils; import com.squareup.okhttp.HttpUrl; @@ -59,6 +60,7 @@ public class OkHttpClientInterceptor extends MarkInterceptor { @Override protected ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); final InvokerService invokerService = PluginServiceManager.getPluginService(InvokerService.class); final Optional rawRequest = getRequest(context); if (!rawRequest.isPresent()) { @@ -77,9 +79,9 @@ protected ExecuteContext doBefore(ExecuteContext context) throws Exception { AtomicReference rebuildRequest = new AtomicReference<>(); rebuildRequest.set(request); invokerService.invoke( - buildInvokerFunc(uri, hostAndPath, request, rebuildRequest, context), - buildExFunc(rebuildRequest), - hostAndPath.get(HttpConstants.HTTP_URI_SERVICE)) + buildInvokerFunc(uri, hostAndPath, request, rebuildRequest, context), + buildExFunc(rebuildRequest), + hostAndPath.get(HttpConstants.HTTP_URI_SERVICE)) .ifPresent(o -> setResultOrThrow(context, o, uri.getPath())); return context; } @@ -110,7 +112,7 @@ private Function buildExFunc(AtomicReference rebuild } private Function buildInvokerFunc(URI uri, Map hostAndPath, - Request request, AtomicReference rebuildRequest, ExecuteContext context) { + Request request, AtomicReference rebuildRequest, ExecuteContext context) { return invokerContext -> { final String method = request.method(); Request newRequest = covertRequest(uri, hostAndPath, request, method, invokerContext.getServiceInstance()); @@ -161,11 +163,13 @@ private Object buildErrorResponse(Throwable ex, Request request) { @Override public ExecuteContext after(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestOnThrowPoint(context); return context; } } diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java index c64b0f6bce..033e8ad251 100644 --- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java +++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java @@ -25,6 +25,7 @@ import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; +import com.huaweicloud.sermant.core.utils.LogUtils; import org.springframework.http.HttpMethod; @@ -47,6 +48,7 @@ public class RestTemplateInterceptor extends MarkInterceptor { @Override protected ExecuteContext doBefore(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestBeforePoint(context); final InvokerService invokerService = PluginServiceManager.getPluginService(InvokerService.class); URI uri = (URI) context.getArguments()[0]; HttpMethod httpMethod = (HttpMethod) context.getArguments()[1]; @@ -59,9 +61,9 @@ protected ExecuteContext doBefore(ExecuteContext context) throws Exception { } RequestInterceptorUtils.printRequestLog("restTemplate", hostAndPath); Optional result = invokerService.invoke( - buildInvokerFunc(uri, hostAndPath, context, httpMethod), - ex -> ex, - hostAndPath.get(HttpConstants.HTTP_URI_SERVICE)); + buildInvokerFunc(uri, hostAndPath, context, httpMethod), + ex -> ex, + hostAndPath.get(HttpConstants.HTTP_URI_SERVICE)); if (result.isPresent()) { Object obj = result.get(); if (obj instanceof Exception) { @@ -95,10 +97,10 @@ private Optional formatUri(String url, URI uri) { } private Function buildInvokerFunc(URI uri, Map hostAndPath, - ExecuteContext context, HttpMethod httpMethod) { + ExecuteContext context, HttpMethod httpMethod) { return invokerContext -> { String url = RequestInterceptorUtils.buildUrlWithIp(uri, invokerContext.getServiceInstance(), - hostAndPath.get(HttpConstants.HTTP_URI_PATH), httpMethod.name()); + hostAndPath.get(HttpConstants.HTTP_URI_PATH), httpMethod.name()); context.getArguments()[0] = rebuildUri(url, uri); return RequestInterceptorUtils.buildFunc(context, invokerContext).get(); }; @@ -106,11 +108,13 @@ private Function buildInvokerFunc(URI uri, Map result = invokerService.invoke( - invokerContextObjectFunction, - ex -> ex, - urlInfo.get(HttpConstants.HTTP_URI_SERVICE)); + invokerContextObjectFunction, + ex -> ex, + urlInfo.get(HttpConstants.HTTP_URI_SERVICE)); if (result.isPresent()) { Object obj = result.get(); if (obj instanceof Exception) { @@ -184,11 +186,13 @@ private void resetHttpClient(Object target, URL url) { @Override public ExecuteContext after(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestAfterPoint(context); return context; } @Override public ExecuteContext onThrow(ExecuteContext context) throws Exception { + LogUtils.printHttpRequestOnThrowPoint(context); return context; } }