当前位置:网站首页>Byte buddy print execution time and method link tracking
Byte buddy print execution time and method link tracking
2022-06-13 05:38:00 【Flying it people】
List of articles
Preface
Many small partners may encounter the problem of printing execution time in the development process . The normal operation is to write a few lines of code in the method to be monitored to print the execution time , Hard working friends will write aop To print the execution time , But most of the time , We don't need to bring this code into the production environment , So you should find the relevant code to delete when you go online . This article provides a set of Java Probe program , It can be developed or tested ( Pressure measurement ) Phase assists in debugging code . Also provide generation traceId, Easy to track method links .
First provide the source code address :
https://github.com/Jamsw/bytebuddy-agent.git
Tips : The following is the main body of this article , The following cases can be used for reference
One 、byte-buddy What is it? ?
Byte Buddy It's a JVM Runtime code generator for , Simple is provided API, It's easy to do by hand 、 adopt Java Agent, Or modify bytecode during build .
Two 、 Use steps
1. Translate it into jar package
The order is as follows :
mvn clean package
After execution , Will be in target Generate... In directory jar Packages and config Catalog .jar Bao He config It needs to be in the same directory . As shown in the figure below :
config The directory contains agent.config The configuration file , There are two parameters :
agent.packname = com.smart.tcp.gateway
agent.ignore = com.smart.tcp.gateway.client.test.UserModel,com.smart.tcp.gateway.client.test.UserModel
agent.packname: The package path where the log needs to be printed .
agent.ignore: Classes to ignore .
2. To configure Java probe
take idea Examples of development tools , Need to be in VM options Configure the following parameters :
-javaagent:path( Fill in the directory address of the local computer or server )/log-agent-1.0-SNAPSHOT.jar
3. Effect demonstration
tid:71b8195c-eadc-4385-8db1-a8fb414743e8:INFO> The method level 【 Class name :com.smart.tcp.gateway.client.test.UserMapper Method name :<init> The execution time is :0ms】
tid:71b8195c-eadc-4385-8db1-a8fb414743e8:INFO> The method level 【 Class name :com.smart.tcp.gateway.client.test.UserMapper Method name :selectUserInfo The execution time is :0ms】
tid:71b8195c-eadc-4385-8db1-a8fb414743e8:INFO> The method level 【 Class name :com.smart.tcp.gateway.client.test.UserServiceImpl Method name :getUserInfo The execution time is :10ms】
tid:71b8195c-eadc-4385-8db1-a8fb414743e8:INFO> Interface level 【url path:/user/getInfo Enter the reference :key=userId value=123456, The ginseng :{
"userName":"smart","userId":null} Overall time-consuming interface :109ms】
3、 ... and 、 Encounter problems
1. Pile driving
Because you want to print the interface return value , So we need to HttpServletResponse Secondary packaging ,agent The program needs to encapsulate the secondary reponse To the method of interception , Therefore, piling is required .
Rewrite first superCall Method , as follows :
public interface OverrideCallable {
Object call(Object[] args);
}
And then in ProcessRequestInterceptor Method input parameter input OverrideCallable, as follows :
public static Object intercept(@AllArguments Object[] allArguments, @Morph OverrideCallable zuper)
stay premain Add piling configuration .withDefaultConfiguration().withBinders(Morph.Binder.install(OverrideCallable.class)), As shown below :
agentBuilder.ignore(ElementMatchers.named("javax.servlet.http"))
.type(ElementMatchers.named("org.springframework.web.servlet.FrameworkServlet"))
.transform((builder, type, classLoader, module) ->
builder.method(ElementMatchers.named("processRequest"))
.intercept(MethodDelegation.withDefaultConfiguration().withBinders(Morph.Binder.install(OverrideCallable.class)).to(ProcessRequestInterceptor.class)))
.installOn(inst);
2.mybatis Report errors
because byte-buddy The original class name will be modified when injecting code , therefore mybatis When you query a method by class name, you will report java.lang.NoSuchMethodError Report errors , The solution is to configure not to modify the class name .disableClassFormatChanges(), The specific code is as follows :
agentBuilder.disableClassFormatChanges().ignore(e)
.type(ElementMatchers.nameStartsWith(Config.Agent.packname))
.transform(transformer).installOn(inst);
边栏推荐
- Unity game optimization [Second Edition] learning record 6
- MongoDB 多字段聚合Group by
- Initial redis experience
- Hump naming and underlining
- 2020 personal annual summary
- C calls the API and parses the returned JSON string
- ZABBIX proxy, sender (without agent monitoring), performance optimization
- Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
- 18 flowable task manualtask and receivetask
- MySQL main query and sub query
猜你喜欢

890. Find and Replace Pattern

13 cancelendevent of a flowable end event and compensationthrowing of a compensation event

Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file

Django uses redis to store sessions starting from 0

Case - simulated landlords (primary version)

File descriptorfile description

Etcd understanding of microservice architecture

MongoDB 多字段聚合Group by

Mysql database backup and restore:

Database design
随机推荐
通过命令行创建harbor镜像库
11 signalthrowingevent and signalboundaryevent of flowable signal event
Small project - household income and expenditure software (2)
Dynamic and static libraries
About anonymous inner classes
How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
【转载】C语言内存和字符操作函数大全
Standard input dialog for pyqt5 qinputdialog
A simple recursion problem of linked list
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
Bicolor case
17 servicetask of flowable task
priority inversion problem
Unity game optimization (version 2) learning record 7
Browser screenshot method (long screenshot, node screenshot, designated area screenshot)
[thread / multithread] execution sequence of threads
15 inclusivegateway and eventgateway of flowable gateway
Case - traversing the directory (file class & recursive call)
No assessment summary
2021-9-19