rails-profiler 0.45.0 → 0.45.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.
@@ -25,10 +25,21 @@ module Profiler
25
25
  Collectors::FlameGraphCollector
26
26
  ].freeze
27
27
 
28
+ RECORDING_KEY = :profiler_console_recording
29
+
28
30
  def self.profile(expression:, &block)
29
31
  return block.call unless Profiler.enabled? && Profiler.configuration.track_console
30
32
 
31
- new(expression: expression).run(&block)
33
+ # IRB and Pry can both be loaded and both evaluate the same expression: only the
34
+ # outermost one records it.
35
+ return block.call if Thread.current[RECORDING_KEY]
36
+
37
+ begin
38
+ Thread.current[RECORDING_KEY] = true
39
+ new(expression: expression).run(&block)
40
+ ensure
41
+ Thread.current[RECORDING_KEY] = nil
42
+ end
32
43
  end
33
44
 
34
45
  def initialize(expression:)
@@ -37,7 +37,15 @@ module Profiler
37
37
  req_headers = Redaction.filter_headers(req.to_hash.transform_values { |v| v.join(", ") })
38
38
  req_content_type = req["content-type"].to_s
39
39
  req_body = captured[:content]
40
- processed_req = req_body.nil? || req_body.empty? ? { body: nil, encoding: "text" } : NetHttpInstrumentation.process_body(req_body, req_content_type)
40
+ processed_req = if req_body.nil? || req_body.empty?
41
+ { body: nil, encoding: "text" }
42
+ else
43
+ NetHttpInstrumentation.process_body(
44
+ req_body, req_content_type,
45
+ limit: captured[:truncated] ? limit : nil,
46
+ size: captured[:size_is_minimum] ? nil : captured[:size]
47
+ )
48
+ end
41
49
 
42
50
  request_id = SecureRandom.hex(8)
43
51
  started_at = Time.now.iso8601(3)
@@ -75,9 +83,13 @@ module Profiler
75
83
  resp_content_encoding = response["content-encoding"].to_s.strip.downcase
76
84
  resp_body = NetHttpInstrumentation.decompress_body(resp_body_raw, resp_content_encoding, limit)
77
85
  resp_truncated = limit ? resp_body.bytesize > limit : false
78
- resp_body = Redaction.cut_bytes(resp_body, limit) if resp_truncated
79
86
  resp_content_type = response["content-type"].to_s
80
- processed_resp = NetHttpInstrumentation.process_body(resp_body, resp_content_type)
87
+ # Cut after the redaction, not before: a cut JSON text cannot be parsed. Only an
88
+ # inflation stopped at the limit leaves a beginning with no known whole size.
89
+ resp_whole_size = !resp_truncated || resp_body.equal?(resp_body_raw) ? resp_body.bytesize : nil
90
+ processed_resp = NetHttpInstrumentation.process_body(
91
+ resp_body, resp_content_type, limit: resp_truncated ? limit : nil, size: resp_whole_size
92
+ )
81
93
 
82
94
  t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
83
95
 
@@ -200,8 +212,8 @@ module Profiler
200
212
  unless content.nil? || content.to_s.empty?
201
213
  content = content.to_s
202
214
  truncated = limit ? content.bytesize > limit : false
203
- return { content: truncated ? Redaction.cut_bytes(content, limit) : content,
204
- size: content.bytesize, size_is_minimum: false, truncated: truncated, not_captured: false }
215
+ # Kept whole: process_body redacts it, then cuts the result.
216
+ return { content: content, size: content.bytesize, size_is_minimum: false, truncated: truncated, not_captured: false }
205
217
  end
206
218
 
207
219
  stream = req.body_stream
@@ -295,18 +307,24 @@ module Profiler
295
307
  defined?(Net::ReadAdapter) && response.body.is_a?(Net::ReadAdapter)
296
308
  end
297
309
 
298
- def self.process_body(body, content_type)
310
+ # +limit+ is set when the body is kept only in part: it is then redacted whole and the result
311
+ # cut to +limit+ bytes (Redaction.filter_truncated_body). +size+ is the length of the whole
312
+ # body, nil when it is not known.
313
+ def self.process_body(body, content_type, limit: nil, size: body&.bytesize)
299
314
  return { body: nil, encoding: "text" } if body.nil? || body.empty?
300
315
 
301
316
  if BodyContent.binary?(body, content_type)
302
- # Masked on the raw bytes, before the encoding hides them from any later search.
303
- { body: Base64.strict_encode64(Redaction.hide_credentials(body.b)), encoding: "base64" }
317
+ # Masked on the raw bytes, before the encoding hides them from any later search, and
318
+ # cut after the masking: a credential cut in two would escape it.
319
+ masked = Redaction.hide_credentials(body.b)
320
+ masked = Redaction.cut_bytes(masked, limit) if limit
321
+ { body: Base64.strict_encode64(masked), encoding: "base64" }
304
322
  else
305
323
  # Bytes are read as UTF-8, as a text body on the wire is; what is not valid UTF-8 shows
306
324
  # as "?".
307
325
  body = body.dup.force_encoding(Encoding::UTF_8) if body.encoding == Encoding::BINARY
308
- text = Redaction.filter_body(body, content_type)
309
- .encode("UTF-8", invalid: :replace, undef: :replace, replace: "?")
326
+ filtered = limit ? Redaction.filter_truncated_body(body, content_type, limit, size) : Redaction.filter_body(body, content_type)
327
+ text = filtered.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?")
310
328
  text = text.scrub("?") unless text.valid_encoding?
311
329
  { body: text, encoding: "text" }
312
330
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Instrumentation
5
+ # Prepended to Pry. Pry commands (ls, cd...) do not go through #evaluate_ruby and are not profiled.
6
+ module PryInstrumentation
7
+ def evaluate_ruby(code)
8
+ Profiler.env_override_store.apply!
9
+ stripped = code.to_s.strip
10
+ skip = !Profiler.enabled? ||
11
+ !Profiler.configuration.track_console ||
12
+ stripped.empty?
13
+ return super if skip
14
+
15
+ Profiler::ConsoleProfiler.profile(expression: stripped) { super }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -169,8 +169,17 @@ module Profiler
169
169
  next unless Profiler.configuration.enabled && Profiler.configuration.track_console
170
170
 
171
171
  require_relative "console_profiler"
172
- require_relative "instrumentation/irb_instrumentation"
173
- IRB::Context.prepend(Profiler::Instrumentation::IrbInstrumentation)
172
+
173
+ # A console is IRB or Pry (pry-rails): instrument whichever is loaded, never assume IRB.
174
+ if defined?(IRB::Context)
175
+ require_relative "instrumentation/irb_instrumentation"
176
+ IRB::Context.prepend(Profiler::Instrumentation::IrbInstrumentation)
177
+ end
178
+
179
+ if defined?(Pry)
180
+ require_relative "instrumentation/pry_instrumentation"
181
+ Pry.prepend(Profiler::Instrumentation::PryInstrumentation)
182
+ end
174
183
  end
175
184
 
176
185
  rake_tasks do
@@ -36,6 +36,11 @@ module Profiler
36
36
  FORM_TYPE = "application/x-www-form-urlencoded"
37
37
  MULTIPART_TYPE = %r{\Amultipart/}i
38
38
 
39
+ # The largest body filter_truncated_body parses to redact it whole. A bigger JSON, NDJSON or
40
+ # form body is not parsed (a parse costs several times its size in memory) and is masked with
41
+ # a label instead, never passed through unredacted.
42
+ MAX_REDACTABLE_BODY_BYTES = 8 * 1024 * 1024
43
+
39
44
  # The characters outside ASCII whose case folding yields ASCII letters
40
45
  # (Unicode CaseFolding.txt): with any of them, a case-insensitive regexp
41
46
  # can match an ASCII filter where a plain ASCII search cannot.
@@ -52,7 +57,7 @@ module Profiler
52
57
  # profile can be read on any node. Wraps every public filter, so that each
53
58
  # capture point gets it.
54
59
  module CredentialMasking
55
- %i[filter_hash filter_value filter_named filter_headers filter_body filter_query filter_url
60
+ %i[filter_hash filter_value filter_named filter_headers filter_body filter_truncated_body filter_query filter_url
56
61
  env_snapshot env_value env_overrides].each do |name|
57
62
  define_method(name) { |*args| hide_credentials(super(*args)) }
58
63
  end
@@ -221,6 +226,35 @@ module Profiler
221
226
  "[FILTERED: #{mime} body that could not be filtered, #{raw.bytesize} bytes]"
222
227
  end
223
228
 
229
+ # The part of a body a profile keeps when it is longer than +limit+ bytes, redacted. The body
230
+ # is redacted whole first and the result is cut after, so the part kept is redaction output
231
+ # and never shows a value the filter masks. +size+ is the length of the whole body when
232
+ # known; the body is whole when +raw+ has that length, and only a beginning otherwise
233
+ # (an inflation stopped at the limit, a body_stream read in part).
234
+ #
235
+ # A JSON, NDJSON or form body that cannot be redacted whole (only its beginning is known, or
236
+ # it is over MAX_REDACTABLE_BODY_BYTES) is masked entirely with a label that says it was
237
+ # truncated: a cut JSON text cannot be parsed and a text search would miss keys. Other types
238
+ # are cut first, as filter_body leaves them as they are or masks them whole anyway. A whole
239
+ # body that is not valid JSON keeps the "unparseable" label of filter_body.
240
+ def filter_truncated_body(raw, content_type, limit, size = raw.is_a?(String) ? raw.bytesize : nil)
241
+ return raw unless raw.is_a?(String) && !raw.empty?
242
+ return filter_body(raw, content_type) if limit.nil? || (size == raw.bytesize && raw.bytesize <= limit)
243
+ return cut_bytes(raw, limit) unless enabled?
244
+
245
+ mime = content_type.to_s.split(";").first.to_s.strip.downcase
246
+ return filter_body(cut_bytes(raw, limit), content_type) unless structured_type?(mime)
247
+
248
+ whole = size == raw.bytesize
249
+ return cut_bytes(filter_body(raw, content_type), limit) if whole && raw.bytesize <= MAX_REDACTABLE_BODY_BYTES
250
+
251
+ reason = whole ? "too large to redact" : "only its beginning is known, it cannot be redacted"
252
+ "[FILTERED: truncated #{mime} body, kept #{limit} of #{size || "more than #{limit}"} bytes, #{reason}]"
253
+ rescue StandardError => e
254
+ report(e, "filtering a truncated body")
255
+ "[FILTERED: truncated body that could not be filtered, #{raw.bytesize} bytes]"
256
+ end
257
+
224
258
  # Masks the filtered values of a query string, pair by pair, leaving the
225
259
  # rest of the string exactly as it was.
226
260
  def filter_query(query)
@@ -481,6 +515,10 @@ module Profiler
481
515
  utf8.valid_encoding? ? utf8 : string.b
482
516
  end
483
517
 
518
+ def structured_type?(mime)
519
+ mime.match?(JSON_TYPES) || mime.match?(NDJSON_TYPES) || mime == FORM_TYPE
520
+ end
521
+
484
522
  def filter_json(text, mime)
485
523
  return text unless json_needs_parse?(text)
486
524
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.45.0"
4
+ VERSION = "0.45.2"
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.0
4
+ version: 0.45.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
@@ -197,6 +197,7 @@ files:
197
197
  - lib/profiler/instrumentation/executor_context_propagation.rb
198
198
  - lib/profiler/instrumentation/irb_instrumentation.rb
199
199
  - lib/profiler/instrumentation/net_http_instrumentation.rb
200
+ - lib/profiler/instrumentation/pry_instrumentation.rb
200
201
  - lib/profiler/instrumentation/request_context.rb
201
202
  - lib/profiler/instrumentation/sidekiq_middleware.rb
202
203
  - lib/profiler/instrumentation/thread_context_propagation.rb