From 5031745c1051fadc4d3085dd86afa2b8aa528eb2 Mon Sep 17 00:00:00 2001 From: huoyo <1729913829@qq.com> Date: Thu, 8 Sep 2022 12:09:13 +0800 Subject: [PATCH] optimize exception handler --- pom.xml | 2 +- .../langpy/kotime/constant/KoSqlConstant.java | 13 +++-- .../java/cn/langpy/kotime/data/DataBase.java | 14 +++--- .../cn/langpy/kotime/data/MemoryBase.java | 5 +- .../java/cn/langpy/kotime/data/RedisBase.java | 5 +- .../langpy/kotime/handler/RunTimeHandler.java | 2 +- .../kotime/model/ExceptionRelation.java | 9 ++++ src/main/resources/sql/mysql-v2.2.3-.sql | 47 +++++++++++++++++++ src/main/resources/sql/mysql-v2.2.4+.sql | 47 +++++++++++++++++++ 9 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 src/main/resources/sql/mysql-v2.2.3-.sql create mode 100644 src/main/resources/sql/mysql-v2.2.4+.sql diff --git a/pom.xml b/pom.xml index 272afe6..5e42638 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.langpy ko-time - 2.2.3 + 2.2.4 KoTime A springboot tool for tracking the paths of the methods,which can help you to find methods's performances easily. diff --git a/src/main/java/cn/langpy/kotime/constant/KoSqlConstant.java b/src/main/java/cn/langpy/kotime/constant/KoSqlConstant.java index 7895776..51b81ed 100644 --- a/src/main/java/cn/langpy/kotime/constant/KoSqlConstant.java +++ b/src/main/java/cn/langpy/kotime/constant/KoSqlConstant.java @@ -6,18 +6,17 @@ public class KoSqlConstant { public final static String queryMethodLikeName = "SELECT id, name, class_name, method_name, route_name, method_type FROM ko_method_node WHERE name like ?"; public final static String queryController = "SELECT id, name, class_name, method_name, route_name, method_type FROM ko_method_node WHERE method_type=?"; public final static String updateMethod = "UPDATE ko_method_node SET name=?, class_name=?, method_name=?, route_name=?, method_type=? WHERE id=?"; - public final static String addException = "INSERT INTO ko_exception_node(id, name, class_name, message) VALUES (?, ?, ?, ?)"; - public final static String queryExceptions = "SELECT id, name, class_name, message FROM ko_exception_node"; - public final static String queryException = "SELECT id, name, class_name, message FROM ko_exception_node WHERE id=?"; + public final static String addException = "INSERT INTO ko_exception_node(id, name, class_name) VALUES (?, ?, ?)"; + public final static String queryExceptions = "SELECT id, name, class_name FROM ko_exception_node"; + public final static String queryException = "SELECT id, name, class_name FROM ko_exception_node WHERE id=?"; public final static String addMethodRe = "INSERT INTO ko_method_relation(id, source_id, target_id, avg_run_time, max_run_time, min_run_time) VALUES (?, ?, ?, ?, ?, ?)"; public final static String queryMethodRe = "SELECT id, source_id, target_id, avg_run_time, max_run_time, min_run_time FROM ko_method_relation WHERE id=?"; public final static String queryMethodReBySource = "SELECT id, source_id, target_id, avg_run_time, max_run_time, min_run_time FROM ko_method_relation WHERE source_id=?"; public final static String queryMethodReByTarget = "SELECT id, source_id, target_id, avg_run_time, max_run_time, min_run_time FROM ko_method_relation WHERE target_id=?"; public final static String updateMethodRe = "UPDATE ko_method_relation SET source_id=?, target_id=?, avg_run_time=?, max_run_time=?, min_run_time=? WHERE id=?"; - public final static String addExceptionRe = "INSERT INTO ko_exception_relation(id, source_id, target_id, location) VALUES (?, ?, ?, ?)"; - public final static String queryExceptionRe = "SELECT id, source_id, target_id, location FROM ko_exception_relation WHERE id=?"; - public final static String queryExceptionReByTarget = "SELECT id, source_id, target_id, location FROM ko_exception_relation WHERE target_id=?"; - public final static String updateExceptionRe = "UPDATE ko_exception_relation SET source_id=?, target_id=?, location=? WHERE id=?"; + public final static String addExceptionRe = "INSERT INTO ko_exception_relation(id, source_id, target_id, location,message) VALUES (?, ?, ?, ?, ?)"; + public final static String queryExceptionRe = "SELECT id, source_id, target_id, location, message FROM ko_exception_relation WHERE id=?"; + public final static String queryExceptionReByTarget = "SELECT id, source_id, target_id, location, message FROM ko_exception_relation WHERE target_id=?"; public final static String addParamsAna = "INSERT INTO ko_param_ana (source_id, params, avg_run_time, max_run_time, min_run_time) VALUES (?, ?, ?, ?, ?)"; public final static String queryParamsAna = "SELECT source_id, params, avg_run_time, max_run_time, min_run_time FROM ko_param_ana WHERE source_id=? and params=?"; public final static String queryParamsAnaBySource = "SELECT source_id, params, avg_run_time, max_run_time, min_run_time FROM ko_param_ana WHERE source_id=?"; diff --git a/src/main/java/cn/langpy/kotime/data/DataBase.java b/src/main/java/cn/langpy/kotime/data/DataBase.java index 149d5a2..61f644c 100644 --- a/src/main/java/cn/langpy/kotime/data/DataBase.java +++ b/src/main/java/cn/langpy/kotime/data/DataBase.java @@ -130,8 +130,7 @@ public class DataBase implements GraphService { Object[] params = new Object[]{ exceptionNode.getId(), exceptionNode.getName(), - exceptionNode.getClassName(), - exceptionNode.getMessage() + exceptionNode.getClassName() }; DataBaseUtil.insert(getWriteConnection(), KoSqlConstant.addException, params); } @@ -192,7 +191,8 @@ public class DataBase implements GraphService { sourceMethodNode.getId() + exceptionNode.getId(), sourceMethodNode.getId(), exceptionNode.getId(), - exceptionNode.getValue() + exceptionNode.getValue(), + exceptionNode.getMessage() }; DataBaseUtil.insert(getWriteConnection(), KoSqlConstant.addExceptionRe, params); } @@ -454,8 +454,8 @@ public class DataBase implements GraphService { } MethodNode methodNode = methodNodes.get(0); - List exceptions = DataBaseUtil.query(getReadConnection(), KoSqlConstant.queryMethod, new Object[]{exceptionId}, ExceptionNode.class); - if (methodNodes.size() == 0) { + List exceptions = DataBaseUtil.query(getReadConnection(), KoSqlConstant.queryException, new Object[]{exceptionId}, ExceptionNode.class); + if (exceptions.size() == 0) { continue; } ExceptionNode exceptionNode = exceptions.get(0); @@ -464,7 +464,7 @@ public class DataBase implements GraphService { exceptionInfo.setName(exceptionNode.getName()); exceptionInfo.setClassName(exceptionNode.getClassName()); exceptionInfo.setLocation(relation.getLocation()); - exceptionInfo.setMessage(exceptionNode.getMessage()); + exceptionInfo.setMessage(relation.getMessage()); exceptionInfo.setMethodName(methodNode.getMethodName()); exceptionInfo.setOccurClassName(methodNode.getClassName()); if (!exceptionInfos.contains(exceptionInfo)) { @@ -489,7 +489,7 @@ public class DataBase implements GraphService { exceptionInfo.setId(exceptionNode.getId()); exceptionInfo.setName(exceptionNode.getName()); exceptionInfo.setClassName(exceptionNode.getClassName()); - exceptionInfo.setMessage(exceptionNode.getMessage()); + exceptionInfo.setMessage(relation.getMessage()); exceptionInfo.setLocation(relation.getLocation()); if (!exceptionInfos.contains(exceptionInfo)) { exceptionInfos.add(exceptionInfo); diff --git a/src/main/java/cn/langpy/kotime/data/MemoryBase.java b/src/main/java/cn/langpy/kotime/data/MemoryBase.java index 73949bb..0db7894 100644 --- a/src/main/java/cn/langpy/kotime/data/MemoryBase.java +++ b/src/main/java/cn/langpy/kotime/data/MemoryBase.java @@ -133,6 +133,7 @@ public class MemoryBase implements GraphService { exceptionRelation.setSourceId(sourceMethodNode.getId()); exceptionRelation.setTargetId(exceptionNode.getId()); exceptionRelation.setLocation(exceptionNode.getValue()); + exceptionRelation.setMessage(exceptionNode.getMessage()); ExceptionRelation old = exceptionRelations.get(exceptionRelation.getId()); if (null == old) { exceptionRelations.put(exceptionRelation.getId(), exceptionRelation); @@ -160,7 +161,7 @@ public class MemoryBase implements GraphService { exceptionInfo.setId(exceptionNode.getId()); exceptionInfo.setName(exceptionNode.getName()); exceptionInfo.setClassName(exceptionNode.getClassName()); - exceptionInfo.setMessage(exceptionNode.getMessage()); + exceptionInfo.setMessage(relation.getMessage()); exceptionInfo.setLocation(relation.getLocation()); if (!exceptionInfos.contains(exceptionInfo)) { exceptionInfos.add(exceptionInfo); @@ -274,7 +275,7 @@ public class MemoryBase implements GraphService { exceptionInfo.setName(exceptionNode.getName()); exceptionInfo.setClassName(exceptionNode.getClassName()); exceptionInfo.setLocation(relation.getLocation()); - exceptionInfo.setMessage(exceptionNode.getMessage()); + exceptionInfo.setMessage(relation.getMessage()); exceptionInfo.setMethodName(methodNode.getMethodName()); exceptionInfo.setOccurClassName(methodNode.getClassName()); if (!exceptionInfos.contains(exceptionInfo)) { diff --git a/src/main/java/cn/langpy/kotime/data/RedisBase.java b/src/main/java/cn/langpy/kotime/data/RedisBase.java index 2307897..30c42b6 100644 --- a/src/main/java/cn/langpy/kotime/data/RedisBase.java +++ b/src/main/java/cn/langpy/kotime/data/RedisBase.java @@ -139,6 +139,7 @@ public class RedisBase implements GraphService { exceptionRelation.setSourceId(sourceMethodNode.getId()); exceptionRelation.setTargetId(exceptionNode.getId()); exceptionRelation.setLocation(exceptionNode.getValue()); + exceptionRelation.setMessage(exceptionNode.getMessage()); String key = exceptionRelationPre + exceptionRelation.getId(); ExceptionRelation old = query(key, ExceptionRelation.class); if (null == old) { @@ -169,7 +170,7 @@ public class RedisBase implements GraphService { exceptionInfo.setId(exceptionNode.getId()); exceptionInfo.setName(exceptionNode.getName()); exceptionInfo.setClassName(exceptionNode.getClassName()); - exceptionInfo.setMessage(exceptionNode.getMessage()); + exceptionInfo.setMessage(relation.getMessage()); exceptionInfo.setLocation(relation.getLocation()); if (!exceptionInfos.contains(exceptionInfo)) { exceptionInfos.add(exceptionInfo); @@ -302,7 +303,7 @@ public class RedisBase implements GraphService { exceptionInfo.setName(exceptionNode.getName()); exceptionInfo.setClassName(exceptionNode.getClassName()); exceptionInfo.setLocation(relation.getLocation()); - exceptionInfo.setMessage(exceptionNode.getMessage()); + exceptionInfo.setMessage(relation.getMessage()); exceptionInfo.setMethodName(methodNode.getMethodName()); exceptionInfo.setOccurClassName(methodNode.getClassName()); if (!exceptionInfos.contains(exceptionInfo)) { diff --git a/src/main/java/cn/langpy/kotime/handler/RunTimeHandler.java b/src/main/java/cn/langpy/kotime/handler/RunTimeHandler.java index 5906ba6..bd80045 100644 --- a/src/main/java/cn/langpy/kotime/handler/RunTimeHandler.java +++ b/src/main/java/cn/langpy/kotime/handler/RunTimeHandler.java @@ -43,7 +43,7 @@ public class RunTimeHandler implements MethodInterceptor { exception.setName(e.getClass().getSimpleName()); exception.setClassName(e.getClass().getName()); exception.setMessage(e.getMessage()); - exception.setId(exception.getClassName() + exception.getName() + exception.getMessage()); + exception.setId(exception.getClassName() + exception.getName()); MethodNode current = MethodNodeService.getCurrentMethodNode(invocation, 0.0); for (StackTraceElement stackTraceElement : e.getStackTrace()) { if (stackTraceElement.getClassName().equals(current.getClassName())) { diff --git a/src/main/java/cn/langpy/kotime/model/ExceptionRelation.java b/src/main/java/cn/langpy/kotime/model/ExceptionRelation.java index 98d962e..0e21e29 100644 --- a/src/main/java/cn/langpy/kotime/model/ExceptionRelation.java +++ b/src/main/java/cn/langpy/kotime/model/ExceptionRelation.java @@ -6,6 +6,7 @@ public class ExceptionRelation { private String id; private String sourceId; private String targetId; + private String message; private Integer location = 0; public Integer getLocation() { @@ -40,6 +41,14 @@ public class ExceptionRelation { this.targetId = targetId; } + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/resources/sql/mysql-v2.2.3-.sql b/src/main/resources/sql/mysql-v2.2.3-.sql new file mode 100644 index 0000000..c4b3be7 --- /dev/null +++ b/src/main/resources/sql/mysql-v2.2.3-.sql @@ -0,0 +1,47 @@ +-- v2.2.3及以下版本 +create table ko_method_node +( + id varchar(400) not null primary key comment '主键', + name varchar(400) null comment '类名+方法名', + class_name varchar(400) null comment '类名', + method_name varchar(400) null comment '方法名', + route_name varchar(400) null comment '路由,controller才有', + method_type varchar(64) null comment '方法类型' +) comment '方法信息表'; + + +create table ko_method_relation +( + id varchar(400) not null primary key comment '', + source_id varchar(400) null comment '调用方id', + target_id varchar(400) null comment '被调用方id', + avg_run_time numeric(10, 2) null comment '平均耗时', + max_run_time numeric(10, 2) null comment '最大耗时', + min_run_time numeric(10, 2) null comment '最小耗时' +) comment '方法调用关系表'; +; +create table ko_exception_node +( + id varchar(400) not null primary key comment '主键', + name varchar(400) null comment '异常名', + class_name varchar(400) null comment '类名', + message varchar(400) null comment '异常消息' +) comment '异常表'; + + +create table ko_exception_relation +( + id varchar(400) not null primary key comment '', + source_id varchar(400) null comment '调用方法id', + target_id varchar(400) null comment '异常id', + location int null comment '异常位置' +) comment '异常关系表'; + +create table ko_param_ana +( + source_id varchar(400) null comment '调用方法id', + params varchar(400) null comment '参数组合,-分隔', + avg_run_time numeric(10, 2) null comment '平均耗时', + max_run_time numeric(10, 2) null comment '最大耗时', + min_run_time numeric(10, 2) null comment '最小耗时' +) comment '参数分析表'; \ No newline at end of file diff --git a/src/main/resources/sql/mysql-v2.2.4+.sql b/src/main/resources/sql/mysql-v2.2.4+.sql new file mode 100644 index 0000000..3f68cd1 --- /dev/null +++ b/src/main/resources/sql/mysql-v2.2.4+.sql @@ -0,0 +1,47 @@ +-- v2.2.4及以上版本 +create table ko_method_node +( + id varchar(400) not null primary key comment '主键', + name varchar(400) null comment '类名+方法名', + class_name varchar(400) null comment '类名', + method_name varchar(400) null comment '方法名', + route_name varchar(400) null comment '路由,controller才有', + method_type varchar(64) null comment '方法类型' +) comment '方法信息表'; + + +create table ko_method_relation +( + id varchar(400) not null primary key comment '', + source_id varchar(400) null comment '调用方id', + target_id varchar(400) null comment '被调用方id', + avg_run_time numeric(10, 2) null comment '平均耗时', + max_run_time numeric(10, 2) null comment '最大耗时', + min_run_time numeric(10, 2) null comment '最小耗时' +) comment '方法调用关系表'; +; +create table ko_exception_node +( + id varchar(400) not null primary key comment '主键', + name varchar(400) null comment '异常名', + class_name varchar(400) null comment '类名' +) comment '异常表'; + + +create table ko_exception_relation +( + id varchar(400) not null primary key comment '', + source_id varchar(400) null comment '调用方法id', + target_id varchar(400) null comment '异常id', + location int null comment '异常位置', + message varchar(400) null comment '异常消息' +) comment '异常关系表'; + +create table ko_param_ana +( + source_id varchar(400) null comment '调用方法id', + params varchar(400) null comment '参数组合,-分隔', + avg_run_time numeric(10, 2) null comment '平均耗时', + max_run_time numeric(10, 2) null comment '最大耗时', + min_run_time numeric(10, 2) null comment '最小耗时' +) comment '参数分析表';