forked from mianshenglee/my-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mason
committed
Aug 23, 2019
1 parent
826dfcf
commit 41e5fe0
Showing
20 changed files
with
1,018 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
# my-example | ||
my example code | ||
# 说明 | ||
|
||
在写文章过程中,经常会使用一些示例工程来辅助说明,本仓库用于存放一些示例工程,以便于读者可以参考。 | ||
|
||
# 示例说明 | ||
|
||
## `java-monitor-example` | ||
|
||
用于java监测技术文章的示例说明,此示例是一个`spring boot`工程,里面包含简单的`controller`和`service`类,`OOM`的接口等功能。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target | ||
*.iml | ||
.idea | ||
logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>me.mason.example</groupId> | ||
<artifactId>java-monitor-example</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>java-monitor-example</name> | ||
<description>java-monitor-example</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.4.RELEASE</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- spring boot --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<!-- 工具包:hutool --> | ||
<dependency> | ||
<groupId>cn.hutool</groupId> | ||
<artifactId>hutool-core</artifactId> | ||
<version>4.5.7</version> | ||
</dependency> | ||
<!-- 工具包:lombok --> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.6</version> | ||
</dependency> | ||
|
||
<!-- BTrace --> | ||
<dependency> | ||
<groupId>com.sun.btrace</groupId> | ||
<artifactId>btrace-agent</artifactId> | ||
<version>1.3.11.3</version> | ||
<type>jar</type> | ||
<scope>system</scope> | ||
<systemPath>E:/installer/btrace/btrace-bin-1.3.11.3/build/btrace-agent.jar</systemPath> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.btrace</groupId> | ||
<artifactId>btrace-boot</artifactId> | ||
<version>1.3.11.3</version> | ||
<type>jar</type> | ||
<scope>system</scope> | ||
<systemPath>E:/installer/btrace/btrace-bin-1.3.11.3/build/btrace-boot.jar</systemPath> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.btrace</groupId> | ||
<artifactId>btrace-client</artifactId> | ||
<version>1.3.11.3</version> | ||
<type>jar</type> | ||
<scope>system</scope> | ||
<systemPath>E:/installer/btrace/btrace-bin-1.3.11.3/build/btrace-client.jar</systemPath> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
97 changes: 97 additions & 0 deletions
97
java-monitor-example/src/main/java/btrace/exception/OnThrow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the Classpath exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
package btrace.exception; | ||
|
||
import com.sun.btrace.annotations.*; | ||
|
||
import static com.sun.btrace.BTraceUtils.*; | ||
|
||
/** | ||
* This example demonstrates printing stack trace | ||
* of an exception and thread local variables. This | ||
* trace script prints exception stack trace whenever | ||
* java.lang.Throwable's constructor returns. This way | ||
* you can trace all exceptions that may be caught and | ||
* "eaten" silently by the traced program. Note that the | ||
* assumption is that the exceptions are thrown soon after | ||
* creation [like in "throw new FooException();"] rather | ||
* that be stored and thrown later. | ||
*/ | ||
@BTrace public class OnThrow { | ||
// store current exception in a thread local | ||
// variable (@TLS annotation). Note that we can't | ||
// store it in a global variable! | ||
@TLS static Throwable currentException; | ||
|
||
// introduce probe into every constructor of java.lang.Throwable | ||
// class and store "this" in the thread local variable. | ||
@OnMethod( | ||
clazz="java.lang.Throwable", | ||
method="<init>" | ||
) | ||
public static void onthrow(@Self Throwable self) { | ||
currentException = self; | ||
} | ||
|
||
@OnMethod( | ||
clazz="java.lang.Throwable", | ||
method="<init>" | ||
) | ||
public static void onthrow1(@Self Throwable self, String s) { | ||
currentException = self; | ||
} | ||
|
||
@OnMethod( | ||
clazz="java.lang.Throwable", | ||
method="<init>" | ||
) | ||
public static void onthrow1(@Self Throwable self, String s, Throwable cause) { | ||
currentException = self; | ||
} | ||
|
||
@OnMethod( | ||
clazz="java.lang.Throwable", | ||
method="<init>" | ||
) | ||
public static void onthrow2(@Self Throwable self, Throwable cause) { | ||
currentException = self; | ||
} | ||
|
||
// when any constructor of java.lang.Throwable returns | ||
// print the currentException's stack trace. | ||
@OnMethod( | ||
clazz="java.lang.Throwable", | ||
method="<init>", | ||
location=@Location(Kind.RETURN) | ||
) | ||
public static void onthrowreturn() { | ||
if (currentException != null) { | ||
Threads.jstack(currentException); | ||
println("====================="); | ||
currentException = null; | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
java-monitor-example/src/main/java/btrace/function/PrintArgs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package btrace.function; | ||
|
||
import com.sun.btrace.AnyType; | ||
import com.sun.btrace.BTraceUtils; | ||
import com.sun.btrace.annotations.BTrace; | ||
import com.sun.btrace.annotations.Kind; | ||
import com.sun.btrace.annotations.Location; | ||
import com.sun.btrace.annotations.OnMethod; | ||
import com.sun.btrace.annotations.ProbeClassName; | ||
import com.sun.btrace.annotations.ProbeMethodName; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* 拦截方法参数 | ||
* | ||
* @author mason | ||
* @since 2019/8/2 | ||
*/ | ||
@BTrace | ||
public class PrintArgs { | ||
/** | ||
* 输出多个参数 | ||
* 普通方法:@OnMethod(clazz = "",method = "") | ||
* 同名函数,使用参数区分 | ||
* 拦截时机:location = @Location(Kind) | ||
* Kind.ENTRY,入口(默认值),Kind.RETURN 返回时 | ||
* Kind.THROW,异常,Kind.LINE 行数 | ||
* @param className | ||
* @param methodName | ||
* @param args | ||
*/ | ||
@OnMethod(clazz = "me.mason.monitor.controller.UserController" | ||
,method = "getUsers",location = @Location(Kind.ENTRY)) | ||
public static void readFunction(@ProbeClassName String className, @ProbeMethodName String methodName | ||
, AnyType[] args) { | ||
// 打印时间 | ||
BTraceUtils.println(BTraceUtils.Time.timestamp("yyyy-MM-dd HH:mm:ss")); | ||
BTraceUtils.println("method controller"); | ||
BTraceUtils.printArray(args); | ||
BTraceUtils.println(className + "," + methodName); | ||
BTraceUtils.println("=========================="); | ||
} | ||
|
||
/** | ||
* 打印单个参数 | ||
* @param arg | ||
*/ | ||
@OnMethod(clazz = "me.mason.monitor.service.UserService", | ||
method = "getUsers", | ||
location = @Location(Kind.ENTRY)) | ||
public static void readGetUserService(int arg){ | ||
// 打印时间 | ||
BTraceUtils.println(BTraceUtils.Time.timestamp("yyyy-MM-dd HH:mm:ss")); | ||
BTraceUtils.println("method service "); | ||
//简单类型使用print或println | ||
BTraceUtils.println(arg); | ||
BTraceUtils.println("=========================="); | ||
} | ||
|
||
/** | ||
* 构造函数:@OnMethod(clazz = "",method = "<init>") | ||
* @param args | ||
*/ | ||
@OnMethod(clazz = "me.mason.monitor.entity.User",method = "<init>") | ||
public static void readConstruct(AnyType[]args){ | ||
// 打印时间 | ||
BTraceUtils.println(BTraceUtils.Time.timestamp("yyyy-MM-dd HH:mm:ss")); | ||
BTraceUtils.println("construct:"); | ||
BTraceUtils.printArray(args); | ||
BTraceUtils.println("=========================="); | ||
} | ||
|
||
/** | ||
* 正则式匹配 | ||
* @param className | ||
* @param methodName | ||
* @param args | ||
*/ | ||
@OnMethod(clazz = "me.mason.monitor.service.UserService", | ||
method = "/.*/", | ||
location = @Location(Kind.ENTRY)) | ||
public static void printAllMethod(@ProbeClassName String className,@ProbeMethodName String methodName, AnyType[] args){ | ||
// 打印时间 | ||
BTraceUtils.println(BTraceUtils.Time.timestamp("yyyy-MM-dd HH:mm:ss")); | ||
BTraceUtils.println("regex print"); | ||
BTraceUtils.println(className + ","+ methodName); | ||
BTraceUtils.printArray(args); | ||
BTraceUtils.println("=========================="); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
java-monitor-example/src/main/java/btrace/function/PrintDuration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package btrace.function; | ||
|
||
import com.sun.btrace.BTraceUtils; | ||
import com.sun.btrace.annotations.BTrace; | ||
import com.sun.btrace.annotations.Duration; | ||
import com.sun.btrace.annotations.Kind; | ||
import com.sun.btrace.annotations.Location; | ||
import com.sun.btrace.annotations.OnMethod; | ||
|
||
/** | ||
* 输出方法执行时间 | ||
* | ||
* @author mason | ||
* @since 2019/8/2 | ||
*/ | ||
@BTrace | ||
public class PrintDuration { | ||
@OnMethod(clazz = "me.mason.monitor.controller.UserController" | ||
,method = "getUsers" | ||
,location = @Location(Kind.RETURN)) | ||
public static void getUsersDuration(@Duration long duration){ | ||
// 打印时间 | ||
BTraceUtils.println(BTraceUtils.Time.timestamp("yyyy-MM-dd HH:mm:ss")); | ||
BTraceUtils.println("time(ns):" + duration); | ||
BTraceUtils.println("time(ms):" + BTraceUtils.str(duration / 1000000)); | ||
BTraceUtils.println("time(s):" + BTraceUtils.str(duration / 1000000000)); | ||
BTraceUtils.println("=========================="); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
java-monitor-example/src/main/java/btrace/function/PrintLine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package btrace.function; | ||
|
||
import com.sun.btrace.AnyType; | ||
import com.sun.btrace.BTraceUtils; | ||
import com.sun.btrace.annotations.BTrace; | ||
import com.sun.btrace.annotations.Kind; | ||
import com.sun.btrace.annotations.Location; | ||
import com.sun.btrace.annotations.OnMethod; | ||
import com.sun.btrace.annotations.ProbeClassName; | ||
import com.sun.btrace.annotations.ProbeMethodName; | ||
|
||
/** | ||
* 按等号拦截 | ||
* | ||
* @author mason | ||
* @since 2019/8/2 | ||
*/ | ||
@BTrace | ||
public class PrintLine { | ||
|
||
@OnMethod(clazz = "me.mason.monitor.service.UserService" | ||
,method = "getUsers" | ||
,location = @Location(value = Kind.LINE,line = 39)) | ||
public static void printLineData(@ProbeClassName String className | ||
, @ProbeMethodName String methodName | ||
,int line){ | ||
// 打印时间 | ||
BTraceUtils.println(BTraceUtils.Time.timestamp("yyyy-MM-dd HH:mm:ss")); | ||
BTraceUtils.println(className + "," + methodName + ","+line); | ||
BTraceUtils.println("=========================="); | ||
} | ||
} |
Oops, something went wrong.