Basic implementation principles of Ruby Probes

Source: Internet
Author: User

Language itself

The Ruby language supports syntax-level systems, frameworks, and even language-based methods for replication, commonly called meta-programming (meta-programming), on top of which are terminology mixin, dynamic definition of methods, runtime class rewriting, and so on, which allow the language itself to be real Now other languages require bytecode in order to achieve the function, for example, the probe requires HttpRequest a hook request method, can be implemented in the following way:


class HttpRequest def request_new puts ‘before request‘ result = request_old puts ‘after request‘ result end alias_method :request_old, :request alias_method :request, :request_newend

Here is just one of the simplest ways to show it, and there are many other ways, such as we want to completely abandon the original method, then we can directly cover the method.

Framework

Some frameworks, such as rails, provide the pub-sub mechanism, in which case the probe only needs to subscribe to a specific type of message, and then the data is re-processed. For rails, this is easier:


module ActionController class PageRequest def call(name, started, finished, unique_id, payload) Rails.logger.debug [name, started, finished, unique_id, payload].join(‘ ‘) end endendActiveSupport::Notifications.subscribe(‘process_action.action_controller‘, ActionController::PageRequest.new)# 每次访问就能够订阅到‘process_action.action_controller’的消息:## process_action.action_controller 2012-04-13 01:08:35 +0300 2012-04-13 01:08:35 +0300 af358ed7fab884532ec7 {# controller: "Devise::SessionsController",# action: "new",# params: {"action"=>"new", "controller"=>"devise/sessions"},# format: :html,# method: "GET",# path: "/login/sign_in",# status: 200,# view_runtime: 279.3080806732178,# db_runtime: 40.053# }

So we can get this. requestData, subscribe to the message type and consult the documentation for the relevant framework.

RACK

Rack is definitely a good thing, it defines the interface of almost all web frameworks and servers, and is so concise, it is a call method. One of the simplest rack applications is as follows:


# configu.ruclass App def call [200, {"Content-Type" => "text/html"}, ["hello world"]] endendrun app# rackup

Just need this call method to be done, and multi-layered rack middlewareIt's just a nested call. callmethod, all to hook, here is enough, but because here is basically the root, you need to pay attention to the amount of data captured and stack information, too long will affect performance.

Database

The database layer is basically a hook to the ORM framework, in each query results output will have the execution of time information, exceeding the threshold to invoke the database itself Query Statement optimization tool, and save the output results. The hook method is either "1" or the "2" method, if the framework supports it.

External services

An external service is a http_client similar to a library that accesses an external HTTP request, that is, the method being used to hook the initiator according to the method in "1" request .

Background tasks

Background task mechanism with the above items, but in a non-HTTP server process run, the same mechanism of data fetching, but the classification of different, need to probe in the process of operation, to determine the object state of data fetching, that is, web transactions or non-web transactions, non-web transactions are unified storage stored in a data container in a background task.

Thread Profiler

Thread Profiler is a enhanced version of the transaction collector, you can set the sampling period and sampling frequency, the cycle of transaction requests for data aggregation processing, and then statistics on the cycle of the stack call the ratio of information, and then based on this information to find out the time or the most number of calls method, It is then targeted for optimization.

Summarize

Above, but also some of the most fundamental knowledge of the principle, the details of the point there are many, such as how to do the local storage and processing of data, and data conflict between threads processing, as well as the fork-based multi-process server process of data transmission, in future articles will be for each of the detailed explanation, We also ask for your continued attention.

——-
This article is compiled and collated by ONEAPM engineers. ONEAPM is the emerging leader in China's basic software industry, helping enterprise users and developers easily implement slow program code and real-time crawling of SQL statements. To read more technical articles, please visit the ONEAPM Official technology blog.

Basic implementation principles of Ruby Probes

Related Article

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.