diff --git a/src/main/java/neatlogic/framework/asynchronization/thread/NeatLogicThread.java b/src/main/java/neatlogic/framework/asynchronization/thread/NeatLogicThread.java index b604dae6501e4001c230736c3cbbd246e75fae7e..5306e1a922c2f36ff510e363998b5a2929fd84cc 100644 --- a/src/main/java/neatlogic/framework/asynchronization/thread/NeatLogicThread.java +++ b/src/main/java/neatlogic/framework/asynchronization/thread/NeatLogicThread.java @@ -17,6 +17,7 @@ package neatlogic.framework.asynchronization.thread; import neatlogic.framework.asynchronization.threadlocal.InputFromContext; +import neatlogic.framework.asynchronization.threadlocal.RequestContext; import neatlogic.framework.asynchronization.threadlocal.TenantContext; import neatlogic.framework.asynchronization.threadlocal.UserContext; import neatlogic.framework.cache.threadlocal.CacheContext; @@ -30,6 +31,7 @@ public abstract class NeatLogicThread implements Runnable { protected UserContext userContext; protected TenantContext tenantContext; protected InputFromContext inputFromContext; + protected RequestContext requestContext; private String threadName; private boolean isUnique = false; @@ -50,6 +52,7 @@ public abstract class NeatLogicThread implements Runnable { userContext = UserContext.get(); tenantContext = TenantContext.get(); inputFromContext = InputFromContext.get(); + requestContext = RequestContext.get(); this.threadName = _threadName; } @@ -57,6 +60,7 @@ public abstract class NeatLogicThread implements Runnable { userContext = UserContext.get(); tenantContext = TenantContext.get(); inputFromContext = InputFromContext.get(); + requestContext = RequestContext.get(); this.threadName = _threadName; this.isUnique = _isUnique; } @@ -66,6 +70,7 @@ public abstract class NeatLogicThread implements Runnable { TenantContext.init(tenantContext); UserContext.init(userContext); InputFromContext.init(inputFromContext); + RequestContext.init(requestContext); try { String oldThreadName = Thread.currentThread().getName(); if (StringUtils.isNotBlank(threadName)) { @@ -90,6 +95,9 @@ public abstract class NeatLogicThread implements Runnable { if (InputFromContext.get() != null) { InputFromContext.get().release(); } + if (RequestContext.get() != null) { + RequestContext.get().release(); + } CacheContext.release(); } } @@ -111,4 +119,4 @@ public abstract class NeatLogicThread implements Runnable { public String getThreadName() { return threadName; } -} \ No newline at end of file +} diff --git a/src/main/java/neatlogic/framework/asynchronization/threadlocal/RequestContext.java b/src/main/java/neatlogic/framework/asynchronization/threadlocal/RequestContext.java index 1b963273d9b5f6a9509f2b4e59f29b1a37f6f23b..58ccfa4c90bdf7be10cd315b4c1d3ec08766f27e 100644 --- a/src/main/java/neatlogic/framework/asynchronization/threadlocal/RequestContext.java +++ b/src/main/java/neatlogic/framework/asynchronization/threadlocal/RequestContext.java @@ -16,16 +16,14 @@ package neatlogic.framework.asynchronization.threadlocal; +import neatlogic.framework.dto.healthcheck.SqlAuditVo; import neatlogic.framework.restful.constvalue.RejectSource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.Serializable; -import java.util.Arrays; -import java.util.Locale; -import java.util.Objects; -import java.util.Optional; +import java.util.*; /** * 保存请求信息 @@ -44,6 +42,8 @@ public class RequestContext implements Serializable { private Double tenantRate; //语言 Locale locale; + //收集该请求执行的sql语句 + private List sqlAuditList = Collections.synchronizedList(new ArrayList<>()); public String getUrl() { return url; @@ -101,11 +101,24 @@ public class RequestContext implements Serializable { this.locale = locale; } + public List getSqlAuditList() { + return sqlAuditList; + } + + public void setSqlAuditList(List sqlAuditList) { + this.sqlAuditList = sqlAuditList; + } + + public void addSqlAudit(SqlAuditVo sqlAuditVo) { + sqlAuditList.add(sqlAuditVo); + } + public static RequestContext init(RequestContext _requestContext) { RequestContext context = new RequestContext(); if (_requestContext != null) { context.setUrl(_requestContext.getUrl()); context.setLocale(_requestContext.getLocale()); + context.setSqlAuditList(_requestContext.getSqlAuditList()); } instance.set(context); return context; diff --git a/src/main/java/neatlogic/framework/dao/plugin/SqlCostInterceptor.java b/src/main/java/neatlogic/framework/dao/plugin/SqlCostInterceptor.java index 7a9386ac0864cccc22899b1593d9a185043c359f..70be64f663d1363f5f5ebc05f82637099bd18d72 100644 --- a/src/main/java/neatlogic/framework/dao/plugin/SqlCostInterceptor.java +++ b/src/main/java/neatlogic/framework/dao/plugin/SqlCostInterceptor.java @@ -16,6 +16,7 @@ package neatlogic.framework.dao.plugin; +import neatlogic.framework.asynchronization.threadlocal.RequestContext; import neatlogic.framework.asynchronization.threadlocal.TenantContext; import neatlogic.framework.asynchronization.threadlocal.UserContext; import neatlogic.framework.dto.healthcheck.SqlAuditVo; @@ -117,7 +118,6 @@ public class SqlCostInterceptor implements Interceptor { //System.out.println("#############################SQL INTERCEPTOR###############################"); //System.out.println("id:" + sqlId); //System.out.println(sql); - sqlAuditVo.setSql(sql); sqlAuditVo.setId(sqlId); } @@ -139,6 +139,10 @@ public class SqlCostInterceptor implements Interceptor { } } SqlAuditManager.addSqlAudit(sqlAuditVo); + RequestContext requestContext =RequestContext.get(); + if (requestContext != null) { + requestContext.addSqlAudit(sqlAuditVo); + } //System.out.println("time cost:" + (System.currentTimeMillis() - starttime) + "ms"); //System.out.println("###########################################################################"); } diff --git a/src/main/java/neatlogic/module/framework/restful/dispatch/handler/AnonymousApiDispatcher.java b/src/main/java/neatlogic/module/framework/restful/dispatch/handler/AnonymousApiDispatcher.java index 3edc0ba9f9117fa79b0d5a7270f107e3a58c52b5..ff2932d999345795a612a3c2b65514d4973966e0 100644 --- a/src/main/java/neatlogic/module/framework/restful/dispatch/handler/AnonymousApiDispatcher.java +++ b/src/main/java/neatlogic/module/framework/restful/dispatch/handler/AnonymousApiDispatcher.java @@ -42,6 +42,7 @@ import neatlogic.framework.restful.dto.ApiVo; import neatlogic.framework.restful.enums.ApiType; import neatlogic.framework.restful.ratelimiter.RateLimiterTokenBucket; import neatlogic.framework.util.AnonymousApiTokenUtil; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.slf4j.Logger; @@ -139,6 +140,7 @@ public class AnonymousApiDispatcher { returnObj.put("TimeCost", endTime - startTime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); } else { returnObj.putAll(JSONObject.parseObject(JSONObject.toJSONString(returnV))); } @@ -165,6 +167,7 @@ public class AnonymousApiDispatcher { returnObj.put("TimeCost", endtime - starttime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); } else { returnObj.putAll(JSONObject.parseObject(JSONObject.toJSONString(returnV))); } @@ -191,6 +194,7 @@ public class AnonymousApiDispatcher { returnObj.put("TimeCost", endtime - starttime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); } else { returnObj.putAll(JSONObject.parseObject(JSONObject.toJSONString(returnV))); } diff --git a/src/main/java/neatlogic/module/framework/restful/dispatch/handler/ApiDispatcher.java b/src/main/java/neatlogic/module/framework/restful/dispatch/handler/ApiDispatcher.java index e22f4093e96ab1b0ef416aee84224c545b5f92cf..9385e4c3683f46da09a204817005c6243f9a1336 100644 --- a/src/main/java/neatlogic/module/framework/restful/dispatch/handler/ApiDispatcher.java +++ b/src/main/java/neatlogic/module/framework/restful/dispatch/handler/ApiDispatcher.java @@ -43,6 +43,7 @@ import neatlogic.framework.restful.ratelimiter.RateLimiterTokenBucket; import neatlogic.framework.util.$; import neatlogic.framework.util.HttpRequestUtil; import neatlogic.framework.util.mongodb.IJsonSerializer; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.reflections.Reflections; @@ -162,6 +163,7 @@ public class ApiDispatcher { returnObj.put("TimeCost", endtime - starttime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); if (restComponent.disableReturnCircularReferenceDetect()) { returnObj.put("_disableDetect", true); } @@ -191,6 +193,7 @@ public class ApiDispatcher { returnObj.put("TimeCost", endtime - starttime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); if (restComponent.disableReturnCircularReferenceDetect()) { returnObj.put("_disableDetect", true); } @@ -216,6 +219,7 @@ public class ApiDispatcher { returnObj.put("TimeCost", endtime - starttime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); if (restComponent.disableReturnCircularReferenceDetect()) { returnObj.put("_disableDetect", true); } diff --git a/src/main/java/neatlogic/module/framework/restful/dispatch/handler/PublicApiDispatcher.java b/src/main/java/neatlogic/module/framework/restful/dispatch/handler/PublicApiDispatcher.java index 87d7e7e12d79f4f8ace7d7356508d2e5c573b2f1..25b8effe7657b7b6b7b52e2a914f9913946c10d6 100644 --- a/src/main/java/neatlogic/module/framework/restful/dispatch/handler/PublicApiDispatcher.java +++ b/src/main/java/neatlogic/module/framework/restful/dispatch/handler/PublicApiDispatcher.java @@ -49,6 +49,7 @@ import neatlogic.framework.restful.dto.ApiVo; import neatlogic.framework.restful.enums.ApiType; import neatlogic.framework.restful.ratelimiter.RateLimiterTokenBucket; import neatlogic.framework.service.AuthenticationInfoService; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.slf4j.Logger; @@ -199,6 +200,7 @@ public class PublicApiDispatcher { returnObj.put("TimeCost", endtime - starttime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); } else { returnObj.putAll(JSONObject.parseObject(JSONObject.toJSONString(returnV))); } @@ -221,6 +223,7 @@ public class PublicApiDispatcher { returnObj.put("TimeCost", endtime - starttime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); } else { returnObj.putAll(JSONObject.parseObject(JSONObject.toJSONString(returnV))); } @@ -243,6 +246,7 @@ public class PublicApiDispatcher { returnObj.put("TimeCost", endtime - starttime); returnObj.put("Return", returnV); returnObj.put("Status", "OK"); + returnObj.put("sqlList", CollectionUtils.isEmpty(RequestContext.get().getSqlAuditList()) ? null : RequestContext.get().getSqlAuditList()); } else { returnObj.putAll(JSONObject.parseObject(JSONObject.toJSONString(returnV))); }