rails-profiler 0.45.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,10 +25,21 @@ module Profiler
25
25
  Collectors::FlameGraphCollector
26
26
  ].freeze
27
27
 
28
+ RECORDING_KEY = :profiler_console_recording
29
+
28
30
  def self.profile(expression:, &block)
29
31
  return block.call unless Profiler.enabled? && Profiler.configuration.track_console
30
32
 
31
- new(expression: expression).run(&block)
33
+ # IRB and Pry can both be loaded and both evaluate the same expression: only the
34
+ # outermost one records it.
35
+ return block.call if Thread.current[RECORDING_KEY]
36
+
37
+ begin
38
+ Thread.current[RECORDING_KEY] = true
39
+ new(expression: expression).run(&block)
40
+ ensure
41
+ Thread.current[RECORDING_KEY] = nil
42
+ end
32
43
  end
33
44
 
34
45
  def initialize(expression:)
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Instrumentation
5
+ # Prepended to Pry. Pry commands (ls, cd...) do not go through #evaluate_ruby and are not profiled.
6
+ module PryInstrumentation
7
+ def evaluate_ruby(code)
8
+ Profiler.env_override_store.apply!
9
+ stripped = code.to_s.strip
10
+ skip = !Profiler.enabled? ||
11
+ !Profiler.configuration.track_console ||
12
+ stripped.empty?
13
+ return super if skip
14
+
15
+ Profiler::ConsoleProfiler.profile(expression: stripped) { super }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -169,8 +169,17 @@ module Profiler
169
169
  next unless Profiler.configuration.enabled && Profiler.configuration.track_console
170
170
 
171
171
  require_relative "console_profiler"
172
- require_relative "instrumentation/irb_instrumentation"
173
- IRB::Context.prepend(Profiler::Instrumentation::IrbInstrumentation)
172
+
173
+ # A console is IRB or Pry (pry-rails): instrument whichever is loaded, never assume IRB.
174
+ if defined?(IRB::Context)
175
+ require_relative "instrumentation/irb_instrumentation"
176
+ IRB::Context.prepend(Profiler::Instrumentation::IrbInstrumentation)
177
+ end
178
+
179
+ if defined?(Pry)
180
+ require_relative "instrumentation/pry_instrumentation"
181
+ Pry.prepend(Profiler::Instrumentation::PryInstrumentation)
182
+ end
174
183
  end
175
184
 
176
185
  rake_tasks do
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.45.0"
4
+ VERSION = "0.45.1"
5
5
 
6
6
  GEM_ROOT = File.expand_path("../..", __dir__)
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.45.0
4
+ version: 0.45.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
@@ -197,6 +197,7 @@ files:
197
197
  - lib/profiler/instrumentation/executor_context_propagation.rb
198
198
  - lib/profiler/instrumentation/irb_instrumentation.rb
199
199
  - lib/profiler/instrumentation/net_http_instrumentation.rb
200
+ - lib/profiler/instrumentation/pry_instrumentation.rb
200
201
  - lib/profiler/instrumentation/request_context.rb
201
202
  - lib/profiler/instrumentation/sidekiq_middleware.rb
202
203
  - lib/profiler/instrumentation/thread_context_propagation.rb