rails-profiler 0.44.13 → 0.44.15

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: 55736f064cdfce5779a5c73aec7a57c01a1605cfb81222319f4f2ce3459c0a5b
4
- data.tar.gz: 5d741d902befdeba45dec4d76a83e098e10399173e1c66d0d2eedc34d5c0bf29
3
+ metadata.gz: 5494040c10e8c5e51c20aacf54788fdab573a5814f394ced5c7c7f7caa4693aa
4
+ data.tar.gz: b84ed786eaed7a1f59ed8ee9b953a546be714432dbecd7ad372fee2b4446d567
5
5
  SHA512:
6
- metadata.gz: 7ed2b44a79b103df46544bf7fd2620d19c634f1c37cdac2c3f624e9921f69a080693ead9014f86fd6699f71cbd98a93916c85b31f2b615a648ce1a51ab50e807
7
- data.tar.gz: 44ffe929e66a17e87354feaa70bf3bbcfddb927a4a8ff1e858b7cef4ab420ac89df3f2aa8baa67632075900ed657b56e9d9bb438a3b5613492f9a0d929b0dc46
6
+ metadata.gz: 4a40f0b6d4d6c79de401838152198aae9528b795061f96c991e7a4bc1c65d8881af8f489d71f8e02078ee110a0de49b3d6715b82f818e00643fbbb67c9e84975
7
+ data.tar.gz: 8b49e505d5a86c01debb8fed088e3e428ff1e28b1492009f8a86c655c70d258b0f323d394fee3a3d2fac49ab2369ba4a14e1a106f7e2a651ebdf22cb14c37222
data/CHANGELOG.md CHANGED
@@ -19,6 +19,24 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.44.15] - 2026-10-23
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Fixed
27
+
28
+ - **Replay:** Include `Content-Type` header from captured request when replaying. Previously, only `HTTP_*` Rack env vars were captured, missing `CONTENT_TYPE` which is stored separately. Replayed JSON POST requests were incorrectly treated as form data. Refs #64
29
+ - **Replay:** Reject stored request bodies containing redaction masks (`[FILTERED]` or `[FILTERED: ...]` markers) and note them in the result instead of sending them as real data, including when the captured params would otherwise have been re-encoded into a replacement body. Refs #64
30
+ - **Replay:** Validate that the `headers` override parameter is a Hash or nil; previously passing a string or other type caused `NoMethodError` resulting in a 500 error. Now raises `Replay::Invalid` with a clear message. Refs #64
31
+
32
+ ## [0.44.14] - 2026-10-23
33
+
34
+ <!-- stamped -->
35
+
36
+ ### Fixed
37
+
38
+ - **SqliteStore:** discard the memoized storage instance after a fork so a fresh connection is built lazily on the next access, instead of reusing the parent's connection, which sqlite3 closes in the child (`preload_app!`, prefork scenarios).
39
+
22
40
  ## [0.44.13] - 2026-10-23
23
41
 
24
42
  <!-- stamped -->
@@ -425,6 +425,13 @@ module Profiler
425
425
  @storage_backend || BACKEND_LOCK.synchronize { @storage_backend ||= build_storage_backend }
426
426
  end
427
427
 
428
+ # Called by Profiler.clear_storage_after_fork: @storage_backend is memoized here too, so
429
+ # clearing only Profiler's own @storage would hand the child back the very same (discarded)
430
+ # instance on its next Profiler.storage call.
431
+ def reset_storage_backend!
432
+ BACKEND_LOCK.synchronize { @storage_backend = nil }
433
+ end
434
+
428
435
  private
429
436
 
430
437
  def default_tmp_path
@@ -428,10 +428,11 @@ module Profiler
428
428
  end
429
429
 
430
430
  def extract_headers(env)
431
- env.select { |k, _| k.start_with?("HTTP_") }
432
- .transform_keys { |k| k.sub(/^HTTP_/, "").split("_").map(&:capitalize).join("-") }
433
- .select { |k, _| ALLOWED_HEADERS.include?(k) }
434
- .then { |headers| Redaction.filter_headers(headers) }
431
+ headers = env.select { |k, _| k.start_with?("HTTP_") }
432
+ .transform_keys { |k| k.sub(/^HTTP_/, "").split("_").map(&:capitalize).join("-") }
433
+ headers["Content-Type"] = env["CONTENT_TYPE"] if env["CONTENT_TYPE"]
434
+ headers.select { |k, _| ALLOWED_HEADERS.include?(k) }
435
+ .then { |h| Redaction.filter_headers(h) }
435
436
  end
436
437
  end
437
438
  end
@@ -242,8 +242,11 @@ module Profiler
242
242
  end
243
243
  notes << "Not sent, masked in the profile: #{redacted.join(", ")}." unless redacted.empty?
244
244
 
245
+ override_headers = options[:headers]
246
+ raise Invalid, "headers must be a Hash or nil" unless override_headers.nil? || override_headers.is_a?(Hash)
247
+
245
248
  ignored = []
246
- (options[:headers] || {}).each do |name, value|
249
+ (override_headers || {}).each do |name, value|
247
250
  if FORBIDDEN_HEADERS.include?(name.to_s.downcase) || name.to_s.match?(/[\r\n:\s]/) || value.to_s.match?(/[\r\n]/)
248
251
  ignored << name.to_s
249
252
  next
@@ -268,22 +271,32 @@ module Profiler
268
271
  end
269
272
 
270
273
  def body_and_query(source, options, method, path_query, headers, notes)
274
+ raise Invalid, "params must be a Hash or nil" if options.key?(:params) && !options[:params].nil? && !options[:params].is_a?(Hash)
275
+ raise Invalid, "body must be a String or nil" if options.key?(:body) && !options[:body].nil? && !options[:body].is_a?(String)
276
+
271
277
  params = options.key?(:params) ? options[:params] : default_params(source, notes)
272
278
  query = options.key?(:query) ? encode_query(options[:query]) : path_query
273
279
  body = nil
274
280
 
281
+ masked_body_dropped = false
275
282
  if options.key?(:body)
276
283
  body = options[:body].to_s
277
- elsif !options.key?(:params) && !BODYLESS_METHODS.include?(method) && (stored = stored_body(source, notes))
278
- body = stored
284
+ elsif !options.key?(:params) && !BODYLESS_METHODS.include?(method)
285
+ body, masked_body_dropped = stored_body(source, notes)
279
286
  end
280
287
 
281
- if params.is_a?(Hash) && !params.empty? && !options.key?(:query) && path_query.nil? && body.nil?
288
+ # A body dropped for masking must not be silently replaced by one rebuilt from params:
289
+ # the note says it "was not sent", not "was sent after all, reconstructed differently".
290
+ if !masked_body_dropped && params.is_a?(Hash) && !params.empty? && !options.key?(:query) && path_query.nil? && body.nil?
282
291
  if BODYLESS_METHODS.include?(method)
283
292
  query = encode_query(params)
284
293
  else
285
294
  body, content_type = encode_params_body(params, headers)
286
- headers["Content-Type"] ||= content_type
295
+ user_content_type = (options[:headers] || {}).any? { |name, _| name.to_s.casecmp?("Content-Type") }
296
+ unless user_content_type
297
+ headers.delete_if { |existing, _| existing.casecmp?("Content-Type") }
298
+ headers["Content-Type"] = content_type
299
+ end
287
300
  end
288
301
  end
289
302
 
@@ -300,19 +313,30 @@ module Profiler
300
313
  params.reject { |_, value| value == Redaction::MASK }
301
314
  end
302
315
 
316
+ # Returns [body, masked]: masked is true only when a body existed and was dropped because
317
+ # it contains a redaction marker, so the caller can tell that case apart from "no body was
318
+ # ever captured", for which the default_params fallback remains appropriate.
303
319
  def stored_body(source, notes)
304
- return nil if source.request_body.nil? || source.request_body.empty?
320
+ return [nil, false] if source.request_body.nil? || source.request_body.empty?
305
321
 
306
322
  if source.request_body_truncated
307
323
  notes << "The stored request body is truncated (max_captured_body_bytes): the replay sends what was kept."
308
324
  end
309
- if source.request_body_encoding == "base64"
310
- Base64.strict_decode64(source.request_body)
311
- else
312
- Models::Profile.decode_body(source.request_body, source.request_body_encoding).first
325
+
326
+ decoded = if source.request_body_encoding == "base64"
327
+ Base64.strict_decode64(source.request_body)
328
+ else
329
+ Models::Profile.decode_body(source.request_body, source.request_body_encoding).first
330
+ end
331
+
332
+ if decoded.include?(Redaction::MASK) || decoded.include?("[FILTERED:")
333
+ notes << "The stored request body contains masked values and was not sent."
334
+ return [nil, true]
313
335
  end
336
+
337
+ [decoded, false]
314
338
  rescue ArgumentError
315
- nil
339
+ [nil, false]
316
340
  end
317
341
 
318
342
  def encode_query(value)
@@ -38,6 +38,32 @@ module Profiler
38
38
  migrate!
39
39
  end
40
40
 
41
+ # Called by Profiler.clear_storage_after_fork, on the parent's store, before the child
42
+ # drops its reference. sqlite3 >= 2.0 would otherwise print a warning (or, with WAL and
43
+ # sqlite3 < 2.0, let its finalizer close a handle the parent still owns) when this object
44
+ # is garbage-collected in the child. #discard only exists on sqlite3 >= 2.0. On an older
45
+ # gem the handle is kept alive instead, deferring the eventual close to process exit
46
+ # rather than an arbitrary GC pass while the worker is still using the database: Ruby still
47
+ # runs the finalizer on a live object at exit, so this delays the close, it does not avoid
48
+ # it. sqlite3 >= 2.0 also installs its own Process._fork hook (SQLite3::ForkSafety) that
49
+ # discards inherited connections on its own; depending on prepend order (whether sqlite3 or
50
+ # the profiler is required first) its warning may still appear once even with this method
51
+ # in place.
52
+ def discard_after_fork
53
+ if @db.respond_to?(:discard)
54
+ # Already discarded by sqlite3's own Process._fork hook (SQLite3::ForkSafety) when the
55
+ # sqlite3 gem was required before the profiler: discarding twice is at best a spurious
56
+ # error here, at worst a NULL sqlite3* reaching the C layer.
57
+ @db.discard unless @db.closed?
58
+ else
59
+ (self.class.kept_alive_after_fork ||= []) << @db
60
+ end
61
+ end
62
+
63
+ class << self
64
+ attr_accessor :kept_alive_after_fork
65
+ end
66
+
41
67
  def do_save(token, profile)
42
68
  data = profile.to_h
43
69
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.44.13"
4
+ VERSION = "0.44.15"
5
5
 
6
6
  GEM_ROOT = File.expand_path("../..", __dir__)
7
7
 
data/lib/profiler.rb CHANGED
@@ -14,6 +14,16 @@ module Profiler
14
14
  # was parsing.
15
15
  LOG_ERROR_MESSAGE_LIMIT = 1000
16
16
 
17
+ # Detects process fork (Ruby 3.1+) and clears the storage instance in the child,
18
+ # so it is recreated with a fresh database connection.
19
+ module ForkHook
20
+ def _fork
21
+ pid = super
22
+ Profiler.clear_storage_after_fork if pid.zero?
23
+ pid
24
+ end
25
+ end
26
+
17
27
  class << self
18
28
  attr_writer :configuration
19
29
  attr_accessor :function_profiling_enabled
@@ -40,11 +50,32 @@ module Profiler
40
50
  # A store that cannot be created is said once (Storage::Unavailable) and tried again on the
41
51
  # next call.
42
52
  def storage
53
+ install_fork_hook!
43
54
  @storage || STORAGE_LOCK.synchronize { @storage ||= configuration.storage_backend }
44
55
  rescue StandardError => e
45
56
  Storage::Unavailable.for(e)
46
57
  end
47
58
 
59
+ private
60
+
61
+ def install_fork_hook!
62
+ return if @fork_hook_installed || !Process.respond_to?(:_fork)
63
+
64
+ Process.singleton_class.prepend(ForkHook)
65
+ @fork_hook_installed = true
66
+ end
67
+
68
+ public
69
+
70
+ def clear_storage_after_fork
71
+ old_storage = @storage
72
+ @storage = nil
73
+ configuration.reset_storage_backend!
74
+ old_storage.discard_after_fork if old_storage.respond_to?(:discard_after_fork)
75
+ rescue => e
76
+ log_warn("could not clear storage after fork", e)
77
+ end
78
+
48
79
  # Saves a profile from a path of the application (a job, a console command, a test, an
49
80
  # outbound HTTP call): a storage error loses the profile, never the application's work.
50
81
  # True when saved; false when the error was logged instead, or when the store is unavailable
@@ -251,6 +282,9 @@ module Profiler
251
282
  self.function_profiling_mode = "lite"
252
283
  self.function_profiling_clock = "wall"
253
284
  self.function_profiling_tracepoint_fallback = false
285
+
286
+ # Install fork hook as early as possible, so it's in place before any fork happens
287
+ install_fork_hook! if Process.respond_to?(:_fork)
254
288
  end
255
289
 
256
290
  # Require core components
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.44.13
4
+ version: 0.44.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy