rails-profiler 0.43.0 → 0.44.3

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.
@@ -44,8 +44,18 @@ module Profiler
44
44
  head :no_content
45
45
  end
46
46
 
47
+ CLEARABLE_TYPES = %w[http job test console].freeze
48
+
49
+ # Without a type the dashboard's long-standing behaviour holds: http profiles only. The
50
+ # cluster proxy names the type it means.
47
51
  def clear
48
- Profiler.storage.clear(type: "http")
52
+ type = params[:type].presence || "http"
53
+ unless CLEARABLE_TYPES.include?(type.to_s)
54
+ return render json: { error: "type must be one of #{CLEARABLE_TYPES.join(", ")}" },
55
+ status: :unprocessable_entity
56
+ end
57
+
58
+ Profiler.storage.clear(type: type.to_s)
49
59
  head :no_content
50
60
  end
51
61
 
@@ -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
@@ -26,6 +26,8 @@ module Profiler
26
26
  end
27
27
 
28
28
  render json: result
29
+ rescue Cluster::SlaveProxy::NotFound => e
30
+ render json: { error: e.message }, status: :not_found
29
31
  # The profiler's own refusals (unknown or offline slave, refused URL, missing secret).
30
32
  rescue Profiler::Error => e
31
33
  render json: { error: e.message }, status: :bad_gateway
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 }
@@ -9,6 +9,9 @@ require_relative "../storage/token"
9
9
  module Profiler
10
10
  module Cluster
11
11
  class SlaveProxy
12
+ # The slave answered 404: it has no such resource.
13
+ class NotFound < Profiler::Error; end
14
+
12
15
  # A slow or hung slave must not block the master's request thread for long.
13
16
  OPEN_TIMEOUT = 5 # seconds
14
17
  READ_TIMEOUT = 10 # seconds
@@ -51,18 +54,40 @@ module Profiler
51
54
 
52
55
  raw = data["profile"] || data
53
56
  profile_from_api(raw)
57
+ rescue NotFound
58
+ nil
54
59
  end
55
60
 
61
+ CHILDREN_PAGE_SIZE = 100
62
+
63
+ # Every child, not the first page of them: the slave lists children newest first, in pages,
64
+ # and the local storages answer oldest first, which is the order given back.
56
65
  def find_by_parent(parent_token)
57
66
  return [] unless Storage::Token.valid?(parent_token)
58
67
 
59
- data = get_json("/_profiler/api/profiles", parent_token: parent_token, all_types: true)
60
- Array(data["profiles"]).map { |h| profile_from_api(h) }
68
+ children = []
69
+ offset = 0
70
+ loop do
71
+ data = get_json("/_profiler/api/profiles", parent_token: parent_token, all_types: true,
72
+ limit: CHILDREN_PAGE_SIZE, offset: offset)
73
+ page = Array(data["profiles"])
74
+ children.concat(page.map { |h| profile_from_api(h) })
75
+ break if data["has_more"] != true || page.empty?
76
+
77
+ offset += page.size
78
+ end
79
+ children.sort_by { |p| p.started_at || Time.at(0) }
61
80
  end
62
81
 
82
+ CLEARABLE_TYPES = %w[http job test console].freeze
83
+
84
+ # ProfilesController#clear honours one type per call (http when none is given), so
85
+ # clearing everything takes one call per type.
63
86
  def clear(type: nil)
64
- params = type ? "?type=#{URI.encode_www_form_component(type)}" : ""
65
- delete_json("/_profiler/api/profiles/clear#{params}")
87
+ (type ? [type.to_s] : CLEARABLE_TYPES).each do |t|
88
+ delete_json("/_profiler/api/profiles/clear?type=#{URI.encode_www_form_component(t)}")
89
+ end
90
+ {}
66
91
  end
67
92
 
68
93
  # Generic HTTP access for action tools
@@ -123,14 +148,31 @@ module Profiler
123
148
  http.request(req)
124
149
  end
125
150
  end
126
- return {} if resp.code == "204"
127
- if resp.code.to_s.start_with?("3")
128
- raise Profiler::Error, "Slave profiler answered #{resp.code} (redirect not followed)"
151
+ interpret(resp)
152
+ end
153
+
154
+ # What a slave's status means, as PeerProxy#interpret reads a peer's: a wrong secret or a
155
+ # refusal is never an empty answer, and neither is an error of the slave.
156
+ def interpret(resp)
157
+ code = resp.code.to_s
158
+ return {} if code == "204"
159
+ raise NotFound, "Slave profiler answered 404: #{failure_detail(resp, "not found")}" if code == "404"
160
+ raise Profiler::Error, "Slave profiler answered #{code} (redirect not followed)" if code.start_with?("3")
161
+ if %w[401 403].include?(code)
162
+ raise Profiler::Error, "Slave profiler refused the request (#{code}): check that master and slave " \
163
+ "share the same config.cluster_secret"
129
164
  end
165
+ raise Profiler::Error, "Slave profiler answered #{code}: #{failure_detail(resp, "no detail")}" unless code.start_with?("2")
130
166
 
131
167
  parse_response(resp)
132
168
  end
133
169
 
170
+ # The slave's own message when its body carries one; never the raw body, which may be anything.
171
+ def failure_detail(resp, fallback)
172
+ message = parse_response(resp)["error"] if resp.body.to_s.start_with?("{")
173
+ message.is_a?(String) && !message.empty? ? message.slice(0, 200) : fallback
174
+ end
175
+
134
176
  def parse_response(resp)
135
177
  return {} if resp.body.nil? || resp.body.empty?
136
178
 
@@ -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:")
@@ -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)
@@ -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).",
@@ -445,7 +467,7 @@ module Profiler
445
467
  ::MCP::Tool.define(name: name, description: description, input_schema: input_schema) do |server_context: nil, **args|
446
468
  result = handler.call(args.transform_keys(&:to_s))
447
469
  result.is_a?(::MCP::Tool::Response) ? result : ::MCP::Tool::Response.new(result)
448
- rescue Profiler::Storage::Unavailable::Error => e
470
+ rescue Profiler::Storage::Unavailable::Error, Profiler::Error => e
449
471
  ::MCP::Tool::Response.new([{ type: "text", text: e.message }], error: true)
450
472
  end
451
473
  end
@@ -2,8 +2,6 @@
2
2
 
3
3
  require_relative "../slave_support"
4
4
 
5
- require "uri"
6
-
7
5
  module Profiler
8
6
  module MCP
9
7
  module Tools
@@ -16,9 +14,7 @@ module Profiler
16
14
  end
17
15
 
18
16
  if (proxy = MCP::SlaveSupport.with_slave_proxy(params))
19
- path = "/_profiler/api/profiles/clear"
20
- path += "?type=#{URI.encode_www_form_component(type)}" if type
21
- proxy.delete_json(path)
17
+ proxy.clear(type: type)
22
18
  else
23
19
  Profiler.storage.clear(type: type)
24
20
  end
@@ -22,7 +22,8 @@ module Profiler
22
22
  end
23
23
 
24
24
  if (proxy = MCP::SlaveSupport.with_slave_proxy(params))
25
- proxy.patch_json("/_profiler/api/env_vars", { key: key, value: "" })
25
+ data = proxy.patch_json("/_profiler/api/env_vars", { key: key, value: "" })
26
+ return ::MCP::Tool::Response.new([{ type: "text", text: "Error: #{data["error"]}" }], error: true) if data["error"]
26
27
  return [{ type: "text", text: "Deleted #{key} on slave '#{params["slave"]}'. Override persisted across restarts until reset." }]
27
28
  end
28
29
 
@@ -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
@@ -9,7 +9,8 @@ module Profiler
9
9
  class ResetAllEnvVars
10
10
  def self.call(params)
11
11
  if (proxy = MCP::SlaveSupport.with_slave_proxy(params))
12
- proxy.delete_json("/_profiler/api/env_vars/reset_all")
12
+ data = proxy.delete_json("/_profiler/api/env_vars/reset_all")
13
+ return ::MCP::Tool::Response.new([{ type: "text", text: "Error: #{data["error"]}" }], error: true) if data["error"]
13
14
  return [{ type: "text", text: "Reset all ENV overrides on slave '#{params["slave"]}'." }]
14
15
  end
15
16
 
@@ -15,7 +15,8 @@ module Profiler
15
15
  return [{ type: "text", text: "Error: key cannot be blank." }] if key.empty?
16
16
 
17
17
  if (proxy = MCP::SlaveSupport.with_slave_proxy(params))
18
- proxy.delete_json("/_profiler/api/env_vars/reset?key=#{URI.encode_www_form_component(key)}")
18
+ data = proxy.delete_json("/_profiler/api/env_vars/reset?key=#{URI.encode_www_form_component(key)}")
19
+ return ::MCP::Tool::Response.new([{ type: "text", text: "Error: #{data["error"]}" }], error: true) if data["error"]
19
20
  return [{ type: "text", text: "Reset #{key} on slave '#{params["slave"]}'." }]
20
21
  end
21
22
 
@@ -28,7 +28,8 @@ module Profiler
28
28
  end
29
29
 
30
30
  if (proxy = MCP::SlaveSupport.with_slave_proxy(params))
31
- proxy.patch_json("/_profiler/api/env_vars", { key: key, value: value })
31
+ data = proxy.patch_json("/_profiler/api/env_vars", { key: key, value: value })
32
+ return ::MCP::Tool::Response.new([{ type: "text", text: "Error: #{data["error"]}" }], error: true) if data["error"]
32
33
  return [{ type: "text", text: "Set #{key}=#{value} on slave '#{params["slave"]}'" }]
33
34
  end
34
35
 
@@ -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("-") }