ProfilerPlugin
works in the order of following steps:
Pinpoint Agent is started when the JVM starts.
Pinpoint Agent loads all plugins under
plugin
directory.Pinpoint Agent invokes
ProfilerPlugin.setup(ProfilerPluginSetupContext)
for each loaded plugin.In the
setup
method, the plugin registers aTransformerCallback
to all classes that are going to be transformed.Target application starts.
Every time a class is loaded, Pinpoint Agent looks for the
TransformerCallback
registered to the class.If a
TransformerCallback
is registered, the Agent invokes it’sdoInTransform
method.TransformerCallback
modifies the target class’ byte code. (e.g. add interceptors, add fields, etc.)The modified byte code is returned to the JVM, and the class is loaded with the returned byte code.
Application continues running.
When a modified method is invoked, the injected interceptor’s
before
andafter
methods are invoked.The interceptor records the trace data.
The most important points to consider when writing a plugin are 1) figuring out which methods are interesting enough to warrant tracing, and 2) injecting interceptors to actually trace these methods. These interceptors are used to extract, store, and pass trace data around before they are sent off to the Collector. Interceptors may even cooperate with each other, sharing context between them. Plugins may also aid in tracing by adding getters or even custom fields to the target class so that the interceptors may access them during execution. Pinpoint plugin sample shows you how the TransformerCallback
modifies classes and what the injected interceptors do to trace a method.
熟悉pinpoint插件机制的同学对以上过程应该很熟悉了。小编今天想带大家学习的是为什么pinpoint的插件会生效?pinpoint agent加载机制是什么样的呢?
首先我们先来回顾下java agent机制。Agent分为两种,一种是在主程序之前运行的Agent(premain),一种是在主程序之后运行的Agent(agentmain),第二种方式可以理解为前者的升级版,jdk1.6以后提供。这两者的区别这里就不过多介绍。 我们来看下pinpoint-bootstrap-2.0.2.jar包中的MANIFEST.MF中是如何定义的:Manifest-Version: 1.0Implementation-Title: pinpoint-bootstrapImplementation-Version: 2.0.2Built-By: irteamC