rails-profiler 0.22.0 → 0.22.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: b7bf5cab3285946189ba9adee630f79ad7303ae591aa4364e0fe4d7af320bab9
4
- data.tar.gz: 4c2ad13691c8c6556355c2c4b2c5934ef2d6aab9d956e2f30d16d92098cd5997
3
+ metadata.gz: 87931b523c1fcf03710fe558f619591bc2a953aa50ad76ccf414357964515a5d
4
+ data.tar.gz: c07a2ae3e5104529f72d4ce04caf67aa04647946c4ba5e98fc68ca1c7b10c320
5
5
  SHA512:
6
- metadata.gz: 1c846fa98d00dd5e6c5cc5ef8ca443b6f2b02664b9fdee77479bdbf6013d38cdf359e892ba6f08721b54fa5fb13d9b8663b909d7f17f97a849402654c4bf3821
7
- data.tar.gz: 1697e1bfe8842df4d41aed3a1c5e84b8090d510fab6ee9622afc44e326553c13843a5252b74b34d5d6bb4195629f3d05330953b5527ef7a596aae89bf9a36b11
6
+ metadata.gz: ffb96a532e285ff59fe55059a2a0615c397ff82838479da8804709e14e7ec1802a0b759ffdd32ed64bb9166ea185efd8da06956cab3078c43bf4e6120b8d7c56
7
+ data.tar.gz: 22de884813f0d456a53c2388bb3849ff343bbd4349951419ab2f04f3c1977ce0e1681ba5c8c5349fff3cb894d6f2a8b34de9cf13f92acce08921ec3298fe816c
@@ -10,6 +10,7 @@ module Profiler
10
10
  super
11
11
  @events = []
12
12
  @subscriptions = []
13
+ @mutex = Mutex.new
13
14
  end
14
15
 
15
16
  def icon
@@ -38,7 +39,7 @@ module Profiler
38
39
 
39
40
  # Controller action
40
41
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("process_action.action_controller") do |_name, started, finished, _unique_id, payload|
41
- @events << Models::TimelineEvent.new(
42
+ add_event Models::TimelineEvent.new(
42
43
  name: "#{payload[:controller]}##{payload[:action]}",
43
44
  started_at: started,
44
45
  finished_at: finished,
@@ -57,7 +58,7 @@ module Profiler
57
58
  # Template rendering
58
59
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("render_template.action_view") do |_name, started, finished, _unique_id, payload|
59
60
  identifier = short_identifier(payload[:identifier])
60
- @events << Models::TimelineEvent.new(
61
+ add_event Models::TimelineEvent.new(
61
62
  name: "Render: #{identifier}",
62
63
  started_at: started,
63
64
  finished_at: finished,
@@ -69,7 +70,7 @@ module Profiler
69
70
  # Partial rendering
70
71
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("render_partial.action_view") do |_name, started, finished, _unique_id, payload|
71
72
  identifier = short_identifier(payload[:identifier])
72
- @events << Models::TimelineEvent.new(
73
+ add_event Models::TimelineEvent.new(
73
74
  name: "Partial: #{identifier}",
74
75
  started_at: started,
75
76
  finished_at: finished,
@@ -84,7 +85,7 @@ module Profiler
84
85
  next if payload[:sql] =~ /^(BEGIN|COMMIT|ROLLBACK|SAVEPOINT)/i
85
86
 
86
87
  sql = payload[:sql].to_s
87
- @events << Models::TimelineEvent.new(
88
+ add_event Models::TimelineEvent.new(
88
89
  name: sql.length > 80 ? "#{sql[0, 80]}..." : sql,
89
90
  started_at: started,
90
91
  finished_at: finished,
@@ -98,7 +99,7 @@ module Profiler
98
99
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe(event_name) do |name, started, finished, _unique_id, payload|
99
100
  op = name.split(".").first.sub("cache_", "")
100
101
  key = payload[:key].to_s
101
- @events << Models::TimelineEvent.new(
102
+ add_event Models::TimelineEvent.new(
102
103
  name: "cache_#{op}: #{key.length > 60 ? "#{key[0, 60]}..." : key}",
103
104
  started_at: started,
104
105
  finished_at: finished,
@@ -111,7 +112,7 @@ module Profiler
111
112
 
112
113
  # Called by Profiler.measure to record custom instrumentation events
113
114
  def record_custom_event(label:, started_at:, finished_at:, metadata: {})
114
- @events << Models::TimelineEvent.new(
115
+ add_event Models::TimelineEvent.new(
115
116
  name: label,
116
117
  started_at: started_at,
117
118
  finished_at: finished_at,
@@ -122,7 +123,7 @@ module Profiler
122
123
 
123
124
  # Called by NetHttpInstrumentation to record outbound HTTP events
124
125
  def record_http_event(started_at:, finished_at:, url:, method:, status:)
125
- @events << Models::TimelineEvent.new(
126
+ add_event Models::TimelineEvent.new(
126
127
  name: "HTTP #{method} #{url}",
127
128
  started_at: started_at,
128
129
  finished_at: finished_at,
@@ -182,6 +183,10 @@ module Profiler
182
183
  roots
183
184
  end
184
185
 
186
+ def add_event(event)
187
+ @mutex.synchronize { @events << event }
188
+ end
189
+
185
190
  def short_identifier(identifier)
186
191
  return identifier.to_s unless identifier.to_s.include?("/")
187
192
 
@@ -9,6 +9,8 @@ module Profiler
9
9
  def initialize(profile)
10
10
  super
11
11
  @requests = []
12
+ @mutex = Mutex.new
13
+ @collected = false
12
14
  end
13
15
 
14
16
  def icon
@@ -40,47 +42,88 @@ module Profiler
40
42
  def collect
41
43
  Thread.current[:profiler_http_collector] = nil
42
44
 
43
- threshold = Profiler.configuration.slow_http_threshold
45
+ data = @mutex.synchronize do
46
+ @collected = true
47
+ build_data(@requests)
48
+ end
49
+ store_data(data)
50
+ end
44
51
 
45
- store_data(
46
- total_requests: @requests.size,
47
- total_duration: @requests.sum { |r| r[:duration] }.round(2),
48
- slow_requests: @requests.count { |r| r[:duration] >= threshold },
49
- error_requests: @requests.count { |r| r[:status] >= 400 || r[:status] == 0 },
50
- by_host: group_by_host,
51
- by_status: group_by_status,
52
- requests: @requests.map { |r| r.transform_keys(&:to_s) }
53
- )
52
+ # Called from NetHttpInstrumentation before the actual HTTP call.
53
+ # Returns the mutable entry so the caller can update it on completion.
54
+ def register_pending(payload)
55
+ entry = payload.merge(in_flight: true, status: 0, duration: nil,
56
+ response_headers: {}, response_body: nil,
57
+ response_body_encoding: "text", response_size: 0)
58
+ @mutex.synchronize { @requests << entry }
59
+ save_if_collected
60
+ entry
54
61
  end
55
62
 
56
- def record_request(payload)
57
- @requests << payload
63
+ # Called from NetHttpInstrumentation after the HTTP response is received.
64
+ def complete_request(entry, **data)
65
+ @mutex.synchronize { entry.merge!(data.merge(in_flight: false)) }
66
+ save_if_collected
67
+ end
68
+
69
+ # Called from NetHttpInstrumentation when the HTTP call raises.
70
+ def fail_request(entry, error:, duration:)
71
+ @mutex.synchronize { entry.merge!(in_flight: false, status: 0, duration: duration, error: error) }
72
+ save_if_collected
58
73
  end
59
74
 
60
75
  def toolbar_summary
61
- total = @requests.size
76
+ requests = @mutex.synchronize { @requests.dup }
77
+ total = requests.size
62
78
  return { text: "0 HTTP", color: "green" } if total == 0
63
79
 
64
80
  threshold = Profiler.configuration.slow_http_threshold
65
- errors = @requests.count { |r| r[:status] >= 400 || r[:status] == 0 }
66
- slow = @requests.count { |r| r[:duration] >= threshold }
67
- duration = @requests.sum { |r| r[:duration] }.round(2)
81
+ in_flight = requests.count { |r| r[:in_flight] }
82
+ errors = requests.count { |r| !r[:in_flight] && (r[:status] >= 400 || r[:status] == 0) }
83
+ slow = requests.count { |r| !r[:in_flight] && r[:duration] && r[:duration] >= threshold }
84
+ duration = requests.sum { |r| r[:duration].to_f }.round(2)
68
85
 
69
86
  color = if errors > 0 || slow > 0
70
87
  "red"
71
- elsif total > 10
88
+ elsif in_flight > 0 || total > 10
72
89
  "orange"
73
90
  else
74
91
  "green"
75
92
  end
76
93
 
77
- { text: "#{total} HTTP (#{duration}ms)", color: color }
94
+ text = in_flight > 0 ? "#{total} HTTP (#{in_flight} pending, #{duration}ms)" : "#{total} HTTP (#{duration}ms)"
95
+ { text: text, color: color }
78
96
  end
79
97
 
80
98
  private
81
99
 
82
- def group_by_host
83
- @requests.each_with_object(Hash.new(0)) do |req, h|
100
+ def build_data(requests)
101
+ threshold = Profiler.configuration.slow_http_threshold
102
+ {
103
+ total_requests: requests.size,
104
+ total_duration: requests.sum { |r| r[:duration].to_f }.round(2),
105
+ slow_requests: requests.count { |r| !r[:in_flight] && r[:duration] && r[:duration] >= threshold },
106
+ error_requests: requests.count { |r| !r[:in_flight] && (r[:status] >= 400 || r[:status] == 0) },
107
+ by_host: group_by_host(requests),
108
+ by_status: group_by_status(requests),
109
+ requests: requests.map { |r| r.transform_keys(&:to_s) }
110
+ }
111
+ end
112
+
113
+ # Rebuilds and persists HTTP data after collect has already run.
114
+ # Called when fire-and-forget threads register or complete requests post-collect.
115
+ def save_if_collected
116
+ data = @mutex.synchronize do
117
+ return unless @collected
118
+
119
+ build_data(@requests)
120
+ end
121
+ store_data(data)
122
+ Profiler.storage.save(@profile.token, @profile)
123
+ end
124
+
125
+ def group_by_host(requests)
126
+ requests.each_with_object(Hash.new(0)) do |req, h|
84
127
  host = begin
85
128
  URI.parse(req[:url]).host || "unknown"
86
129
  rescue URI::InvalidURIError
@@ -90,16 +133,17 @@ module Profiler
90
133
  end
91
134
  end
92
135
 
93
- def group_by_status
94
- @requests.each_with_object(Hash.new(0)) do |req, h|
95
- status = req[:status]
96
- key = if status == 0
136
+ def group_by_status(requests)
137
+ requests.each_with_object(Hash.new(0)) do |req, h|
138
+ key = if req[:in_flight]
139
+ "pending"
140
+ elsif req[:status] == 0
97
141
  "error"
98
- elsif status < 300
142
+ elsif req[:status] < 300
99
143
  "2xx"
100
- elsif status < 400
144
+ elsif req[:status] < 400
101
145
  "3xx"
102
- elsif status < 500
146
+ elsif req[:status] < 500
103
147
  "4xx"
104
148
  else
105
149
  "5xx"
@@ -44,9 +44,28 @@ module Profiler
44
44
  end
45
45
  end
46
46
  req_headers = req.to_hash.transform_values { |v| v.join(", ") }
47
+ req_content_type = req["content-type"].to_s
48
+ processed_req = req_body.empty? ? { body: nil, encoding: "text" } : NetHttpInstrumentation.process_body(req_body, req_content_type)
49
+
47
50
  request_id = SecureRandom.hex(8)
48
51
  started_at = Time.now.iso8601(3)
49
52
  t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
53
+
54
+ # Register the request as pending before the network call so that
55
+ # fire-and-forget threads appear in the UI immediately, even if
56
+ # collect() runs before this thread completes.
57
+ entry = collector.register_pending(
58
+ id: request_id,
59
+ started_at: started_at,
60
+ url: url,
61
+ method: req.method,
62
+ request_headers: req_headers,
63
+ request_body: processed_req[:body],
64
+ request_body_encoding: processed_req[:encoding],
65
+ request_size: req_body.bytesize,
66
+ backtrace: NetHttpInstrumentation.extract_backtrace
67
+ )
68
+
50
69
  Thread.current[:profiler_http_recording] = true
51
70
 
52
71
  response = super
@@ -56,29 +75,18 @@ module Profiler
56
75
  resp_content_encoding = response["content-encoding"].to_s.strip.downcase
57
76
  resp_body = NetHttpInstrumentation.decompress_body(resp_body_raw, resp_content_encoding)
58
77
  resp_content_type = response["content-type"].to_s
59
- req_content_type = req["content-type"].to_s
60
-
61
- processed_req = req_body.empty? ? { body: nil, encoding: "text" } : NetHttpInstrumentation.process_body(req_body, req_content_type)
62
78
  processed_resp = NetHttpInstrumentation.process_body(resp_body, resp_content_type)
63
79
 
64
80
  t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
65
81
 
66
- collector.record_request(
67
- id: request_id,
68
- started_at: started_at,
69
- url: url,
70
- method: req.method,
82
+ collector.complete_request(
83
+ entry,
71
84
  status: response.code.to_i,
72
85
  duration: duration,
73
- request_headers: req_headers,
74
- request_body: processed_req[:body],
75
- request_body_encoding: processed_req[:encoding],
76
- request_size: req_body.bytesize,
77
86
  response_headers: response.to_hash.transform_values { |v| v.join(", ") },
78
87
  response_body: processed_resp[:body],
79
88
  response_body_encoding: processed_resp[:encoding],
80
- response_size: resp_body_raw.bytesize,
81
- backtrace: NetHttpInstrumentation.extract_backtrace
89
+ response_size: resp_body_raw.bytesize
82
90
  )
83
91
 
84
92
  fg = Thread.current[:profiler_flamegraph_collector]
@@ -86,26 +94,9 @@ module Profiler
86
94
 
87
95
  response
88
96
  rescue => e
89
- if defined?(t0) && t0
90
- duration = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0) * 1000).round(2)
91
- collector&.record_request(
92
- id: defined?(request_id) ? request_id : SecureRandom.hex(8),
93
- started_at: defined?(started_at) ? started_at : Time.now.iso8601(3),
94
- url: url,
95
- method: req.method,
96
- status: 0,
97
- duration: duration,
98
- request_headers: defined?(req_headers) ? req_headers : {},
99
- request_body: nil,
100
- request_body_encoding: "text",
101
- request_size: 0,
102
- response_headers: {},
103
- response_body: nil,
104
- response_body_encoding: "text",
105
- response_size: 0,
106
- backtrace: NetHttpInstrumentation.extract_backtrace,
107
- error: e.message
108
- )
97
+ duration = defined?(t0) && t0 ? ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0) * 1000).round(2) : 0.0
98
+ if defined?(entry) && entry
99
+ collector.fail_request(entry, error: e.message, duration: duration)
109
100
  end
110
101
  raise
111
102
  ensure
@@ -166,9 +157,8 @@ module Profiler
166
157
  end
167
158
 
168
159
  def self.extract_backtrace
169
- caller_locations(5, 15)
160
+ caller_locations(5, 40)
170
161
  .reject { |l| l.path.to_s.include?("net/http") || l.path.to_s.include?("profiler/instrumentation") }
171
- .first(5)
172
162
  .map { |l| "#{l.path}:#{l.lineno}:in `#{l.label}`" }
173
163
  end
174
164
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Instrumentation
5
+ module ThreadContextPropagation
6
+ PROPAGATED_KEYS = %i[
7
+ profiler_http_collector
8
+ profiler_flamegraph_collector
9
+ ].freeze
10
+
11
+ def initialize(*args, &block)
12
+ parent_context = PROPAGATED_KEYS.filter_map do |key|
13
+ val = Thread.current[key]
14
+ [key, val] unless val.nil?
15
+ end.to_h
16
+
17
+ if parent_context.empty?
18
+ super
19
+ else
20
+ super(*args) do
21
+ parent_context.each { |k, v| Thread.current[k] = v }
22
+ begin
23
+ block&.call
24
+ ensure
25
+ PROPAGATED_KEYS.each { |k| Thread.current[k] = nil }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ Thread.prepend(Profiler::Instrumentation::ThreadContextPropagation)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.22.0"
4
+ VERSION = "0.22.1"
5
5
  end
data/lib/profiler.rb CHANGED
@@ -106,5 +106,6 @@ require_relative "profiler/collectors/env_collector"
106
106
  require_relative "profiler/collectors/mailer_collector"
107
107
 
108
108
  require_relative "profiler/env_override_store"
109
+ require_relative "profiler/instrumentation/thread_context_propagation"
109
110
  require_relative "profiler/railtie" if defined?(Rails::Railtie)
110
111
  require_relative "profiler/engine" if defined?(Rails::Engine)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.0
4
+ version: 0.22.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-05-04 00:00:00.000000000 Z
11
+ date: 2026-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -149,6 +149,7 @@ files:
149
149
  - lib/profiler/instrumentation/active_job_instrumentation.rb
150
150
  - lib/profiler/instrumentation/net_http_instrumentation.rb
151
151
  - lib/profiler/instrumentation/sidekiq_middleware.rb
152
+ - lib/profiler/instrumentation/thread_context_propagation.rb
152
153
  - lib/profiler/job_profiler.rb
153
154
  - lib/profiler/mcp/body_formatter.rb
154
155
  - lib/profiler/mcp/file_cache.rb