rails-profiler 0.45.4 → 0.46.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b4332eee9a830876c3ffe5c137d94543461a2ad84f05e9c32943c8aac2ed8a1
4
- data.tar.gz: b004d3659cf24ddddae10cbdc416922ccf827ece582b1681417b456c1037d96c
3
+ metadata.gz: 3edc716df0abddb6767cc8099a84ae1b9dd2123c7178fef125aae17b49a68acb
4
+ data.tar.gz: a1f038e8bd3e8b3e6210e4e22f1da7733f3e576bcea4e2dc637e427ff28aaa14
5
5
  SHA512:
6
- metadata.gz: ea60c6cfdac1a5be97ea9c4b7f7e12b6968ada0b0a1e3d7df5edead621cd2b43fa567b69b217351ac46df0af149c772a98591e4b24f84586d9c677f748422ab7
7
- data.tar.gz: 995ee37e14b80fca5949f3a49c2ee11fedf0a08e992b0c37435b83c6fea24c4e6193b3971a93df125288f681897e630f5e7ae06a940f4413c9592d9e4825a6fa
6
+ metadata.gz: a68f1541357a93832c42457f4729cf71738b9157de2c30a546460c4021ca59058204ebd4df8bcdc09777eefc10a48d28f902b9f713af1fd63271f481f249e923
7
+ data.tar.gz: f408c3d66eb3910f21963482cc142a70d227f9216cff63a48e4de7ab916355b860ea751a8a3c3726a35171881d2ee33314c2ae8a3cdea3a9e33ae40bf03b8b90
data/CHANGELOG.md CHANGED
@@ -19,6 +19,23 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.46.1] - 2026-10-28
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Fixed
27
+
28
+ - A release that did not complete now blocks the branch pipelines: the `release:backlog` job fails, naming the version and what to do, while master stamps a version without a tag (`fix/release-*` branches are exempt). The release job no longer dies silently of SIGPIPE (status 141) when several versions are waiting.
29
+
30
+ ## [0.46.0] - 2026-10-28
31
+
32
+ <!-- stamped -->
33
+
34
+ ### Added
35
+
36
+ - RSpec and Minitest are hooked automatically when `track_tests` is on and the framework is loaded: no `RSpecSupport.install` or `MinitestSupport.install` in the app. Installing by hand as well no longer doubles the profiles.
37
+ - `rails runner` executions are profiled, as console profiles with `source: "runner"`: expression (code or script path), return value, failure with its exception, saved before the process exits. `track_runner` is on in development, `runner_skip` lists what to leave out. `query_console_profiles` gains a `source` filter and column. A successful `exit` in a console or runner is no longer recorded as a failure.
38
+
22
39
  ## [0.45.4] - 2026-10-27
23
40
 
24
41
  <!-- stamped -->
@@ -5,9 +5,10 @@ require_relative "base_collector"
5
5
  module Profiler
6
6
  module Collectors
7
7
  class ConsoleCollector < BaseCollector
8
- def initialize(profile, expression:)
8
+ def initialize(profile, expression:, source: "console")
9
9
  super(profile)
10
10
  @expression = expression
11
+ @source = source
11
12
  @return_value = nil
12
13
  @return_value_captured = false
13
14
  end
@@ -40,7 +41,7 @@ module Profiler
40
41
  end
41
42
 
42
43
  def collect
43
- data = { expression: @expression }
44
+ data = { expression: @expression, source: @source }
44
45
  data[:return_value] = @return_value if @return_value_captured
45
46
  store_data(data)
46
47
  end
@@ -30,7 +30,7 @@ module Profiler
30
30
 
31
31
  # Options whose default depends on the Rails environment: the railtie sets them only when
32
32
  # the application has not assigned them itself (see #default).
33
- RAILS_DEFAULTED = %i[enabled storage track_tests max_profiles].freeze
33
+ RAILS_DEFAULTED = %i[enabled storage track_tests track_runner max_profiles].freeze
34
34
 
35
35
  BACKEND_LOCK = Mutex.new
36
36
 
@@ -54,7 +54,7 @@ module Profiler
54
54
  :track_http, :slow_http_threshold, :http_backtrace_depth,
55
55
  :otel_enabled, :otel_service_name, :otel_otlp_endpoint,
56
56
  :track_jobs,
57
- :track_console,
57
+ :track_console, :runner_skip,
58
58
  :apply_env_overrides_when_disabled, :restrict_storage_permissions,
59
59
  :test_runner_allow_undiscovered_files, :allow_explain_outside_development,
60
60
  :test_runner_kill_grace_seconds,
@@ -72,7 +72,7 @@ module Profiler
72
72
  :regression_query_threshold, :regression_cache_threshold,
73
73
  :replay_enabled, :replay_max_count, :replay_timeout
74
74
 
75
- attr_reader :authorize_block, :enabled, :track_tests, :max_profiles, :editor_url_scheme, :editor_path_map
75
+ attr_reader :authorize_block, :enabled, :track_tests, :track_runner, :max_profiles, :editor_url_scheme, :editor_path_map
76
76
 
77
77
  def initialize
78
78
  @assigned = []
@@ -129,6 +129,10 @@ module Profiler
129
129
  @otel_otlp_endpoint = nil
130
130
  @track_jobs = true
131
131
  @track_console = true
132
+ @track_runner = false
133
+ # Runner executions left out: Regexps matched against the code or script path, Strings
134
+ # compared to it whole.
135
+ @runner_skip = [/\AProfiler\b/]
132
136
  @apply_env_overrides_when_disabled = false
133
137
  @track_tests = false
134
138
  # The test runner only runs the files listed by its discovery. true restores the
@@ -330,6 +334,17 @@ module Profiler
330
334
  @enabled = value
331
335
  end
332
336
 
337
+ def track_runner=(value)
338
+ @assigned |= [:track_runner]
339
+ @track_runner = value
340
+ end
341
+
342
+ def runner_skipped?(expression)
343
+ Array(runner_skip).any? do |pattern|
344
+ pattern.is_a?(Regexp) ? pattern.match?(expression.to_s) : pattern == expression.to_s
345
+ end
346
+ end
347
+
333
348
  def track_tests=(value)
334
349
  @assigned |= [:track_tests]
335
350
  @track_tests = value
@@ -27,8 +27,10 @@ module Profiler
27
27
 
28
28
  RECORDING_KEY = :profiler_console_recording
29
29
 
30
- def self.profile(expression:, &block)
31
- return block.call unless Profiler.enabled? && Profiler.configuration.track_console
30
+ # source is "console" for an expression typed in IRB or Pry, "runner" for a
31
+ # `rails runner` execution; each has its own track_* option.
32
+ def self.profile(expression:, source: "console", &block)
33
+ return block.call unless Profiler.enabled? && tracked?(source)
32
34
 
33
35
  # IRB and Pry can both be loaded and both evaluate the same expression: only the
34
36
  # outermost one records it.
@@ -36,14 +38,19 @@ module Profiler
36
38
 
37
39
  begin
38
40
  Thread.current[RECORDING_KEY] = true
39
- new(expression: expression).run(&block)
41
+ new(expression: expression, source: source).run(&block)
40
42
  ensure
41
43
  Thread.current[RECORDING_KEY] = nil
42
44
  end
43
45
  end
44
46
 
45
- def initialize(expression:)
47
+ def self.tracked?(source)
48
+ source == "runner" ? Profiler.configuration.track_runner : Profiler.configuration.track_console
49
+ end
50
+
51
+ def initialize(expression:, source: "console")
46
52
  @expression = expression
53
+ @source = source
47
54
  end
48
55
 
49
56
  def run(&block)
@@ -51,9 +58,9 @@ module Profiler
51
58
  profile.profile_type = "console"
52
59
  profile.gem_version = Profiler.version
53
60
  profile.path = Redaction.truncate(@expression, 200)
54
- profile.method = "CONSOLE"
61
+ profile.method = @source == "runner" ? "RUNNER" : "CONSOLE"
55
62
 
56
- console_collector = Collectors::ConsoleCollector.new(profile, expression: @expression)
63
+ console_collector = Collectors::ConsoleCollector.new(profile, expression: @expression, source: @source)
57
64
  collectors = [console_collector] + CONSOLE_COLLECTOR_CLASSES.map { |klass| klass.new(profile) }
58
65
  return block.call unless Collectors::Lifecycle.subscribe_all(collectors, "ConsoleProfiler")
59
66
 
@@ -71,8 +78,11 @@ module Profiler
71
78
  console_collector.set_return_value(result)
72
79
  result
73
80
  rescue Exception => e # rubocop:disable Lint/RescueException
74
- console_status = "failed"
75
- exception_collector&.capture(e)
81
+ # `exit` or `exit 0` in a runner script is a normal end, not a failure.
82
+ unless e.is_a?(SystemExit) && e.success?
83
+ console_status = "failed"
84
+ exception_collector&.capture(e)
85
+ end
76
86
  raise
77
87
  ensure
78
88
  Profiler::CurrentContext.token = previous_token
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Instrumentation
5
+ # Prepended to Rails.application.executor by the railtie's runner hook, which runs before
6
+ # the code does. `rails runner` evaluates the code inside executor.wrap with this source:
7
+ # the one place that sees the whole execution, its result and its exception. Skipped with
8
+ # `runner -w`, which does not use the executor.
9
+ module RunnerInstrumentation
10
+ RUNNER_SOURCE = "application.runner.railties"
11
+
12
+ def wrap(*args, **options, &block)
13
+ return super unless options[:source] == RUNNER_SOURCE && block
14
+
15
+ expression = RunnerInstrumentation.expression_for(block)
16
+ return super if Profiler.configuration.runner_skipped?(expression)
17
+
18
+ Profiler::ConsoleProfiler.profile(expression: expression, source: "runner") do
19
+ super(*args, **options, &block)
20
+ end
21
+ end
22
+
23
+ # The runner's own block closes over the command line: code_or_file is the code, the
24
+ # script path, or "-" for standard input.
25
+ def self.expression_for(block)
26
+ value = block.binding.local_variable_get(:code_or_file).to_s
27
+ value == "-" ? "(stdin)" : value
28
+ rescue NameError
29
+ "(runner)"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -366,10 +366,11 @@ module Profiler
366
366
  input_schema: {
367
367
  properties: {
368
368
  expression: { type: "string", description: "Filter by expression content (partial match, e.g. 'User.find')" },
369
+ source: { type: "string", description: "Filter by origin: 'console' (rails console) or 'runner' (rails runner)" },
369
370
  status: { type: "string", description: "Filter by status: 'completed' or 'failed'" },
370
371
  min_duration: { type: "number", description: "Minimum duration in milliseconds" },
371
372
  limit: { type: "number", description: "Maximum number of results (default 20)" },
372
- fields: { type: "array", items: { type: "string" }, description: "Columns to include. Valid values: time, expression, return_value, status, duration, queries, token. Omit for all." },
373
+ fields: { type: "array", items: { type: "string" }, description: "Columns to include. Valid values: time, source, expression, return_value, status, duration, queries, token. Omit for all." },
373
374
  cursor: { type: "string", description: "Pagination cursor: ISO8601 timestamp of the last item seen. Returns profiles older than this." },
374
375
  slave: slave_param
375
376
  }
@@ -6,7 +6,7 @@ module Profiler
6
6
  module MCP
7
7
  module Tools
8
8
  class QueryConsoleProfiles
9
- ALL_FIELDS = %w[time expression return_value status duration queries token].freeze
9
+ ALL_FIELDS = %w[time source expression return_value status duration queries token].freeze
10
10
 
11
11
  def self.call(params)
12
12
  limit = [[params["limit"]&.to_i || 20, 1].max, 500].min
@@ -24,6 +24,12 @@ module Profiler
24
24
  end
25
25
  end
26
26
 
27
+ if params["source"]
28
+ consoles = consoles.select do |p|
29
+ (p.collector_data("console") || {}).fetch("source", "console") == params["source"]
30
+ end
31
+ end
32
+
27
33
  if params["status"]
28
34
  consoles = consoles.select do |p|
29
35
  case params["status"]
@@ -78,6 +84,7 @@ module Profiler
78
84
  row = fields.map do |f|
79
85
  case f
80
86
  when "time" then profile.started_at&.strftime("%H:%M:%S") || "-"
87
+ when "source" then console_data.fetch("source", "console")
81
88
  when "expression" then console_data["expression"].to_s.then { |e| e.length > 60 ? "#{e[0, 57]}..." : e }
82
89
  when "return_value" then console_data["return_value"].to_s.then { |v| v.length > 80 ? "#{v[0, 77]}..." : v }
83
90
  when "status" then profile.status == 200 ? "completed" : "failed"
@@ -15,6 +15,7 @@ module Profiler
15
15
  config.default(:enabled, development_or_test)
16
16
  config.default(:storage, development_or_test ? :file : :memory)
17
17
  config.default(:track_tests, Rails.env.test?)
18
+ config.default(:track_runner, Rails.env.development?)
18
19
  # One test profile per example: no cap on the count in the test environment, so that the
19
20
  # report at the end of a suite sees all of them.
20
21
  config.default(:max_profiles, Rails.env.test? ? nil : 100)
@@ -93,6 +94,11 @@ module Profiler
93
94
  require_relative "test_runner/discovery"
94
95
  require_relative "test_runner/run_store"
95
96
  require_relative "test_runner/runner"
97
+
98
+ if defined?(RSpec) && RSpec.respond_to?(:configure)
99
+ RSpec.configure { |config| Profiler::TestHelpers::RSpecSupport.install(config) }
100
+ end
101
+ Profiler::TestHelpers::MinitestSupport.install if defined?(Minitest::Test)
96
102
  end
97
103
 
98
104
  initializer "profiler.setup_job_instrumentation" do
@@ -182,6 +188,16 @@ module Profiler
182
188
  end
183
189
  end
184
190
 
191
+ runner do
192
+ next unless Profiler.configuration.enabled && Profiler.configuration.track_runner
193
+
194
+ require_relative "console_profiler"
195
+ require_relative "instrumentation/runner_instrumentation"
196
+ executor = Rails.application.executor
197
+ executor.singleton_class.prepend(Profiler::Instrumentation::RunnerInstrumentation) unless
198
+ executor.singleton_class.include?(Profiler::Instrumentation::RunnerInstrumentation)
199
+ end
200
+
185
201
  rake_tasks do
186
202
  load "profiler/tasks/profiler.rake"
187
203
  end
@@ -12,8 +12,15 @@ module Profiler
12
12
  # require 'profiler/test_helpers/minitest_support'
13
13
  # Profiler::TestHelpers::MinitestSupport.install
14
14
  module MinitestSupport
15
+ def self.installed?
16
+ !!@installed
17
+ end
18
+
15
19
  def self.install
16
20
  require "minitest"
21
+ return if installed?
22
+
23
+ @installed = true
17
24
 
18
25
  Minitest::Test.prepend(RunWrapper)
19
26
 
@@ -13,8 +13,21 @@ module Profiler
13
13
  # RSpec.configure do |config|
14
14
  # Profiler::TestHelpers::RSpecSupport.install(config)
15
15
  # end
16
+ #
17
+ # The railtie calls it itself when RSpec is loaded in the test
18
+ # environment, and a second call on the same configuration does nothing.
16
19
  module RSpecSupport
20
+ @installed = {}.compare_by_identity
21
+
22
+ def self.installed?(config)
23
+ @installed.key?(config)
24
+ end
25
+
17
26
  def self.install(config)
27
+ return if installed?(config)
28
+
29
+ @installed[config] = true
30
+
18
31
  hook = proc do |example|
19
32
  Profiler::TestProfiler.profile(
20
33
  test_name: example.full_description,
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.45.4"
4
+ VERSION = "0.46.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.4
4
+ version: 0.46.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
@@ -200,6 +200,7 @@ files:
200
200
  - lib/profiler/instrumentation/net_http_instrumentation.rb
201
201
  - lib/profiler/instrumentation/pry_instrumentation.rb
202
202
  - lib/profiler/instrumentation/request_context.rb
203
+ - lib/profiler/instrumentation/runner_instrumentation.rb
203
204
  - lib/profiler/instrumentation/sidekiq_middleware.rb
204
205
  - lib/profiler/instrumentation/thread_context_propagation.rb
205
206
  - lib/profiler/job_profiler.rb