rails-profiler 0.44.27 → 0.44.29

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: 6f2ed2fe704d2027059860b5738d7a188b8374020aa60041d7121be951b6afdb
4
- data.tar.gz: 3b6a7bfe912382205fdb7b04a390634c2763805b18f6f41c93dda2d2d9d2aefe
3
+ metadata.gz: 8aaceadc8eb83f058028d9119aaa8d520fe326f3229102165cc9d47a4f8e50a9
4
+ data.tar.gz: 983d4aaad3182af60c19626a72f55cd84b8de8e109336438faec7079d3c385c1
5
5
  SHA512:
6
- metadata.gz: dccc3fb45bcb676d5467e01cead1d205adc52ddde042a9587639b95351300ac622e21ae8db9f2d4a86b255e1ab8b77241bc59d39e26faed7b7c0067172aac60d
7
- data.tar.gz: 5888e29b8dcccbd98705c36dabfadab10c62bc9e141c4f53b35061c8ea3ecd0d0f7ed70ff217922d676e9282870e2c98f62c767e48b1c8573195fda342a6587c
6
+ metadata.gz: c0b55328521b2285527700c7b7674e71abcfac7189cdb5538e0eda34336faac7f3c090b49acc9bdb2d82b96df8df6cc0d50920c536e12d49c979626e603854ec
7
+ data.tar.gz: 9679682523471ae75f48a570f114f770dcecdd79280fdfcefd84c97c9c4f0fb45e2eb2ee3a2516cff102a9d37caafc228de6c86b8cedecaf86a7c77d98da790b
data/CHANGELOG.md CHANGED
@@ -19,6 +19,28 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.44.29] - 2026-10-26
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Fixed
27
+
28
+ - **Profiles:** Validate that `filter` parameter is a Hash before attempting to access its keys; previously passing a string or other type caused `NoMethodError` resulting in a 500 error. Now returns 422 with a clear error message. Refs #74
29
+ - **Profiles:** Clamp `limit` and `offset` to non-negative values using the existing `page_params` helper; a negative limit previously raised `ArgumentError` from `Array#first`, resulting in a 500 error. Refs #74
30
+ - **EnvVars:** Refuse an array or object `value` with 422 instead of storing its inspect string; nil, strings and JSON scalars (numbers, booleans) are still accepted and coerced with `to_s`. Refs #74
31
+ - **TestRunner:** Answer 422 when the `framework` parameter of `POST test_runner/runs` or `GET test_runner/files` is not a string, instead of a 500 from `NoMethodError`; `Runner.start` and `Discovery.files` raise `ArgumentError` for a framework that is neither a String nor a Symbol. Refs #74
32
+
33
+ ## [0.44.28] - 2026-10-26
34
+
35
+ <!-- stamped -->
36
+
37
+ ### Fixed
38
+
39
+ - **TokenCache:** Delete expired entries in `fetch()` instead of leaving them in memory, and once the cache holds 1000 entries, sweep the expired ones and then evict the oldest so it stays bounded. Refs #72
40
+ - **NetHttpInstrumentation:** Reuse the current request's span ID in outgoing `traceparent` headers instead of generating a random one, and propagate incoming trace flags (also while a streamed body is iterated) to maintain consistency across the distributed trace. Refs #72
41
+ - **SqlStatement:** Refuse MariaDB executable comments (`/*M! ... */`) for EXPLAIN like the MySQL ones (`/*! ... */`). Refs #72
42
+ - **SqlQuery:** Fix transaction detection regex to properly anchor at the start of the statement and handle leading whitespace, and extract a shared `TRANSACTION_REGEX` constant used across collectors and the query model to prevent drift. Also recognize `SAVEPOINT` and `RELEASE` as transaction statements. Refs #72
43
+
22
44
  ## [0.44.27] - 2026-10-26
23
45
 
24
46
  <!-- stamped -->
@@ -32,6 +32,14 @@ module Profiler
32
32
 
33
33
  value = params[:value]
34
34
 
35
+ # A structured value (JSON array or object) has no ENV representation. Scalars (numbers,
36
+ # booleans) are still coerced with to_s, as before.
37
+ if value.is_a?(Array) || value.is_a?(Hash) || value.respond_to?(:to_unsafe_h)
38
+ render json: { error: "Value must be a scalar, not an array or an object; #{key} was left unchanged" },
39
+ status: :unprocessable_entity
40
+ return
41
+ end
42
+
35
43
  # A value ENV cannot hold is refused before the store is written, like a refused key.
36
44
  if (reason = Profiler::EnvKeyPolicy.value_rejection(key, value))
37
45
  render json: { error: "#{reason}; #{key} was left unchanged" }, status: :unprocessable_entity
@@ -16,6 +16,8 @@ module Profiler
16
16
 
17
17
  def files
18
18
  framework = params[:framework]
19
+ return render_invalid_framework unless framework.nil? || framework.is_a?(String)
20
+
19
21
  tree = Profiler::TestRunner::Discovery.files(framework: framework)
20
22
  frameworks = Profiler::TestRunner::Discovery.frameworks
21
23
 
@@ -28,6 +30,7 @@ module Profiler
28
30
  def create
29
31
  files = Array(params[:files])
30
32
  framework = params[:framework] || detect_framework
33
+ return render_invalid_framework unless framework.is_a?(String)
31
34
 
32
35
  if files.empty?
33
36
  return render json: { error: "No files selected" }, status: :unprocessable_entity
@@ -88,6 +91,10 @@ module Profiler
88
91
 
89
92
  private
90
93
 
94
+ def render_invalid_framework
95
+ render json: { error: "framework must be a String" }, status: :unprocessable_entity
96
+ end
97
+
91
98
  def detect_framework
92
99
  if Profiler::TestRunner::Discovery.rspec_available?
93
100
  "rspec"
@@ -5,8 +5,13 @@ require_relative "../../../lib/profiler/cluster/slave_proxy"
5
5
  module Profiler
6
6
  class ProfilesController < ApplicationController
7
7
  def index
8
- limit = params[:limit]&.to_i || 50
9
- offset = params[:offset]&.to_i || 0
8
+ limit, offset = page_params
9
+
10
+ # Validate filter parameter is a Hash if present
11
+ if params[:filter].present? && !params[:filter].respond_to?(:to_unsafe_h) && !params[:filter].is_a?(Hash)
12
+ render json: { error: "filter must be a hash" }, status: :unprocessable_entity
13
+ return
14
+ end
10
15
 
11
16
  @profiles = Profiler.storage.list(limit: limit, offset: offset)
12
17
  @profiles = filter_profiles(@profiles) if params[:filter].present?
@@ -6,6 +6,7 @@ module Profiler
6
6
  module Cluster
7
7
  class TokenCache
8
8
  TTL = 600 # 10 minutes
9
+ MAX_ENTRIES = 1000
9
10
 
10
11
  def initialize
11
12
  @cache = Concurrent::Map.new
@@ -13,18 +14,39 @@ module Profiler
13
14
 
14
15
  def fetch(token)
15
16
  entry = @cache[token]
16
- return nil if entry.nil? || Time.now > entry[:expires_at]
17
+ return nil if entry.nil?
18
+
19
+ if Time.now > entry[:expires_at]
20
+ @cache.delete_pair(token, entry)
21
+ return nil
22
+ end
17
23
 
18
24
  entry[:slave_name]
19
25
  end
20
26
 
21
27
  def store(token, slave_name)
28
+ make_room if @cache.size >= MAX_ENTRIES
22
29
  @cache[token] = { slave_name: slave_name, expires_at: Time.now + TTL }
23
30
  end
24
31
 
25
32
  def invalidate(token)
26
33
  @cache.delete(token)
27
34
  end
35
+
36
+ private
37
+
38
+ # Drops the expired entries, then the oldest live ones, until there is room for one more.
39
+ def make_room
40
+ now = Time.now
41
+ @cache.each_pair { |token, entry| @cache.delete_pair(token, entry) if now > entry[:expires_at] }
42
+ excess = @cache.size - MAX_ENTRIES + 1
43
+ return unless excess.positive?
44
+
45
+ pairs = []
46
+ @cache.each_pair { |token, entry| pairs << [token, entry] }
47
+ oldest = pairs.min_by(excess) { |_token, entry| entry[:expires_at] }
48
+ oldest.each { |token, entry| @cache.delete_pair(token, entry) }
49
+ end
28
50
  end
29
51
  end
30
52
  end
@@ -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] =~ /\A\s*(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE)\b/i
61
+ next if payload[:sql] =~ Models::SqlQuery::TRANSACTION_REGEX
62
62
 
63
63
  query = Models::SqlQuery.new(
64
64
  sql: payload[:sql],
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "base_collector"
4
4
  require_relative "../models/timeline_event"
5
+ require_relative "../models/sql_query"
5
6
 
6
7
  module Profiler
7
8
  module Collectors
@@ -83,7 +84,7 @@ module Profiler
83
84
  # SQL queries
84
85
  @subscriptions << subscribe_notification("sql.active_record") do |_name, started, finished, _unique_id, payload|
85
86
  next if payload[:name] == "SCHEMA"
86
- next if payload[:sql] =~ /\A\s*(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE)\b/i
87
+ next if payload[:sql] =~ Models::SqlQuery::TRANSACTION_REGEX
87
88
 
88
89
  sql = payload[:sql].to_s
89
90
  add_event Models::TimelineEvent.new(
@@ -19,9 +19,29 @@ module Profiler
19
19
  Thread.current[:profiler_trace_id] = value
20
20
  end
21
21
 
22
+ # The span id of the profile in progress, for the outgoing calls to carry as parent.
23
+ def self.span_id
24
+ Thread.current[:profiler_span_id]
25
+ end
26
+
27
+ def self.span_id=(value)
28
+ Thread.current[:profiler_span_id] = value
29
+ end
30
+
31
+ # The trace flags from the incoming request (if any) to propagate outward.
32
+ def self.trace_flags
33
+ Thread.current[:profiler_trace_flags]
34
+ end
35
+
36
+ def self.trace_flags=(value)
37
+ Thread.current[:profiler_trace_flags] = value
38
+ end
39
+
22
40
  def self.clear
23
41
  Thread.current[:profiler_token] = nil
24
42
  Thread.current[:profiler_trace_id] = nil
43
+ Thread.current[:profiler_span_id] = nil
44
+ Thread.current[:profiler_trace_flags] = nil
25
45
  end
26
46
  end
27
47
  end
@@ -138,15 +138,17 @@ module Profiler
138
138
  end
139
139
 
140
140
  # Adds a traceparent to a call made while a profiled request runs on this thread: the trace
141
- # id of that request and a span id of its own. A traceparent the application set is kept,
142
- # and a host excluded from the instrumentation gets none.
141
+ # id of that request and the current span as parent. A traceparent the application set is
142
+ # kept, and a host excluded from the instrumentation gets none.
143
143
  def self.inject_trace_context(req, host, port)
144
144
  trace_id = CurrentContext.trace_id
145
145
  return unless trace_id
146
146
  return if req[TraceContext::HEADER]
147
147
  return if skip_host?(host, port)
148
148
 
149
- req[TraceContext::HEADER] = TraceContext.format(trace_id, TraceContext.new_span_id)
149
+ parent_span_id = CurrentContext.span_id || TraceContext.new_span_id
150
+ trace_flags = CurrentContext.trace_flags || "01"
151
+ req[TraceContext::HEADER] = TraceContext.format(trace_id, parent_span_id, trace_flags)
150
152
  rescue => e
151
153
  Profiler.log_error("NetHttpInstrumentation: could not add the traceparent header", e)
152
154
  end
@@ -90,6 +90,8 @@ module Profiler
90
90
  profile.parent_span_id = incoming&.parent_id
91
91
  profile.span_id = TraceContext.new_span_id
92
92
  Profiler::CurrentContext.trace_id = profile.trace_id
93
+ Profiler::CurrentContext.span_id = profile.span_id
94
+ Profiler::CurrentContext.trace_flags = incoming&.flags || "01"
93
95
  end
94
96
 
95
97
  # Read after the application has run, so after the routing: the route parameters are there,
@@ -66,6 +66,7 @@ module Profiler
66
66
  @collectors = collectors
67
67
  @on_finish = on_finish
68
68
  @context = Fiber.current
69
+ @trace_flags = CurrentContext.trace_flags
69
70
  @started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
70
71
  @mutex = Mutex.new
71
72
  @released = false
@@ -115,8 +116,12 @@ module Profiler
115
116
  def enter
116
117
  token = CurrentContext.token
117
118
  trace_id = CurrentContext.trace_id
119
+ span_id = CurrentContext.span_id
120
+ trace_flags = CurrentContext.trace_flags
118
121
  CurrentContext.token = @profile.token
119
122
  CurrentContext.trace_id = @profile.trace_id
123
+ CurrentContext.span_id = @profile.span_id
124
+ CurrentContext.trace_flags = @trace_flags
120
125
  lent = @mutex.synchronize do
121
126
  next [] if @released
122
127
 
@@ -124,14 +129,16 @@ module Profiler
124
129
  [collector, collector.lend_thread_slots] if collector.respond_to?(:lend_thread_slots)
125
130
  end
126
131
  end
127
- [token, lent, trace_id]
132
+ [token, lent, trace_id, span_id, trace_flags]
128
133
  end
129
134
 
130
135
  def leave(state)
131
- token, lent, trace_id = state
136
+ token, lent, trace_id, span_id, trace_flags = state
132
137
  lent.reverse_each { |collector, previous| collector.return_thread_slots(previous) }
133
138
  CurrentContext.token = token
134
139
  CurrentContext.trace_id = trace_id
140
+ CurrentContext.span_id = span_id
141
+ CurrentContext.trace_flags = trace_flags
135
142
  end
136
143
  end
137
144
  end
@@ -3,6 +3,8 @@
3
3
  module Profiler
4
4
  module Models
5
5
  class SqlQuery
6
+ TRANSACTION_REGEX = /\A\s*(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE)\b/i.freeze
7
+
6
8
  attr_reader :sql, :duration, :binds, :name, :connection, :backtrace, :started_at
7
9
 
8
10
  def initialize(sql:, duration:, binds: [], name: nil, connection: nil, backtrace: [], started_at: nil)
@@ -24,7 +26,7 @@ module Profiler
24
26
  end
25
27
 
26
28
  def transaction?
27
- @sql =~ /^(BEGIN|COMMIT|ROLLBACK)/i
29
+ @sql =~ TRANSACTION_REGEX
28
30
  end
29
31
 
30
32
  def to_h
@@ -17,6 +17,9 @@ module Profiler
17
17
  # named `updated_at` is fine and an unquoted column named `share` is refused.
18
18
  WRITE_KEYWORDS = %w[INSERT UPDATE DELETE MERGE TRUNCATE DROP ALTER CREATE INTO LOCK SHARE].freeze
19
19
 
20
+ # MariaDB executable and version-specific comments.
21
+ MARIADB_COMMENT = /\A\/\*M?!/.freeze
22
+
20
23
  Token = Struct.new(:type, :text)
21
24
 
22
25
  # 'it''s' everywhere; 'it\'s' too where a backslash escapes. An unterminated
@@ -36,8 +39,8 @@ module Profiler
36
39
  # @return [String, nil] why the statement must not be explained, or nil when it reads only
37
40
  def self.read_only_refusal(sql, dialect:)
38
41
  all = tokenize(sql, dialect)
39
- if dialect == :mysql && all.any? { |t| t.type == :comment && t.text.start_with?("/*!") }
40
- return refusal("it holds a MySQL executable comment (/*! ... */)")
42
+ if dialect == :mysql && all.any? { |t| t.type == :comment && t.text.match?(MARIADB_COMMENT) }
43
+ return refusal("it holds a MySQL or MariaDB executable comment (/*! ... */ or /*M! ... */)")
41
44
  end
42
45
 
43
46
  tokens = all.reject { |t| %i[space comment].include?(t.type) }
@@ -15,6 +15,12 @@ module Profiler
15
15
 
16
16
  def self.files(framework: nil)
17
17
  root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
18
+
19
+ # Validate framework is a String or Symbol if provided
20
+ if !framework.nil? && !framework.is_a?(String) && !framework.is_a?(Symbol)
21
+ raise ArgumentError, "framework must be a String or Symbol"
22
+ end
23
+
18
24
  result = {}
19
25
 
20
26
  globs = case framework&.to_sym
@@ -24,6 +24,12 @@ module Profiler
24
24
  # Raises TooManyRunsError when config.test_runner_max_active_runs runs still have their
25
25
  # test process.
26
26
  def self.start(files:, framework:)
27
+ unless framework.is_a?(String) || framework.is_a?(Symbol)
28
+ raise ArgumentError, "framework must be a String or Symbol"
29
+ end
30
+
31
+ framework = framework.to_s
32
+
27
33
  validate_files!(files, framework)
28
34
  config = Profiler.configuration
29
35
  run = Profiler::TestRunner.run_store.create(
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.44.27"
4
+ VERSION = "0.44.29"
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.44.27
4
+ version: 0.44.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy