Zorka source code interpretation through the BeanShell of the process of inserting piles

Source: Internet
Author: User
Tags add time

An overview of the Zorka in the process of inserting piles

1, in the spydefinition to configure the plug-in properties, the Spydefinition instance is submitted to the plug-in engine.
2, the spydefinition example contains the insertion pile probe Probes,probe inserted into the method, the execution of the method is monitored.
The insertion phase of the method consists of three main stages: the start phase (entry), the return phase ("returns"), and the exception phase (error).
Each probe is inserted into the method according to its specified stage, capturing the data, such as the current time, the method parameters, the variables inside the method, and so on.
The data captured by the probe is encapsulated as a Spyrecord instance (Symbolicrecord), which records the record.
3. According to the stage defined in probe, the record will be submitted to different processor chains for processing.
Records of all probes for a method are grouped together and submitted to the submit chain for processing.
The above procedure is unique to the thread of a method call (to ensure thread safety)
4. The last record is presented to the Collect chain (single thread), and collector updates the statistics and writes the record to the corresponding stream.
We want to customize the insert pile, mainly by writing BeanShell to define the insertion pile probe probe, define the insertion pile record processor processor, specify the method and location of the pile.

Demo

Example 1: Log the URL of the request for Tomcat.
(Monitor the Org.apache.catalina.core.StandardEngineValve method, get the URI in the request, and log to the logs)
1, add the following code directly in the CATALINA.BSH, save. (Note: The original Org.apache.catalina.core.StandardEngineValve method of the insertion pile code is commented out, can only be defined once)

 Spy.add (spy.instance ("catalina_log_requests" ). OnEnter (Spy.fetcharg ( " REQ ", 1), Spy.zorkalog (" INFO "," Httpzsy "," ${req.request.requesturi} " "Org.apache.catalina.core.StandardEngineValve", "Invoke"))); 

Description
1), the Spy is in the Zorka boot time has been registered to the BeanShell object, in BeanShell can be used directly.
The role of the spy is to configure the insertion pile engine.
2) The Add method adds an instance of Spydefinition to the spy.
Spy.instance ("Catalina_log_requests") is an Spydefinition object that instantiates a 0 configuration, and catalina_log_requests is the specified name.
3), call the OnEnter method to add configuration information for the Enter stage to the Spydefinition instance.
Configuration information includes Spyprobe instance-probe, spyprocessor instance-record processor
Spy.fetcharg ("REQ", 1): Defines a probe that gets the first parameter of a method and puts it into a variable of REQ.
Spy.zorkalog ("INFO", "Httpzsy", "${req.request.requesturi}") defines a record processor, which is defined by the log collector. The first parameter is the log level, the second is tag, and the third is the log information (this is the URI that gets the request from the REQ variable)
4), the Include method is used to add the dowel location information to the Spydefinition instance.
The timing of the insertion pile is encapsulated in the Spymatcher. This means that the Org.apache.catalina.core.StandardEngineValve method is called when the pile is inserted.
In summary, when the Org.apache.catalina.core.StandardEngineValve method is called, the first parameter is fetched, and the URI information in the extraction parameter is written to the Zorka log.
2. Restart Tomcat, or click Reload in Jconsole to reload the Zorka configuration.

3. Access the Web under local tomcat, such as http://localhost:8080/
4, open the Zorka under the log, such as D:\apache-tomcat-7.0.62-windows-x64\apache-tomcat-7.0.62\zorka\log\zorka.log

Example 2: Monitor HTTP request, return, execution time and other information, choose whether to perform the corresponding monitoring trace according to the configuration file.
Let's analyze the HTTP monitoring that comes with the CATALINA.BSH. The following are such complex monitoring, mainly added to the configuration file based on the configuration zorka.properties to add monitoring and record tracking.
1. Code Analysis

Spy.add (Spy.instrument ("Catalina_http")//The method is also called Spydefinition.instance to instantiate a spydefinition instance, and add time to monitor the probe and calculate the processor of the runtime. . OnEnter (//add a method into the probe stageSpy.fetcharg ("REQ", 1),//gets the first parameter into the Req, which is the fetch request objectSpy.fetcharg ("RESP", 2),//gets the second argument into the RESP, which gets the response objectSpy.format ("URI", "${req.request.requesturi}"),//get URIHttp._trace? Spy.subchain (//if Http.trace=yes (the default is to turn on HTTP tracing), add the Logicalfilterprocessor processor (containing a bunch of sub-processor, traverse the record, and return the original record if the result is empty) Http._uriqualifier,//URI matching processor for filtering URLs based on http.trace.exclude and http.trace.include configuration ItemsTracer.begin ("HTTP", Zorka.longcfg ("Http.trace.time")),//Add a tracebeginprocessor to start a new trace (call Tracebuilder.tracebegin). This indicates that a trace with a name of HTTP is started, and the minimum tracking time is specified for the configuration item. Tracer.attr ("uri", "uri")//returns a traceattrprocessor that is used to add attributes to the record. This is used to place the URI in the source field in the URI variable. ) :NULL, Http._congestion? Spy.zorkastats ("Java", Http._mbean, "stats", "All", "T",NULL, Spy. Action_enter):NULL//Add method call statistics processor if Http.stats.congestion is configured to True). OnReturn (Spy.put ("ERR", ""))//when the method returns, put a null value to the ERR variable. OnError (Spy.fetcherror ("EX"),//adds an exception-caught probe, when the method throws an exception, probe catches the exception objectSpy.format ("ERR", "${ex}"),//formatting the exception and putting it into the ERR variableHttp._trace? Spy.subchain (//http trace is turned on, add the following processorHttp._uriqualifier,//URL Filtering processorHttp._error_chain,//If Http.error = yes, add the appropriate trappercollector to send the error record to the record collectorTracer.flags (Tracer. Submit_trace), Tracer.attr ("Err", "err")) : NULL). OnSubmit (//Submission PhaseSpy.strtime ("Time"),//Get method Execution TimeSpy.format ("STATUS", "${resp.status}"),//GET request StatusSpy.get ("USER", "REQ", "RemoteUser"),//get remote User addressHttp._trace?Spy.subchain (http._uriqualifier,tracer.attr ("Status", "status"), Tracer.filterby ("STATUS",NULL, Http._errors,NULL,NULL), Tracer.attr ("User", "user"), Http._cookies? Http.cookies_processor (true,true): Spy.put ("COOKIES", "" "), Http._headers? Headers_processor ():NULL, Http._params? Http.param_processor (): Spy.put ("PARAMS", "" ")) : NULL, Http._slow_chain,spy.subchain (Spy.valsetfilter ("STATUS", Http._errors), Spy.markerror (), Tracer.markerror (), Http._error_chain), Http._redirqualifier,http._stats? Spy.zorkastats ("Java", Http._mbean, "stats", "All", "T",NULL, http._action):NULL, Http._tags_chain). Include (Spy.bymethod ("Org.apache.catalina.core.StandardEngineValve", "invoke"));//the insertion pile is executed when the Org.apache.catalina.core.StandardEngineValve method is called. 

2. Monitoring results
We use Zico to view the monitoring results, we can see that the above BSH script specifies the length of the monitoring, the number of calls, error messages, URLs, request status, etc. are recorded.

Zorka source code interpretation through the BeanShell of the process of inserting piles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.