rails-profiler 0.42.0 → 0.44.2

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.
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "profiler/replay"
4
+
5
+ module Profiler
6
+ module Api
7
+ # POST /_profiler/api/profiles/:id/replay. The dashboard asks the user to confirm before it
8
+ # calls this; the confirmation is the client's, not checked here (the MCP tool has none).
9
+ class ReplaysController < ApplicationController
10
+ def create
11
+ options = replay_params
12
+ result = Profiler::Replay.call(params[:id].to_s, options)
13
+ render json: result.to_h
14
+ rescue Profiler::Replay::Disabled => e
15
+ render json: { error: e.message }, status: :forbidden
16
+ rescue Profiler::Replay::NotFound => e
17
+ render json: { error: e.message }, status: :not_found
18
+ rescue Profiler::Replay::Error => e
19
+ render json: { error: e.message }, status: :unprocessable_entity
20
+ end
21
+
22
+ private
23
+
24
+ # Every key of the body goes to Replay, which refuses the ones that name a host or a URL.
25
+ def replay_params
26
+ body = request.request_parameters.to_h
27
+ body.except("id", "controller", "action", "replay", "format")
28
+ end
29
+ end
30
+ end
31
+ end
data/config/routes.rb CHANGED
@@ -39,6 +39,7 @@ Profiler::Engine.routes.draw do
39
39
 
40
40
  resources :profiles, only: [:index, :show, :destroy] do
41
41
  collection { delete :clear }
42
+ member { post :replay, to: "replays#create" }
42
43
  end
43
44
  resources :jobs, only: [:index, :show, :destroy] do
44
45
  collection { delete :clear }
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Alerts
5
+ # Removes from a SQL statement what the shared N1Patterns.normalize_sql lets through: strings
6
+ # with a backslash escape or a doubled quote, double quoted text, comments and dollar-quoted
7
+ # bodies. Used for alerts only; the shared normalization is left as it is.
8
+ #
9
+ # The statement is read twice, with backslash escapes (MySQL) and without (standard SQL). When
10
+ # the two readings disagree, or a quote or a comment opener is left, the statement is cut at
11
+ # the first one: a value is dropped rather than guessed. A literal in a vendor syntax not
12
+ # listed here can still remain, hence also the cut at SQL_LIMIT in Alerts.
13
+ module SqlRedaction
14
+ STRINGS_WITH_BACKSLASH = /'(?:[^'\\]|\\.|'')*'|"(?:[^"\\]|\\.|"")*"/m
15
+ STRINGS_STANDARD = /'(?:[^']|'')*'|"(?:[^"]|"")*"/m
16
+ DOLLAR_QUOTED = /\$(?<tag>[A-Za-z_]\w*|)\$.*?\$\k<tag>\$/m
17
+ COMMENTS = %r{/\*.*?\*/|--[^\n]*|(?<=\s|\A)\#[^\n]*}m
18
+ # Tried left to right, so a quote in a comment, or a comment in a quote, is not read as the other.
19
+ READINGS = [STRINGS_WITH_BACKSLASH, STRINGS_STANDARD].map do |strings|
20
+ Regexp.union(strings, DOLLAR_QUOTED, COMMENTS)
21
+ end.freeze
22
+ # What is left of a literal or a comment when a form was not read to its end.
23
+ OPENER = %r{['"]|/\*|--|\$(?:[A-Za-z_]\w*)?\$|(?<=\s|\A)#}
24
+
25
+ # The statement is cut here before it is read. The dollar-quote pattern scans to the end for
26
+ # each opener, which is quadratic on a long statement full of $ with no closing tag; this
27
+ # bounds it. Alerts only send SQL_LIMIT characters of the normalized statement anyway.
28
+ MAX_LENGTH = 5_000
29
+
30
+ module_function
31
+
32
+ def redact(sql)
33
+ sql = sql[0, MAX_LENGTH] if sql.length > MAX_LENGTH
34
+ readings = READINGS.map { |pattern| sql.gsub(pattern) { |m| m.start_with?("'", '"') ? "?" : " " } }
35
+ return sql[0, sql.index(OPENER) || sql.length] if readings.uniq.size > 1 || readings.first.match?(OPENER)
36
+
37
+ readings.first
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "uri"
6
+
7
+ module Profiler
8
+ module Alerts
9
+ # POSTs an alert as JSON. Called from the detached thread of Alerts.check, which holds nothing
10
+ # of the request, so its own HTTP call is not recorded into a profile and it never delays the
11
+ # request. Errors are logged, never raised.
12
+ module Webhook
13
+ module_function
14
+
15
+ # The URL as a String, nil when none is given. Raises ArgumentError for a URL that cannot be
16
+ # called: not http(s), no host, or credentials in it (never sent, so refused).
17
+ def validate!(value)
18
+ return nil if value.nil? || value.to_s.strip.empty?
19
+
20
+ uri = URI.parse(value.to_s.strip)
21
+ unless uri.is_a?(URI::HTTP) && !uri.host.to_s.empty?
22
+ raise ArgumentError, "config.alert_webhook_url: #{value.to_s.inspect} must be an http(s) URL with a host"
23
+ end
24
+ if uri.userinfo
25
+ raise ArgumentError, "config.alert_webhook_url: credentials in the URL are not sent; " \
26
+ "put the secret in the path or a query parameter of the receiver"
27
+ end
28
+ uri.to_s
29
+ rescue URI::InvalidURIError
30
+ raise ArgumentError, "config.alert_webhook_url: #{value.to_s.inspect} is not a valid URL"
31
+ end
32
+
33
+ def post(alert, url, timeout)
34
+ return if url.nil? || url.to_s.empty?
35
+
36
+ send_request(url, alert, timeout)
37
+ end
38
+
39
+ def send_request(url, alert, timeout)
40
+ response = http_for(URI.parse(url.to_s), timeout).request(request_for(url, alert))
41
+ return if response.is_a?(Net::HTTPSuccess)
42
+
43
+ Profiler.log_error("Alerts: webhook answered #{response.code} for #{alert[:type]}")
44
+ rescue StandardError => e
45
+ Profiler.log_error("Alerts: webhook failed for #{alert[:type]}", e)
46
+ end
47
+
48
+ def http_for(uri, timeout)
49
+ http = Net::HTTP.new(uri.host, uri.port)
50
+ http.use_ssl = uri.scheme == "https"
51
+ http.open_timeout = http.read_timeout = http.write_timeout = timeout
52
+ http
53
+ end
54
+
55
+ def request_for(url, alert)
56
+ request = Net::HTTP::Post.new(URI.parse(url.to_s).request_uri, "Content-Type" => "application/json")
57
+ request.body = JSON.generate(alert)
58
+ request
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Alerts
5
+ # One thread, one bounded queue. Every check (the detections, the hook, the webhook) runs here,
6
+ # one after the other, so a slow or unreachable webhook never piles up threads: when the queue
7
+ # is full the job is dropped and logged. The thread is started on first use, with
8
+ # RequestContext.detached so it holds nothing of the request that started it, and started
9
+ # again when it is gone (a fork does not carry threads over).
10
+ class Worker
11
+ CAPACITY = 100
12
+
13
+ def initialize(capacity: CAPACITY)
14
+ @queue = SizedQueue.new(capacity)
15
+ @lock = Mutex.new
16
+ @idle = ConditionVariable.new
17
+ @pending = 0
18
+ @thread = nil
19
+ @pid = Process.pid
20
+ end
21
+
22
+ # True when queued, false when the queue is full.
23
+ def push(&job)
24
+ ensure_thread
25
+ @lock.synchronize { @pending += 1 }
26
+ @queue.push(job, true)
27
+ true
28
+ rescue ThreadError
29
+ @lock.synchronize { @pending -= 1 }
30
+ false
31
+ end
32
+
33
+ # Waits until every job queued so far is done. For tests and for shutdown.
34
+ def drain(timeout = 10)
35
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
36
+ @lock.synchronize do
37
+ while @pending.positive?
38
+ left = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
39
+ return false unless left.positive?
40
+
41
+ @idle.wait(@lock, left)
42
+ end
43
+ end
44
+ true
45
+ end
46
+
47
+ private
48
+
49
+ def ensure_thread
50
+ @lock.synchronize do
51
+ reset_after_fork
52
+ return if @thread&.alive?
53
+
54
+ @thread = Instrumentation::RequestContext.detached { Thread.new { work } }
55
+ end
56
+ end
57
+
58
+ def reset_after_fork
59
+ return if @pid == Process.pid
60
+
61
+ @pid = Process.pid
62
+ @queue = SizedQueue.new(@queue.max)
63
+ @pending = 0
64
+ @thread = nil
65
+ end
66
+
67
+ def work
68
+ loop do
69
+ job = @queue.pop
70
+ begin
71
+ job.call
72
+ rescue StandardError => e
73
+ Profiler.log_error("Alerts: a check failed", e)
74
+ ensure
75
+ @lock.synchronize do
76
+ @pending -= 1
77
+ @idle.broadcast if @pending <= 0
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,199 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "time"
5
+ require "net/http"
6
+ require "uri"
7
+ require "digest"
8
+ require_relative "alerts/webhook"
9
+ require_relative "alerts/sql_redaction"
10
+ require_relative "alerts/worker"
11
+ # Alerts.normalize_sql wraps N1Patterns.normalize_sql. That file is otherwise only loaded by the MCP
12
+ # server, which an application without MCP never starts, so it is required here, not on first use.
13
+ require_relative "mcp/resources/n1_patterns"
14
+ require_relative "instrumentation/request_context"
15
+ require_relative "collectors/scoped_notifications"
16
+
17
+ module Profiler
18
+ # Threshold alerts. When config.alerts_enabled is true, every profile saved is compared with the
19
+ # thresholds the profiler already has (slow_query_threshold, max_queries_warning,
20
+ # allocated_objects_warning_threshold) and checked for an N+1. Each detection is handed to
21
+ # config.on_detection and, when config.alert_webhook_url is set, POSTed there as JSON.
22
+ #
23
+ # An alert carries no raw data: no body, no parameters, no header, only the SQL statement with
24
+ # its literals replaced with ? (see SqlRedaction) and a fingerprint of it.
25
+ #
26
+ # Nothing here may slow or break the request: the whole check (the detections, which walk the
27
+ # queries of the profile, then the hook and the webhook) runs on the one worker thread of
28
+ # Alerts::Worker, fed through a bounded queue (a full queue drops the check and logs it), so
29
+ # `check` returns at once and a slow webhook never piles up threads.
30
+ # Each family of alert is detected in its own rescue, so an error on one never hides the others.
31
+ #
32
+ # One alert per type and per profile: a profile saved again (a late outbound HTTP call) is not
33
+ # alerted again for a type already delivered. The path of a console profile is the expression
34
+ # typed, which may hold a secret passed as a literal, so it is left out of the payload.
35
+ module Alerts
36
+ TYPES = %w[slow_query too_many_queries excessive_allocations n_plus_one].freeze
37
+ SQL_LIMIT = 200
38
+ N_PLUS_ONE_LIMIT = 10
39
+ DELIVERED_LIMIT = 2000
40
+ DELIVERED_LOCK = Mutex.new
41
+ DELIVERED = {}
42
+ WORKER = Worker.new
43
+
44
+ module_function
45
+
46
+ # Queues the check for the worker thread. True when queued, nil when alerts are off or the
47
+ # queue is full. The profile is saved and no longer written to by then.
48
+ def check(profile)
49
+ config = Profiler.configuration
50
+ return unless config.alerts_enabled
51
+ return true if WORKER.push { run(profile, config) }
52
+
53
+ Profiler.log_error("Alerts: queue full, check of profile #{profile&.token} dropped")
54
+ nil
55
+ rescue StandardError => e
56
+ Profiler.log_error("Alerts: could not start the check of profile #{profile&.token}", e)
57
+ nil
58
+ end
59
+
60
+ def run(profile, config)
61
+ Collectors::ScopedNotifications.forget_inherited_scope
62
+ detections(profile, config).each { |alert| deliver(alert, config) if first_delivery?(alert) }
63
+ rescue StandardError => e
64
+ Profiler.log_error("Alerts: could not check profile #{profile&.token}", e)
65
+ end
66
+
67
+ # True once per profile token and alert type. The store is bounded, oldest token out first.
68
+ def first_delivery?(alert)
69
+ DELIVERED_LOCK.synchronize do
70
+ types = (DELIVERED[alert[:profile_token]] ||= [])
71
+ next false if types.include?(alert[:type])
72
+
73
+ types << alert[:type]
74
+ DELIVERED.shift while DELIVERED.size > DELIVERED_LIMIT
75
+ true
76
+ end
77
+ end
78
+
79
+ # Waits for the queued checks to finish (tests, shutdown).
80
+ def drain(timeout = 10)
81
+ WORKER.drain(timeout)
82
+ end
83
+
84
+ # Every family in its own rescue: an error on one is logged and the others still go out.
85
+ def detections(profile, config)
86
+ base = base_payload(profile)
87
+ queries = queries_of(profile)
88
+ { slow_query: -> { slow_query_alerts(base, queries, config) },
89
+ too_many_queries: -> { too_many_queries_alerts(base, queries, config) },
90
+ excessive_allocations: -> { allocation_alerts(base, profile, config) },
91
+ n_plus_one: -> { n_plus_one_alerts(base, queries) } }
92
+ .flat_map { |family, detect| safely(family, profile, &detect) }
93
+ rescue StandardError => e
94
+ Profiler.log_error("Alerts: could not read profile #{profile&.token}", e)
95
+ []
96
+ end
97
+
98
+ def safely(family, profile)
99
+ yield
100
+ rescue StandardError => e
101
+ Profiler.log_error("Alerts: could not detect #{family} for profile #{profile&.token}", e)
102
+ []
103
+ end
104
+
105
+ def base_payload(profile)
106
+ request = profile.collector_data("request")
107
+ {
108
+ profile_token: profile.token,
109
+ method: profile.method,
110
+ path: profile.profile_type == "console" ? nil : profile.path,
111
+ controller_action: request.is_a?(Hash) ? read(request, :controller_action) : nil,
112
+ status: profile.status,
113
+ detected_at: Time.now.utc.iso8601
114
+ }
115
+ end
116
+
117
+ # A collector's value whatever the keys' type: symbols as the collector built it (the profile
118
+ # Alerts.check receives), strings once the profile is stored and read back. Same principle as
119
+ # Otel::TraceExporter.list.
120
+ def read(hash, key)
121
+ hash.key?(key) ? hash[key] : hash[key.to_s]
122
+ end
123
+
124
+ def queries_of(profile)
125
+ data = profile.collector_data("database")
126
+ list = data.is_a?(Hash) ? read(data, :queries) : nil
127
+ return [] unless list.is_a?(Array)
128
+
129
+ list.filter_map do |q|
130
+ next unless q.is_a?(Hash) && read(q, :sql).is_a?(String)
131
+
132
+ { sql: read(q, :sql), duration: read(q, :duration) }
133
+ end
134
+ end
135
+
136
+ def slow_query_alerts(base, queries, config)
137
+ threshold = config.slow_query_threshold
138
+ return [] unless threshold
139
+
140
+ slow = queries.select { |q| q[:duration].to_f > threshold }
141
+ return [] if slow.empty?
142
+
143
+ slowest = slow.max_by { |q| q[:duration].to_f }
144
+ [base.merge(type: "slow_query", threshold: threshold, unit: "ms", value: slowest[:duration].to_f,
145
+ count: slow.size, query: describe_normalized(normalize_sql(slowest[:sql])))]
146
+ end
147
+
148
+ def too_many_queries_alerts(base, queries, config)
149
+ threshold = config.max_queries_warning
150
+ return [] unless threshold && queries.size > threshold
151
+
152
+ [base.merge(type: "too_many_queries", threshold: threshold, unit: "queries", value: queries.size)]
153
+ end
154
+
155
+ def allocation_alerts(base, profile, config)
156
+ threshold = config.allocated_objects_warning_threshold
157
+ value = profile.allocated_objects
158
+ return [] unless threshold && value && value > threshold
159
+
160
+ [base.merge(type: "excessive_allocations", threshold: threshold, unit: "objects", value: value)]
161
+ end
162
+
163
+ def n_plus_one_alerts(base, queries)
164
+ groups = queries.group_by { |q| normalize_sql(q[:sql]) }
165
+ repeated = groups.select { |_, list| list.size > 1 }
166
+ return [] if repeated.empty?
167
+
168
+ [base.merge(type: "n_plus_one", threshold: 1, unit: "repeats", value: repeated.values.map(&:size).max,
169
+ patterns: patterns_of(repeated))]
170
+ end
171
+
172
+ def patterns_of(repeated)
173
+ repeated.sort_by { |_, list| -list.size }.first(N_PLUS_ONE_LIMIT).map do |normalized, list|
174
+ describe_normalized(normalized).merge(count: list.size)
175
+ end
176
+ end
177
+
178
+ # The shared normalization (numbers, $1 and simple quoted strings) after SqlRedaction.redact.
179
+ def normalize_sql(sql)
180
+ MCP::Resources::N1Patterns.normalize_sql(SqlRedaction.redact(sql))
181
+ end
182
+
183
+ def describe_normalized(normalized)
184
+ { sql: normalized[0, SQL_LIMIT], fingerprint: Digest::SHA256.hexdigest(normalized)[0, 12] }
185
+ end
186
+
187
+ def deliver(alert, config)
188
+ call_hook(alert, config)
189
+ Webhook.post(alert, config.alert_webhook_url, config.alert_webhook_timeout)
190
+ end
191
+
192
+ def call_hook(alert, config)
193
+ hook = config.on_detection
194
+ hook&.call(alert)
195
+ rescue StandardError => e
196
+ Profiler.log_error("Alerts: config.on_detection raised for #{alert[:type]}", e)
197
+ end
198
+ end
199
+ end
@@ -58,7 +58,7 @@ module Profiler
58
58
 
59
59
  # Skip schema queries and internal Rails queries
60
60
  next if payload[:name] == "SCHEMA"
61
- next if payload[:sql] =~ /^(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE)/i
61
+ next if payload[:sql] =~ /\A\s*(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE)\b/i
62
62
 
63
63
  query = Models::SqlQuery.new(
64
64
  sql: payload[:sql],
@@ -82,7 +82,7 @@ module Profiler
82
82
  # SQL queries
83
83
  @subscriptions << subscribe_notification("sql.active_record") do |_name, started, finished, _unique_id, payload|
84
84
  next if payload[:name] == "SCHEMA"
85
- next if payload[:sql] =~ /^(BEGIN|COMMIT|ROLLBACK|SAVEPOINT)/i
85
+ next if payload[:sql] =~ /\A\s*(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE)\b/i
86
86
 
87
87
  sql = payload[:sql].to_s
88
88
  add_event Models::TimelineEvent.new(
@@ -4,7 +4,7 @@ require_relative "base_collector"
4
4
 
5
5
  module Profiler
6
6
  module I18nLookupTracker
7
- def translate(key, **options)
7
+ def translate(key = nil, **options)
8
8
  result = super
9
9
  if (collector = Thread.current[:profiler_i18n_collector])
10
10
  missing = result.is_a?(String) && result.downcase.include?("translation missing:")
@@ -3,6 +3,7 @@
3
3
  require "pathname"
4
4
  require_relative "local_request"
5
5
  require_relative "allocation_counter"
6
+ require_relative "alerts/webhook"
6
7
 
7
8
  module Profiler
8
9
  # Header the profiler's own clients send on every request. A page on another origin cannot
@@ -40,9 +41,12 @@ module Profiler
40
41
  # URL schemes the source links can use. Anything else is never turned into a link.
41
42
  EDITOR_URL_SCHEMES = %w[vscode idea cursor].freeze
42
43
 
44
+ attr_reader :alert_webhook_url
45
+
43
46
  attr_accessor :storage_options, :collectors,
44
47
  :slow_query_threshold, :max_queries_warning, :sql_backtrace,
45
48
  :track_memory, :allocated_objects_warning_threshold,
49
+ :alerts_enabled, :alert_webhook_timeout,
46
50
  :mcp_enabled, :mcp_transport, :mcp_port, :mcp_read_only,
47
51
  :authorization_mode, :extension_cors_enabled,
48
52
  :cors_allowed_origins, :api_forgery_protection, :frame_ancestors,
@@ -65,7 +69,8 @@ module Profiler
65
69
  :collector_circuit_breaker_enabled,
66
70
  :peers, :peer_token, :peer_allow_insecure_http,
67
71
  :regression_duration_threshold, :regression_memory_threshold,
68
- :regression_query_threshold, :regression_cache_threshold
72
+ :regression_query_threshold, :regression_cache_threshold,
73
+ :replay_enabled, :replay_max_count, :replay_timeout
69
74
 
70
75
  attr_reader :authorize_block, :enabled, :track_tests, :max_profiles, :editor_url_scheme, :editor_path_map
71
76
 
@@ -84,6 +89,11 @@ module Profiler
84
89
  # versions did, about 0.1 ms each), :none never.
85
90
  @sql_backtrace = :first_and_slow
86
91
  @track_memory = true
92
+ # Threshold alerts (Profiler::Alerts): off until enabled; the webhook stays off until a URL is set.
93
+ @alerts_enabled = false
94
+ @alert_webhook_url = nil
95
+ @alert_webhook_timeout = 3 # seconds, for the connection and for the answer
96
+ @on_detection = nil
87
97
  # Not compared with anything yet. The default is the former 100 MB memory threshold read
88
98
  # as the objects it stood for.
89
99
  @allocated_objects_warning_threshold = 100 * 1024 * 1024 / AllocationCounter::LEGACY_BYTES_PER_OBJECT
@@ -166,6 +176,11 @@ module Profiler
166
176
  @cluster_allowed_slave_urls = []
167
177
  @mcp_allowed_hosts = []
168
178
  @cluster_allow_insecure_http = false
179
+ # Replaying a profiled request (Profiler::Replay): off by default, it sends real requests.
180
+ @replay_enabled = false
181
+ @replay_max_count = 10 # most replays one call may ask for
182
+ @replay_timeout = 30 # seconds, per replay
183
+ @replay_port = nil # port the application listens on; set it when the server is not Puma
169
184
  # Where the profiler's own errors go (Profiler.log): Rails.logger when nil, or $stderr when
170
185
  # there is none.
171
186
  @logger = nil
@@ -202,6 +217,13 @@ module Profiler
202
217
  # The editor the file:line links open: "vscode", "idea" or "cursor" (String or Symbol). A
203
218
  # value outside that list is kept as given, but #editor_link_scheme answers nil for it, so
204
219
  # that no link is built.
220
+ # Nil or empty turns the webhook off. Otherwise an absolute http(s) URL with a host, and no
221
+ # user or password in it: the webhook sends none, so a URL carrying them is refused rather than
222
+ # silently called without them.
223
+ def alert_webhook_url=(value)
224
+ @alert_webhook_url = Alerts::Webhook.validate!(value)
225
+ end
226
+
205
227
  def editor_url_scheme=(value)
206
228
  @editor_url_scheme = value.nil? ? nil : value.to_s
207
229
  end
@@ -230,6 +252,17 @@ module Profiler
230
252
  @tmp_path = value.nil? ? nil : Pathname.new(value)
231
253
  end
232
254
 
255
+ attr_reader :replay_port
256
+
257
+ # The port the application server really listens on (1 to 65535), or nil to rely on the one the
258
+ # profiler captured from the Puma socket. When set, a replay always uses it.
259
+ def replay_port=(value)
260
+ port = value.to_s.match?(/\A\d{1,5}\z/) ? value.to_s.to_i : nil
261
+ raise ArgumentError, "config.replay_port must be a port from 1 to 65535 or nil, got #{value.inspect}" unless value.nil? || port&.between?(1, 65_535)
262
+
263
+ @replay_port = port
264
+ end
265
+
233
266
  attr_reader :cluster_allowed_slave_urls, :mcp_allowed_hosts, :skip_paths, :http_skip_hosts
234
267
 
235
268
  # Paths that are not profiled: a Regexp matched against the path, or a String equal to it.
@@ -348,6 +381,13 @@ module Profiler
348
381
  @name || (defined?(Rails) ? Rails.application.class.module_parent_name.underscore : "profiler")
349
382
  end
350
383
 
384
+ # Block called with the alert (a Hash, see Profiler::Alerts) each time a profile crosses a
385
+ # threshold or shows an N+1. Only runs when config.alerts_enabled is true.
386
+ def on_detection(&block)
387
+ @on_detection = block if block
388
+ @on_detection
389
+ end
390
+
351
391
  def authorize_with(&block)
352
392
  @authorize_block = block
353
393
  end
@@ -97,6 +97,13 @@ module Profiler
97
97
  ActionDispatch::HostAuthorization::Permissions.new(hosts).allows?(host)
98
98
  end
99
99
 
100
+ # Whether a Host header value (with or without a port) names this machine or a host the
101
+ # application allows in config.hosts: the test Profiler::Replay applies to the host it is
102
+ # about to send a request to.
103
+ def host_allowed?(value)
104
+ allowed_host?(host_name(value))
105
+ end
106
+
100
107
  private
101
108
 
102
109
  def host_denial_reason(request)
@@ -6,10 +6,10 @@ require_relative "http_guard"
6
6
  module Profiler
7
7
  module MCP
8
8
  class Server
9
- # The tools that change the profiler's data, ENV or run code: left out when
9
+ # The tools that change the profiler's data, ENV, run code or send real requests: left out when
10
10
  # config.mcp_read_only is true or PROFILER_MCP_READ_ONLY is set (Server.read_only?).
11
11
  WRITE_TOOLS = %w[
12
- run_tests clear_profiles set_env_var delete_env_var reset_env_var reset_all_env_vars
12
+ run_tests clear_profiles set_env_var delete_env_var reset_env_var reset_all_env_vars replay_request
13
13
  ].freeze
14
14
 
15
15
  # Makes the server read-only like config.mcp_read_only, for exe/profiler-mcp above all: that
@@ -113,6 +113,7 @@ module Profiler
113
113
  require_relative "tools/query_test_profiles"
114
114
  require_relative "tools/get_test_profile_detail"
115
115
  require_relative "tools/run_tests"
116
+ require_relative "tools/replay_request"
116
117
  require_relative "tools/query_console_profiles"
117
118
  require_relative "tools/clear_profiles"
118
119
  require_relative "tools/list_env_vars"
@@ -333,6 +334,27 @@ module Profiler
333
334
  },
334
335
  handler: Tools::RunTests
335
336
  ),
337
+ define_tool(
338
+ name: "replay_request",
339
+ description: "Replay a profiled HTTP request for real against the application's own host (the host and port of the original request; no option can change it). Creates one new profile per replay, linked to the original (replay_of). Methods with side effects (POST, PUT, PATCH, DELETE) are replayed too and there is no confirmation: calling this tool is the confirmation. Disabled unless config.replay_enabled is true.",
340
+ input_schema: {
341
+ properties: {
342
+ token: { type: "string", description: "Token of the profiled HTTP request to replay" },
343
+ count: { type: "number", description: "Number of replays in a row (default 1, at most config.replay_max_count, 10 by default)" },
344
+ method: { type: "string", description: "Override the HTTP method (GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS)" },
345
+ path: { type: "string", description: "Override the path of the application, e.g. '/users/1?x=2'. Must start with a single '/'" },
346
+ query: { type: "object", description: "Replace the query string with these parameters" },
347
+ params: { type: "object", description: "Replace the parameters: sent as the query for GET/HEAD/DELETE/OPTIONS, as the body otherwise" },
348
+ body: { type: "string", description: "Replace the request body" },
349
+ headers: { type: "object", description: "Request headers to add or replace. Host and forwarding headers are ignored" },
350
+ cookies: { type: "object", description: "Replace the Cookie header with these cookies" },
351
+ timeout: { type: "number", description: "Seconds to wait for each replay (default config.replay_timeout, at most 120)" },
352
+ slave: slave_param
353
+ },
354
+ required: ["token"]
355
+ },
356
+ handler: Tools::ReplayRequest
357
+ ),
336
358
  define_tool(
337
359
  name: "query_console_profiles",
338
360
  description: "Search and filter Rails console profiling sessions (IRB/rails console executions).",