Skip to content

Commit

Permalink
添加各插件拦截请求前后的finest级别日志
Browse files Browse the repository at this point in the history
  • Loading branch information
lilai23 committed Mar 25, 2023
1 parent 643638f commit 96fcbc6
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,6 +81,7 @@ private Map<String, String> 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();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,6 +37,7 @@ public class DispatcherServletInterceptor extends AbstractInterceptor {

@Override
public ExecuteContext before(ExecuteContext context) {
LogUtils.printHttpRequestBeforePoint(context);
if (checkContext(context)) {
return context;
}
Expand All @@ -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];
Expand All @@ -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;
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -39,13 +40,15 @@ public AbstractDirectoryInterceptor() {

@Override
public ExecuteContext before(ExecuteContext context) {
LogUtils.printDubboRequestBeforePoint(context);
return context;
}

@Override
public ExecuteContext after(ExecuteContext context) {
context.changeResult(abstractDirectoryService.selectInvokers(context.getObject(), context.getArguments(),
context.getResult()));
LogUtils.printDubboRequestAfterPoint(context);
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 96fcbc6

Please sign in to comment.