Skip to content

Commit f51ce71

Browse files
committed
不修改字节码111
1 parent 3a7cae7 commit f51ce71

File tree

28 files changed

+418
-388
lines changed

28 files changed

+418
-388
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 功能
44
java类调用过程记录Agent, 并以树状调用层次结构输出。
5-
5+
[![](https://jitpack.io/v/izerui/wx-java-sdk.svg)](https://jitpack.io/#izerui/wx-java-sdk)
66
效果图:
77
<img src="https://github.com/izerui/java-runner-feedback/blob/main/demo.jpg?raw=true">
88

@@ -18,12 +18,21 @@ java类调用过程记录Agent, 并以树状调用层次结构输出。
1818
```
1919

2020
2. 指定切入口:
21-
添加依赖(需要自行上传到自己的私服仓库):
21+
添加仓库
22+
```
23+
<repositories>
24+
<repository>
25+
<id>jitpack.io</id>
26+
<url>https://jitpack.io</url>
27+
</repository>
28+
</repositories>
29+
```
30+
添加依赖
2231
```
2332
<dependency>
2433
<groupId>com.github.izerui</groupId>
2534
<artifactId>runner-feedback-core</artifactId>
26-
<version>1.0.3</version>
35+
<version>1.1.0</version>
2736
</dependency>
2837
```
2938
声明要切入的入口方法:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.izerui</groupId>
88
<artifactId>java-runner-feedback</artifactId>
9-
<version>1.0.3</version>
9+
<version>1.1.0</version>
1010
<packaging>pom</packaging>
1111
<modules>
1212
<module>runner-feedback-agent</module>

runner-feedback-agent/pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.izerui</groupId>
88
<artifactId>java-runner-feedback</artifactId>
9-
<version>1.0.3</version>
9+
<version>1.1.0</version>
1010
</parent>
1111

1212
<artifactId>runner-feedback-agent</artifactId>
@@ -24,11 +24,6 @@
2424
<version>1.18.28</version>
2525
<scope>provided</scope>
2626
</dependency>
27-
<dependency>
28-
<groupId>org.apache.commons</groupId>
29-
<artifactId>commons-text</artifactId>
30-
<version>1.9</version>
31-
</dependency>
3227
<dependency>
3328
<groupId>org.ow2.asm</groupId>
3429
<artifactId>asm</artifactId>

runner-feedback-agent/src/main/java/com/github/izerui/Agent.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.github.izerui.context.Context;
44
import com.github.izerui.logger.LoggerTransformer;
55
import com.github.izerui.structure.StructureTransformer;
6-
import org.yaml.snakeyaml.Yaml;
76

87
import java.lang.instrument.Instrumentation;
98
import java.util.Arrays;

runner-feedback-agent/src/main/java/com/github/izerui/AgentProperties.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

runner-feedback-agent/src/main/java/com/github/izerui/context/Context.java

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
import com.github.izerui.ansi.AnsiColor;
55
import com.github.izerui.ansi.AnsiOutput;
66
import lombok.SneakyThrows;
7-
import net.bytebuddy.description.type.TypeDescription;
8-
import net.bytebuddy.matcher.ElementMatcher;
9-
import net.bytebuddy.matcher.ElementMatchers;
107
import org.objectweb.asm.Opcodes;
118
import org.yaml.snakeyaml.Yaml;
129

13-
import java.lang.annotation.Annotation;
14-
import java.text.SimpleDateFormat;
1510
import java.util.HashMap;
1611
import java.util.Map;
1712
import java.util.UUID;
@@ -26,13 +21,8 @@ public final class Context {
2621
public final static int ASM_VERSION = Opcodes.ASM9;
2722

2823

29-
/**
30-
* 类名与类的缓存
31-
*/
32-
private final static Map<String, Class> classCacheMap = new HashMap<>();
3324

3425

35-
public final static SimpleDateFormat DATE_TIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
3626

3727
static {
3828
Yaml yaml = new Yaml();
@@ -42,6 +32,7 @@ public final class Context {
4232

4333
/**
4434
* 获取配置
35+
*
4536
* @return
4637
*/
4738
public static AgentProperties getProperties() {
@@ -58,121 +49,11 @@ public static void printAfterAgent() {
5849
}
5950

6051

61-
/**
62-
* 通过类名匹配是否属于设置的包下
63-
*
64-
* @param className
65-
* @return
66-
*/
67-
public static boolean matchPackages(String className) {
68-
for (String ignorePackage : properties.getIgnore_packages()) {
69-
if (className.startsWith(ignorePackage)) {
70-
return false;
71-
}
72-
}
73-
for (String aPackage : properties.getPackages()) {
74-
if (className.startsWith(aPackage)) {
75-
return true;
76-
}
77-
}
78-
return false;
79-
}
8052

8153

82-
/**
83-
* 通过类名匹配是否属于设置的包下
84-
*
85-
* @param currentStackFrame
86-
* @return
87-
*/
88-
public static boolean matchInterfaceMethods(StackWalker.StackFrame currentStackFrame) {
89-
for (String classMethod : properties.getClass_methods()) {
90-
String[] split = classMethod.split("#");
91-
String cls = split[0];
92-
String mdp = split[1];
93-
if (cachedClass(cls).isAssignableFrom(cachedClass(getOriginName(currentStackFrame.getClassName(), "$")))
94-
&& (mdp.equals("*") || mdp.startsWith(getOriginName(currentStackFrame.getMethodName(), "$") + currentStackFrame.getDescriptor()))) {
95-
return true;
96-
}
97-
}
98-
return false;
99-
}
100-
10154

102-
/**
103-
* 匹配类型忽略指定的注解
104-
*
105-
* @param matcher
106-
* @return
107-
*/
108-
public static ElementMatcher.Junction<? super TypeDescription> matchTypeWithOutAnnotation(ElementMatcher.Junction<? super TypeDescription> matcher) {
109-
for (String annotationClassName : properties.getIgnore_annotations()) {
110-
try {
111-
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) cachedClass(annotationClassName);
112-
matcher = matcher.and(ElementMatchers.not(ElementMatchers.hasAnnotation(ElementMatchers.annotationType(annotationClass))));
113-
} catch (Exception ex) {
114-
;
115-
}
116-
}
117-
return matcher;
118-
}
11955

12056

121-
/**
122-
* 匹配指定是指定接口或者父类的子类匹配
123-
*
124-
* @param matcher
125-
* @return
126-
*/
127-
public static ElementMatcher.Junction<? super TypeDescription> matchTypeWithSubTypeOf(ElementMatcher.Junction<? super TypeDescription> matcher) {
128-
for (String classMethod : properties.getClass_methods()) {
129-
String[] split = classMethod.split("#");
130-
String cls = split[0];
131-
try {
132-
Class<?> aClass = cachedClass(cls);
133-
matcher = matcher.or(ElementMatchers.isSubTypeOf(aClass));
134-
} catch (Exception ex) {
135-
;
136-
}
137-
}
138-
return matcher;
139-
}
140-
141-
/**
142-
* 从缓存中获取class
143-
*
144-
* @param className
145-
* @return
146-
*/
147-
@SneakyThrows
148-
public static Class cachedClass(String className) {
149-
Class aClass = classCacheMap.get(className);
150-
if (aClass == null) {
151-
aClass = Class.forName(className);
152-
classCacheMap.put(className, aClass);
153-
}
154-
return aClass;
155-
}
156-
157-
158-
/**
159-
* 获取原始名
160-
*
161-
* @param proxyName 代理名称
162-
* @param proxyIdentifier 代理标识符, class类型: $$ method类型: $
163-
* @return
164-
*/
165-
public static String getOriginName(String proxyName, String proxyIdentifier) {
166-
String originMethodName = proxyName;
167-
int proxySplitIndex = proxyName.indexOf(proxyIdentifier);
168-
if (proxySplitIndex > -1) {
169-
originMethodName = proxyName.substring(0, proxySplitIndex);
170-
}
171-
if (originMethodName == null || "".equals(originMethodName)) {
172-
originMethodName = proxyName;
173-
}
174-
return originMethodName;
175-
}
17657

17758

17859
public static final String[] TRACE_CHARS = new String[]{"a", "b", "c", "d", "e", "f",

runner-feedback-agent/src/main/java/com/github/izerui/logger/LoggerInterceptor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.izerui.logger;
22

3+
import com.github.izerui.AgentProperties;
34
import com.github.izerui.annotation.Tracer;
45
import com.github.izerui.context.Context;
56
import com.github.izerui.context.MethodContext;
@@ -69,10 +70,11 @@ public static Object intercept(
6970
StackWalker.StackFrame currentStackFrame = stackFrames.get(0);
7071
// 当tracer存在,则后续跨度及子线程都需要拦截记录
7172
com.github.izerui.support.Tracer tracer = TracerContext.getTracer();
73+
AgentProperties properties = Context.getProperties();
7274
// 只记录指定扫描的包下或者指定接口方法的
7375
if (tracer != null &&
74-
(Context.matchPackages(currentStackFrame.getClassName())
75-
|| Context.matchInterfaceMethods(currentStackFrame))) {
76+
(properties.matchPackages(currentStackFrame.getClassName())
77+
|| properties.getMatchCustomizer(currentStackFrame.getClassName(), currentStackFrame.getMethodName(), currentStackFrame.getDescriptor()) != null)) {
7678
int methodLine = MethodContext.getLine(method.getDeclaringClass().getName(), method.getName(), currentStackFrame.getDescriptor());
7779
// 当能获取到本地类路径的方法行号则记录, 或者deepshow参数为true表示指定包下对象的父类方法也记录
7880
// System.out.println("拦截: " + method.getDeclaringClass().getName() + "#" + method.getName());
@@ -91,7 +93,7 @@ public static Object intercept(
9193
.build());
9294
if (rootInComming) {
9395
tracer.setEnd(end);
94-
tracer.print();
96+
tracer.print(Context.getProperties());
9597
}
9698
}
9799
} catch (Exception ex) {

0 commit comments

Comments
 (0)