rails-profiler 0.45.2 → 0.45.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e77f6e8c5a2b0148fc9288da73e9c9f464e57e3fadad34df3c5e96b7d8af2de
4
- data.tar.gz: 6877c02a32028e0c501bf0ac4e0249b91d97c7865b94ccbba9fe497ebfa3eebd
3
+ metadata.gz: 84a3ca793017ac61edb9131e4471d02fdacdc9324f569a7d53a81d63f0ceb07c
4
+ data.tar.gz: 29856f7b8101ed1a112fc2f80e722fbcd4996633bca3fca4f82d03fccb020901
5
5
  SHA512:
6
- metadata.gz: b2e27e15a4774ffbf717c7f76e8f6ff8f46001e452e59adfcfdfb9219ec3fd9611e19790ba061c846c964f71a1c1787e3486b3fcdc3e60fe89bbe68244289c9d
7
- data.tar.gz: a9c1be674583b25ba7a2a6d901b36e76ac188c19be8f219c2a607fe280bb37b77022c3ca91a2b69f5c58508582267613f9e72a8eb004dd6eb645f5dcddc9265b
6
+ metadata.gz: fd9398c466b98aec376ba14770d67f21bff834799d1ec7d55c9fe9c02cdb6033b6b39b3d242172c709a25ea5c1b230d76e24b4ab2b26706f8ccbd433c8ba5be0
7
+ data.tar.gz: 974d4dc48e021535125bf3c1f8ae67baf35c15b08d914f4ae92535dea2de33084598d2b7d200a65d21e891fad13274c08a336dac85f4a31455eb01502aed6fc5
data/CHANGELOG.md CHANGED
@@ -19,6 +19,14 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.45.3] - 2026-10-27
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Fixed
27
+
28
+ - **Outbound HTTP:** The body of a request sent through a `body_stream` that cannot be rewound (RestClient's payload, a pipe) is now captured, redacted and cut at `max_captured_body_bytes` like any other, instead of showing "Request body not captured". The profiler copies the bytes Net::HTTP reads and leaves the stream and what the server receives untouched.
29
+
22
30
  ## [0.45.2] - 2026-10-27
23
31
 
24
32
  <!-- stamped -->
@@ -91,6 +91,12 @@ module Profiler
91
91
  save_if_collected
92
92
  end
93
93
 
94
+ # Called from NetHttpInstrumentation to add what is only known once the request is sent.
95
+ def update_request(entry, **data)
96
+ @mutex.synchronize { entry.merge!(data) }
97
+ save_if_collected
98
+ end
99
+
94
100
  # Called from NetHttpInstrumentation when the HTTP call raises.
95
101
  def fail_request(entry, error:, duration:)
96
102
  @mutex.synchronize { entry.merge!(in_flight: false, status: 0, duration: duration, error: error) }
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Instrumentation
5
+ # Only the methods the original has are defined: IO.copy_stream picks readpartial over
6
+ # read when it finds it, and Net::HTTP rewinds a stream only if it can.
7
+ module Partial
8
+ def readpartial(*args)
9
+ result = @original.readpartial(*args)
10
+ record(result, false)
11
+ result
12
+ rescue EOFError
13
+ @finished = true
14
+ raise
15
+ end
16
+
17
+ def read_nonblock(*args, **opts)
18
+ result = @original.read_nonblock(*args, **opts)
19
+ record(result, false) if result.is_a?(String)
20
+ result
21
+ rescue EOFError
22
+ @finished = true
23
+ raise
24
+ end
25
+ end
26
+
27
+ module Rewindable
28
+ # Net::HTTP rewinds a stream before it retries an idempotent request: what was copied
29
+ # belongs to the attempt that failed.
30
+ def rewind(*args)
31
+ result = @original.rewind(*args)
32
+ @buffer.clear
33
+ @total_read = 0
34
+ @finished = false
35
+ result
36
+ end
37
+ end
38
+
39
+ # Stands in for a request body_stream that cannot be rewound (a pipe, a socket, a payload
40
+ # object such as RestClient's) while Net::HTTP sends it. It hands every read to the original
41
+ # stream untouched and copies what came out, up to one byte past +limit+, into a buffer; nothing is
42
+ # read ahead, so the server gets exactly the bytes it would have got.
43
+ #
44
+ # to_io and to_path are deliberately not forwarded: IO.copy_stream would use them to go around
45
+ # this object and read the original directly.
46
+ class CapturingStream
47
+ attr_reader :original, :total_read
48
+
49
+ def initialize(original, limit)
50
+ @original = original
51
+ @limit = limit
52
+ @buffer = String.new(encoding: Encoding::BINARY)
53
+ @total_read = 0
54
+ @finished = false
55
+ extend Partial if original.respond_to?(:readpartial)
56
+ extend Rewindable if original.respond_to?(:rewind)
57
+ end
58
+
59
+ # The bytes seen so far, at most one past +limit+ (so that the caller can tell the body was
60
+ # cut, and cut it with Redaction.cut_bytes, which holds back what could start a credential).
61
+ def captured
62
+ @buffer.dup
63
+ end
64
+
65
+ # True once a read has reported the end of the stream.
66
+ def finished?
67
+ @finished
68
+ end
69
+
70
+ def read(*args)
71
+ result = @original.read(*args)
72
+ record(result, args.first.nil?)
73
+ result
74
+ end
75
+
76
+ def respond_to_missing?(name, include_private = false)
77
+ return false if %i[to_io to_path].include?(name)
78
+
79
+ @original.respond_to?(name, include_private) || super
80
+ end
81
+
82
+ def method_missing(name, *args, **opts, &block)
83
+ if %i[to_io to_path].include?(name) || !@original.respond_to?(name)
84
+ return super
85
+ end
86
+
87
+ opts.empty? ? @original.public_send(name, *args, &block) : @original.public_send(name, *args, **opts, &block)
88
+ end
89
+
90
+ private
91
+
92
+ # +whole+: a read without a length, which returns the rest and so ends the stream.
93
+ def record(chunk, whole)
94
+ @finished = true if chunk.nil? || whole
95
+ return if chunk.nil? || chunk.empty?
96
+
97
+ @total_read += chunk.bytesize
98
+ room = @limit ? @limit + 1 - @buffer.bytesize : chunk.bytesize
99
+ @buffer << chunk.byteslice(0, room).b if room.positive?
100
+ end
101
+ end
102
+ end
103
+ end
@@ -10,6 +10,7 @@ require_relative "../body_content"
10
10
  require_relative "../configuration"
11
11
  require_relative "../current_context"
12
12
  require_relative "../trace_context"
13
+ require_relative "capturing_stream"
13
14
 
14
15
  module Profiler
15
16
  module Instrumentation
@@ -34,6 +35,7 @@ module Profiler
34
35
  url = Redaction.filter_url(build_url(host, port, req.path, use_ssl?))
35
36
  limit = Profiler.configuration.max_captured_body_bytes
36
37
  captured = NetHttpInstrumentation.capture_request_body(req, body, limit)
38
+ tee = captured[:tee]
37
39
  req_headers = Redaction.filter_headers(req.to_hash.transform_values { |v| v.join(", ") })
38
40
  req_content_type = req["content-type"].to_s
39
41
  req_body = captured[:content]
@@ -118,6 +120,7 @@ module Profiler
118
120
  raise
119
121
  ensure
120
122
  Thread.current[:profiler_http_recording] = false
123
+ NetHttpInstrumentation.finish_stream_capture(req, tee, entry, collector) if defined?(tee) && tee
121
124
  end
122
125
 
123
126
  private
@@ -222,7 +225,9 @@ module Profiler
222
225
  declared = req["content-length"].to_s
223
226
  declared = declared.match?(/\A\d+\z/) ? declared.to_i : nil
224
227
  unless rewindable?(stream)
225
- return { content: nil, size: declared, size_is_minimum: false, truncated: false, not_captured: true }
228
+ # Read as Net::HTTP sends it, by a stand-in that copies what passes (see finish_stream_capture).
229
+ tee = tee_stream(req, stream, limit)
230
+ return { content: nil, size: declared, size_is_minimum: false, truncated: false, not_captured: true, tee: tee }
226
231
  end
227
232
 
228
233
  position = stream.pos
@@ -246,6 +251,48 @@ module Profiler
246
251
  { content: nil, size: declared, size_is_minimum: false, truncated: false, not_captured: true }
247
252
  end
248
253
 
254
+ # Puts a CapturingStream in place of +stream+ on the request; nil (the request untouched) when
255
+ # the stream cannot be read or the stand-in cannot be installed.
256
+ def self.tee_stream(req, stream, limit)
257
+ return nil unless stream.respond_to?(:read)
258
+
259
+ # A JSON, NDJSON or form body is redacted whole before it is cut, so more of it is copied.
260
+ mime = req["content-type"].to_s.split(";").first.to_s.strip.downcase
261
+ copy = limit && mime.match?(%r{json|x-www-form-urlencoded}) ? Redaction::MAX_REDACTABLE_BODY_BYTES : limit
262
+ tee = CapturingStream.new(stream, copy)
263
+ req.body_stream = tee
264
+ tee
265
+ rescue StandardError => e
266
+ Profiler.log_error_once(:net_http_body_stream_tee, "NetHttpInstrumentation: could not capture a request body_stream", e)
267
+ nil
268
+ end
269
+
270
+ # Once the request has been sent (or has failed): the original stream goes back on the
271
+ # request, and the entry gets the body that passed, redacted like any other. An entry whose
272
+ # stream was never read stays "not captured".
273
+ def self.finish_stream_capture(req, tee, entry, collector)
274
+ req.body_stream = tee.original if req.body_stream.equal?(tee)
275
+ return unless entry && tee.total_read.positive?
276
+
277
+ limit = Profiler.configuration.max_captured_body_bytes
278
+ declared = req["content-length"].to_s
279
+ declared = declared.match?(/\A\d+\z/) ? declared.to_i : nil
280
+ raw = tee.captured
281
+ size = declared || tee.total_read
282
+ cut = limit && raw.bytesize > limit
283
+ processed = process_body(raw.force_encoding(Encoding::UTF_8), req["content-type"].to_s,
284
+ limit: (limit if cut), size: size)
285
+ kept = cut ? limit : raw.bytesize
286
+ collector.update_request(
287
+ entry,
288
+ request_body: processed[:body], request_body_encoding: processed[:encoding],
289
+ request_size: size, request_size_is_minimum: declared.nil? && !tee.finished?,
290
+ request_body_truncated: kept < size, request_body_not_captured: false
291
+ )
292
+ rescue StandardError => e
293
+ Profiler.log_error_once(:net_http_body_stream_tee, "NetHttpInstrumentation: could not record a request body_stream", e)
294
+ end
295
+
249
296
  # What is left to read of a stream that knows its size (a StringIO, a file), else nil.
250
297
  def self.remaining_size(stream, position)
251
298
  size = stream.respond_to?(:size) ? stream.size : nil
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.45.2"
4
+ VERSION = "0.45.3"
5
5
 
6
6
  GEM_ROOT = File.expand_path("../..", __dir__)
7
7
 
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.45.2
4
+ version: 0.45.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
@@ -194,6 +194,7 @@ files:
194
194
  - lib/profiler/env_override_store.rb
195
195
  - lib/profiler/explain_runner.rb
196
196
  - lib/profiler/instrumentation/active_job_instrumentation.rb
197
+ - lib/profiler/instrumentation/capturing_stream.rb
197
198
  - lib/profiler/instrumentation/executor_context_propagation.rb
198
199
  - lib/profiler/instrumentation/irb_instrumentation.rb
199
200
  - lib/profiler/instrumentation/net_http_instrumentation.rb