ActiveSupport :: Notifications transaction_id

时间:2012-10-16 14:05:37

标签: ruby-on-rails transactions notifications

我一直在浏览Github上的Rails存储库以找到transaction_id的定义,但是不成功。

它是什么类型的交易?看起来相同的transaction_id用于一组事件。

ActiveSupport::Notifications.subscribe do |name, start, finish, transaction_id , payload|
  Rails.logger.debug(["notification:", name, start, finish, transaction_id , payload].join(" "))
end

结果:

Started GET "/" for 127.0.0.1 at 2012-10-16 15:51:40 +0200

notification: sql.active_record 2012-10-16 15:51:40 +0200 2012-10-16 15:51:40 +0200 9eb707034598e4cc7c32 {:sql=>"SHOW client_min_messages", :name=>"SCHEMA", :connection_id=>46235640, :binds=>[]}

notification: sql.active_record 2012-10-16 15:51:40 +0200 2012-10-16 15:51:40 +0200 9eb707034598e4cc7c32 {:sql=>"SET client_min_messages TO 'panic'", :name=>"SCHEMA", :connection_id=>46235640, :binds=>[]}

notification: sql.active_record 2012-10-16 15:51:40 +0200 2012-10-16 15:51:40 +0200 9eb707034598e4cc7c32 {:sql=>"SET standard_conforming_strings = on", :name=>"SCHEMA", :connection_id=>46235640, :binds=>[]}

notification: sql.active_record 2012-10-16 15:51:40 +0200 2012-10-16 15:51:40 +0200 9eb707034598e4cc7c32 {:sql=>"SET client_min_messages TO 'notice'", :name=>"SCHEMA", :connection_id=>46235640, :binds=>[]}

notification: sql.active_record 2012-10-16 15:51:40 +0200 2012-10-16 15:51:40 +0200 9eb707034598e4cc7c32 {:sql=>"SET time zone 'UTC'", :name=>"SCHEMA", :connection_id=>46235640, :binds=>[]}

...

notification: process_action.action_controller 2012-10-16 15:51:40 +0200 2012-10-16 15:51:41 +0200 9eb707034598e4cc7c32 {:controller=>"PagesController", :action=>"index", :params=>{"controller"=>"pages", "action"=>"index"}, :format=>:html, :method=>"GET", :path=>"/", :status=>200, :view_runtime=>339.344333, :db_runtime=>31.421587}

相同的transaction_id:9eb707034598e4cc7c32

1 个答案:

答案 0 :(得分:4)

我只是深入研究这个,希望有一些方法可以从LogSubscriber中获取请求。没有这样的运气。

Activesupport :: Notifications :: Instrumenter有一个唯一的id,在初始化时初始化为SecureRandom.hex(10)。此指导者创建的任何永久物都会获得10位十六进制数字。

在ActiveSupport :: Notifications中,指示器从Thread.current中挑出,如果不存在则创建 - 因此在单个线程范围内发送的所有通知都将具有相同的transaction_d,将它们统一在一起(这很好) ),但该transaction_id的初始值只是一个10位的随机十六进制。 (在Activesupport :: Notifications :: Instrumenter#unique_id中定义)