rails-profiler 0.43.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +47 -0
- data/app/assets/builds/profiler-toolbar.js +2 -2
- data/app/assets/builds/profiler.js +13 -11
- data/app/controllers/profiler/api/replays_controller.rb +31 -0
- data/config/routes.rb +1 -0
- data/lib/profiler/collectors/database_collector.rb +1 -1
- data/lib/profiler/collectors/flamegraph_collector.rb +1 -1
- data/lib/profiler/collectors/i18n_collector.rb +1 -1
- data/lib/profiler/configuration.rb +18 -1
- data/lib/profiler/local_request.rb +7 -0
- data/lib/profiler/mcp/server.rb +24 -2
- data/lib/profiler/mcp/tools/replay_request.rb +66 -0
- data/lib/profiler/middleware/profiler_middleware.rb +12 -0
- data/lib/profiler/models/profile.rb +35 -2
- data/lib/profiler/replay.rb +363 -0
- data/lib/profiler/storage/base_store.rb +20 -0
- data/lib/profiler/storage/sqlite_store.rb +6 -0
- data/lib/profiler/storage/unavailable.rb +1 -1
- data/lib/profiler/version.rb +1 -1
- metadata +5 -2
|
@@ -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 }
|
|
@@ -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] =~
|
|
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] =~
|
|
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:")
|
|
@@ -69,7 +69,8 @@ module Profiler
|
|
|
69
69
|
:collector_circuit_breaker_enabled,
|
|
70
70
|
:peers, :peer_token, :peer_allow_insecure_http,
|
|
71
71
|
:regression_duration_threshold, :regression_memory_threshold,
|
|
72
|
-
:regression_query_threshold, :regression_cache_threshold
|
|
72
|
+
:regression_query_threshold, :regression_cache_threshold,
|
|
73
|
+
:replay_enabled, :replay_max_count, :replay_timeout
|
|
73
74
|
|
|
74
75
|
attr_reader :authorize_block, :enabled, :track_tests, :max_profiles, :editor_url_scheme, :editor_path_map
|
|
75
76
|
|
|
@@ -175,6 +176,11 @@ module Profiler
|
|
|
175
176
|
@cluster_allowed_slave_urls = []
|
|
176
177
|
@mcp_allowed_hosts = []
|
|
177
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
|
|
178
184
|
# Where the profiler's own errors go (Profiler.log): Rails.logger when nil, or $stderr when
|
|
179
185
|
# there is none.
|
|
180
186
|
@logger = nil
|
|
@@ -246,6 +252,17 @@ module Profiler
|
|
|
246
252
|
@tmp_path = value.nil? ? nil : Pathname.new(value)
|
|
247
253
|
end
|
|
248
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
|
+
|
|
249
266
|
attr_reader :cluster_allowed_slave_urls, :mcp_allowed_hosts, :skip_paths, :http_skip_hosts
|
|
250
267
|
|
|
251
268
|
# Paths that are not profiled: a Regexp matched against the path, or a String equal to it.
|
|
@@ -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)
|
data/lib/profiler/mcp/server.rb
CHANGED
|
@@ -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
|
|
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).",
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mcp"
|
|
4
|
+
|
|
5
|
+
require_relative "../slave_support"
|
|
6
|
+
require_relative "../../replay"
|
|
7
|
+
|
|
8
|
+
module Profiler
|
|
9
|
+
module MCP
|
|
10
|
+
module Tools
|
|
11
|
+
# Sends a profiled request again, for real, to the application's own host (see
|
|
12
|
+
# Profiler::Replay). A real side-effecting action, like run_tests: the MCP channel has no
|
|
13
|
+
# interactive confirmation, so the client that calls the tool answers for the request it
|
|
14
|
+
# replays, POST, DELETE and the other methods with side effects included. The dashboard asks
|
|
15
|
+
# the user to confirm; the server adds no further guard for this channel.
|
|
16
|
+
class ReplayRequest
|
|
17
|
+
# The only arguments read: anything else is refused by Profiler::Replay, and the ones that
|
|
18
|
+
# name a host, port or URL are refused by name.
|
|
19
|
+
def self.call(params)
|
|
20
|
+
token = params["token"]
|
|
21
|
+
return error_response("token parameter is required") unless token
|
|
22
|
+
|
|
23
|
+
options = params.except("token", "slave")
|
|
24
|
+
if (proxy = MCP::SlaveSupport.with_slave_proxy(params))
|
|
25
|
+
return run_on_slave(proxy, params["slave"], token, options)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
result = Profiler::Replay.call(token, options)
|
|
29
|
+
[{ type: "text", text: format_result(result) }]
|
|
30
|
+
rescue Profiler::Replay::Error => e
|
|
31
|
+
error_response(e.message)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.run_on_slave(proxy, slave, token, options)
|
|
35
|
+
data = proxy.post_json("/_profiler/api/profiles/#{token}/replay", options)
|
|
36
|
+
return error_response("Error replaying on slave '#{slave}': #{data["error"]}") if data["error"]
|
|
37
|
+
|
|
38
|
+
result = Profiler::Replay::Result.new(source_token: data["source_token"], target: data["target"],
|
|
39
|
+
replays: Array(data["replays"]).map { |r| r.transform_keys(&:to_sym) },
|
|
40
|
+
notes: Array(data["notes"]))
|
|
41
|
+
[{ type: "text", text: format_result(result) }]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.format_result(result)
|
|
45
|
+
lines = ["# Replay of `#{result.source_token}`", "", "Sent to #{result.target} (the host of the original request).", ""]
|
|
46
|
+
lines << "| # | Status | New profile | Duration |"
|
|
47
|
+
lines << "|---|--------|-------------|----------|"
|
|
48
|
+
result.replays.each do |replay|
|
|
49
|
+
if replay[:error]
|
|
50
|
+
lines << "| #{replay[:index]} | **error** | #{replay[:error]} | |"
|
|
51
|
+
else
|
|
52
|
+
profile = replay[:token] ? "`#{replay[:token]}`" : "(not profiled)"
|
|
53
|
+
lines << "| #{replay[:index]} | #{replay[:status]} | #{profile} | #{replay[:duration_ms]}ms |"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
result.notes.each { |note| lines << "" << "Note: #{note}" }
|
|
57
|
+
lines.join("\n")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.error_response(message)
|
|
61
|
+
::MCP::Tool::Response.new([{ type: "text", text: "Error: #{message}" }], error: true)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "rack"
|
|
4
4
|
require_relative "../models/profile"
|
|
5
|
+
require_relative "../replay"
|
|
5
6
|
require_relative "../current_context"
|
|
6
7
|
require_relative "../trace_context"
|
|
7
8
|
require_relative "../otel/trace_exporter"
|
|
@@ -33,6 +34,7 @@ module Profiler
|
|
|
33
34
|
begin
|
|
34
35
|
profile = Models::Profile.new(build_request(env), params: false)
|
|
35
36
|
profile.gem_version = Profiler.version
|
|
37
|
+
profile.replay_of = replay_source(env)
|
|
36
38
|
Profiler::CurrentContext.token = profile.token
|
|
37
39
|
assign_trace(env, profile)
|
|
38
40
|
|
|
@@ -314,6 +316,16 @@ module Profiler
|
|
|
314
316
|
Profiler.configuration.skip_paths.none? { |pattern| pattern === path }
|
|
315
317
|
end
|
|
316
318
|
|
|
319
|
+
# The token of the profile a replay (Profiler::Replay) repeats, named by the header the
|
|
320
|
+
# replay adds. Only honoured when replays are enabled, and only a well-formed token: the
|
|
321
|
+
# header changes how the profile is linked, never what the application does.
|
|
322
|
+
def replay_source(env)
|
|
323
|
+
return nil unless Profiler.configuration.replay_enabled
|
|
324
|
+
|
|
325
|
+
token = env[Profiler::Replay::SOURCE_ENV_KEY].to_s
|
|
326
|
+
Storage::Token.valid?(token) ? token : nil
|
|
327
|
+
end
|
|
328
|
+
|
|
317
329
|
def build_request(env)
|
|
318
330
|
if defined?(ActionDispatch::Request)
|
|
319
331
|
ActionDispatch::Request.new(env)
|
|
@@ -15,7 +15,7 @@ module Profiler
|
|
|
15
15
|
attr_accessor :token, :method, :status, :duration, :allocated_objects,
|
|
16
16
|
:started_at, :finished_at, :params, :headers,
|
|
17
17
|
:response_headers, :collectors_data, :collectors_metadata,
|
|
18
|
-
:parent_token, :is_ajax, :profile_type,
|
|
18
|
+
:parent_token, :replay_of, :server_port, :url_scheme, :is_ajax, :profile_type,
|
|
19
19
|
:request_body, :request_body_encoding,
|
|
20
20
|
:response_body, :response_body_encoding,
|
|
21
21
|
:request_body_size, :request_body_truncated, :request_body_size_is_minimum,
|
|
@@ -31,6 +31,7 @@ module Profiler
|
|
|
31
31
|
@collectors_data = {}
|
|
32
32
|
@collectors_metadata = []
|
|
33
33
|
@parent_token = nil
|
|
34
|
+
@replay_of = nil
|
|
34
35
|
@is_ajax = false
|
|
35
36
|
@profile_type = "http"
|
|
36
37
|
|
|
@@ -39,6 +40,7 @@ module Profiler
|
|
|
39
40
|
@method = request.request_method
|
|
40
41
|
capture_params(request) if params
|
|
41
42
|
@headers = extract_headers(request.env)
|
|
43
|
+
@server_port, @url_scheme = listening_endpoint(request.env)
|
|
42
44
|
end
|
|
43
45
|
end
|
|
44
46
|
|
|
@@ -170,6 +172,9 @@ module Profiler
|
|
|
170
172
|
trace_id: @trace_id,
|
|
171
173
|
span_id: @span_id,
|
|
172
174
|
parent_span_id: @parent_span_id,
|
|
175
|
+
replay_of: @replay_of,
|
|
176
|
+
server_port: @server_port,
|
|
177
|
+
url_scheme: @url_scheme,
|
|
173
178
|
is_ajax: @is_ajax
|
|
174
179
|
}
|
|
175
180
|
end
|
|
@@ -217,6 +222,9 @@ module Profiler
|
|
|
217
222
|
profile.response_body_size_is_minimum = data[:response_body_size_is_minimum]
|
|
218
223
|
profile.collectors_released_after_seconds = data[:collectors_released_after_seconds]
|
|
219
224
|
profile.parent_token = data[:parent_token]
|
|
225
|
+
profile.replay_of = data[:replay_of]
|
|
226
|
+
profile.server_port = data[:server_port]
|
|
227
|
+
profile.url_scheme = data[:url_scheme]
|
|
220
228
|
profile.is_ajax = data[:is_ajax] || false
|
|
221
229
|
profile.profile_type = data[:profile_type] || "http"
|
|
222
230
|
profile.gem_version = data[:gem_version]
|
|
@@ -391,9 +399,34 @@ module Profiler
|
|
|
391
399
|
Accept Accept-Charset Accept-Encoding Accept-Language
|
|
392
400
|
Authorization Cache-Control Connection Content-Length Content-Type
|
|
393
401
|
Cookie Host If-Modified-Since If-None-Match Origin
|
|
394
|
-
Referer User-Agent Turbo-Frame
|
|
402
|
+
Referer User-Agent Turbo-Frame X-Forwarded-Proto
|
|
395
403
|
].freeze
|
|
396
404
|
|
|
405
|
+
# The port the application server really listens on and the scheme of that connection, read
|
|
406
|
+
# from the connection itself and from nothing a client wrote. SERVER_PORT is not used: Puma
|
|
407
|
+
# derives it from the Host header (Host: localhost:6379 gives SERVER_PORT 6379), and it derives
|
|
408
|
+
# rack.url_scheme from X-Forwarded-Proto. Puma exposes the accepted connection as
|
|
409
|
+
# env["puma.socket"]: its local port is the listening port, and a TLS socket means https. Any
|
|
410
|
+
# other server gives no port (nil), and a replay then needs config.replay_port.
|
|
411
|
+
def listening_endpoint(env)
|
|
412
|
+
socket = env["puma.socket"]
|
|
413
|
+
port = socket_port(socket)
|
|
414
|
+
return [nil, nil] unless port
|
|
415
|
+
|
|
416
|
+
[port, socket.class.name.to_s.match?(/SSL/) ? "https" : "http"]
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def socket_port(socket)
|
|
420
|
+
return nil unless socket
|
|
421
|
+
|
|
422
|
+
io = socket.respond_to?(:to_io) ? socket.to_io : socket
|
|
423
|
+
address = io.local_address
|
|
424
|
+
port = address.ip? ? address.ip_port : nil
|
|
425
|
+
port&.between?(1, 65_535) ? port : nil
|
|
426
|
+
rescue StandardError
|
|
427
|
+
nil
|
|
428
|
+
end
|
|
429
|
+
|
|
397
430
|
def extract_headers(env)
|
|
398
431
|
env.select { |k, _| k.start_with?("HTTP_") }
|
|
399
432
|
.transform_keys { |k| k.sub(/^HTTP_/, "").split("_").map(&:capitalize).join("-") }
|