@@ -22,7 +22,30 @@ module Utils
2222 attach_function :thread_id , [ ] , :uint64
2323 end
2424
25- Config = Struct . new ( :application_name , :app_name , :server_address , :auth_token , :log_level , :sample_rate , :detect_subprocesses , :oncpu , :report_pid , :report_thread_id , :tags , :compression , :report_encoding ) do
25+ class Engine < Rails ::Engine
26+ config . after_initialize do
27+ next unless Pyroscope . current_config && Pyroscope . current_config . autoinstrument_rails
28+
29+ ::Pyroscope . initialize_rails_hooks
30+ end
31+ end
32+
33+ Config = Struct . new (
34+ :application_name ,
35+ :app_name ,
36+ :server_address ,
37+ :auth_token ,
38+ :log_level ,
39+ :sample_rate ,
40+ :detect_subprocesses ,
41+ :oncpu ,
42+ :report_pid ,
43+ :report_thread_id ,
44+ :tags ,
45+ :compression ,
46+ :report_encoding ,
47+ :autoinstrument_rails ,
48+ ) do
2649 def initialize ( *)
2750 super
2851 # defaults:
@@ -38,10 +61,15 @@ def initialize(*)
3861 self . tags = { }
3962 self . compression = 'gzip'
4063 self . report_encoding = 'pprof'
64+ self . autoinstrument_rails = true
4165 end
4266 end
4367
4468 class << self
69+ def current_config
70+ @config
71+ end
72+
4573 def configure
4674 @config = Config . new
4775
@@ -64,11 +92,8 @@ def configure
6492 @log_level = 50
6593 end
6694
67- # Initialize Logging
6895 Rust . initialize_logging ( @log_level )
6996
70-
71- # initialize Pyroscope Agent
7297 Rust . initialize_agent (
7398 # these are defaults in case user-provided values are nil:
7499 @config . app_name || @config . application_name || "" ,
@@ -85,6 +110,17 @@ def configure
85110 )
86111 end
87112
113+ def initialize_rails_hooks
114+ block = lambda do |ctrl , action |
115+ Pyroscope . tag_wrapper ( {
116+ "action" => "#{ ctrl . controller_name } /#{ ctrl . action_name } "
117+ } , &action )
118+ end
119+
120+ ActionController ::API . __send__ ( :around_action , block ) if defined? ActionController ::API
121+ ActionController ::Base . __send__ ( :around_action , block ) if defined? ActionController ::Base
122+ end
123+
88124 def tag_wrapper ( tags )
89125 tid = thread_id
90126 _add_tags ( tid , tags )
@@ -103,32 +139,30 @@ def remove_tags(*tags)
103139 warn ( "deprecated. Use `Pyroscope.tag_wrapper` instead." )
104140 end
105141
106- # convert tags object to string
107- def tags_to_string ( tags )
108- tags . map { |k , v | "#{ k } =#{ v } " } . join ( ',' )
109- end
110-
111- # get thread id
112142 def thread_id
113143 return Utils . thread_id
114144 end
115145
116- # add tags
117146 def _add_tags ( thread_id , tags )
118147 tags . each do |tag_name , tag_value |
119148 Rust . add_thread_tag ( thread_id , tag_name . to_s , tag_value . to_s )
120149 end
121150 end
122151
123- # remove tags
124152 def _remove_tags ( thread_id , tags )
125153 tags . each do |tag_name , tag_value |
126154 Rust . remove_thread_tag ( thread_id , tag_name . to_s , tag_value . to_s )
127155 end
128156 end
129157
130- def shutdown
158+ def stop
131159 Rust . drop_agent
132160 end
161+
162+ private
163+
164+ def tags_to_string ( tags )
165+ tags . map { |k , v | "#{ k } =#{ v } " } . join ( ',' )
166+ end
133167 end
134168end
0 commit comments