rails-profiler 0.44.26 → 0.44.28

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: 21bc2d1a836ed387db93a03b924c8af44d69d0d6f8d9d50e6ebf2ca9117d038f
4
- data.tar.gz: 4afb33c62cc46edae07feb0c3e9fc825f26451b430f3888ae7a71915ece91749
3
+ metadata.gz: db4bcba43c1c1b5d0870757e25bc084c42ce53150ca7e61e2f9cf18404a82314
4
+ data.tar.gz: d777658db9176d4e5b1d08cf3e2b5eba2c4c7a6477329d3873170377c1aa0463
5
5
  SHA512:
6
- metadata.gz: 0b048545adaa158803c1beb9d2c8eff6adcd42ab7fc2722dd918a4887096c75cc3ece99ec56b2b5d140323efbdb67fa4b9e16cbdac1734bfe732cdce7cd4db31
7
- data.tar.gz: 2a933760b94b6bc7e2890a69b00431bb013feeb8138ccf0f2827ac9d222de066a340ea2bac2fdafbf0bb3bb1a2d7e5b2648fe6ddd80dfb17a2473105c61f9d75
6
+ metadata.gz: 1c27ced14b0f9b1dc252242dce5d3e525116c0030e19209ca6c9e01067fe9c970f740e7333cbfb27216bef74e33ba426bc98717e23f3253bcde775cad1abb232
7
+ data.tar.gz: eea5be61d9ca47fcecf1454e1d6a7bf960c76671eaea78e9d62839407d8ab7e6b226983c4d02a85a85bffb5fc3698f6a00a09e77f93ccc589cc5621542dcbc2e
data/CHANGELOG.md CHANGED
@@ -19,6 +19,25 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.44.28] - 2026-10-26
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Fixed
27
+
28
+ - **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
29
+ - **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
30
+ - **SqlStatement:** Refuse MariaDB executable comments (`/*M! ... */`) for EXPLAIN like the MySQL ones (`/*! ... */`). Refs #72
31
+ - **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
32
+
33
+ ## [0.44.27] - 2026-10-26
34
+
35
+ <!-- stamped -->
36
+
37
+ ### Fixed
38
+
39
+ - **MCP tools:** cursor pagination now uses microsecond precision timestamps, so profiles started within the same second are no longer lost between pages. The SQLite store keeps its times with microseconds too (existing rows are padded once on open), and the `query_mailers` cursor carries the position of the last delivery shown so that the deliveries of one request are not dropped at a page boundary.
40
+
22
41
  ## [0.44.26] - 2026-10-26
23
42
 
24
43
  <!-- stamped -->
@@ -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
@@ -91,7 +91,7 @@ module Profiler
91
91
 
92
92
  if consoles.size == limit
93
93
  lines << ""
94
- lines << "*Next cursor: #{consoles.last.started_at.iso8601}*"
94
+ lines << "*Next cursor: #{consoles.last.started_at.iso8601(6)}*"
95
95
  end
96
96
 
97
97
  lines.join("\n")
@@ -83,7 +83,7 @@ module Profiler
83
83
 
84
84
  if jobs.size == limit
85
85
  lines << ""
86
- lines << "*Next cursor: #{jobs.last.started_at.iso8601}*"
86
+ lines << "*Next cursor: #{jobs.last.started_at.iso8601(6)}*"
87
87
  end
88
88
 
89
89
  lines.join("\n")
@@ -22,8 +22,9 @@ module Profiler
22
22
  next unless mailer_data
23
23
 
24
24
  all_emails = Array(mailer_data["emails"]) + Array(mailer_data["errors"])
25
- all_emails.each do |email|
26
- entry = email.merge("profile_token" => profile.token, "profile_started_at" => profile.started_at)
25
+ all_emails.each_with_index do |email, index|
26
+ entry = email.merge("profile_token" => profile.token, "profile_started_at" => profile.started_at,
27
+ "profile_index" => index)
27
28
 
28
29
  if params["mailer_class"]
29
30
  next unless entry["mailer_class"].to_s.downcase.include?(params["mailer_class"].downcase)
@@ -49,15 +50,23 @@ module Profiler
49
50
  end
50
51
 
51
52
  if params["cursor"]
53
+ # "<time>|<n>": the n-th delivery of the profile started at <time> was the last one
54
+ # shown, so the others of that profile (same start time) still come after it.
55
+ time_part, index_part = params["cursor"].to_s.split("|", 2)
52
56
  cutoff = begin
53
- Time.parse(params["cursor"])
57
+ Time.parse(time_part)
54
58
  rescue ArgumentError, TypeError
55
59
  nil # a cursor that is not a time is ignored, as before
56
60
  end
61
+ last_index = Integer(index_part, exception: false) if index_part
57
62
  if cutoff
63
+ cutoff_key = cutoff.getutc.iso8601(6)
58
64
  emails = emails.select do |e|
59
65
  started = e["profile_started_at"]
60
- started && started < cutoff
66
+ next false unless started
67
+
68
+ started < cutoff ||
69
+ (last_index && started.getutc.iso8601(6) == cutoff_key && e["profile_index"] > last_index)
61
70
  end
62
71
  end
63
72
  end
@@ -109,7 +118,7 @@ module Profiler
109
118
  if emails.size == limit
110
119
  last_started = emails.last["profile_started_at"]
111
120
  lines << ""
112
- lines << "*Next cursor: #{last_started.iso8601}*" if last_started
121
+ lines << "*Next cursor: #{last_started.iso8601(6)}|#{emails.last["profile_index"]}*" if last_started
113
122
  end
114
123
 
115
124
  lines.join("\n")
@@ -77,7 +77,7 @@ module Profiler
77
77
 
78
78
  if profiles.size == limit
79
79
  lines << ""
80
- lines << "*Next cursor: #{profiles.last.started_at.iso8601}*"
80
+ lines << "*Next cursor: #{profiles.last.started_at.iso8601(6)}*"
81
81
  end
82
82
 
83
83
  lines.join("\n")
@@ -90,7 +90,7 @@ module Profiler
90
90
 
91
91
  if tests.size == limit
92
92
  lines << ""
93
- lines << "*Next cursor: #{tests.last.started_at.iso8601}*"
93
+ lines << "*Next cursor: #{tests.last.started_at.iso8601(6)}*"
94
94
  end
95
95
 
96
96
  lines.join("\n")
@@ -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
@@ -149,7 +149,7 @@ module Profiler
149
149
  duration: @duration,
150
150
  allocated_objects: @allocated_objects,
151
151
  memory: memory,
152
- started_at: @started_at&.iso8601,
152
+ started_at: @started_at&.iso8601(6),
153
153
  finished_at: @finished_at&.iso8601,
154
154
  params: @params,
155
155
  params_truncated: @params_truncated,
@@ -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) }
@@ -196,7 +196,7 @@ module Profiler
196
196
  end
197
197
 
198
198
  def cleanup(older_than: 24 * 60 * 60)
199
- cutoff = (Time.now - older_than).utc.iso8601
199
+ cutoff = (Time.now - older_than).utc.iso8601(6)
200
200
  tokens = @db.execute(
201
201
  "SELECT token FROM profiler_profiles WHERE started_at < :cutoff", cutoff: cutoff
202
202
  ).map { |r| r["token"] }
@@ -323,6 +323,7 @@ module Profiler
323
323
 
324
324
  @db.execute("CREATE INDEX IF NOT EXISTS idx_profiler_trace_id ON profiler_profiles(trace_id)")
325
325
  migrate_times_to_utc
326
+ migrate_times_to_microseconds
326
327
  end
327
328
 
328
329
  # Version 1 stores the times in UTC. The rows written before it carry the local offset of
@@ -343,6 +344,23 @@ module Profiler
343
344
  end
344
345
  end
345
346
 
347
+ # Version 2 stores the times with microseconds. As text, "...:00Z" sorts after "...:00.5Z"
348
+ # ('.' < 'Z'), so the rows written without a fraction are padded to the same width.
349
+ def migrate_times_to_microseconds
350
+ return if @db.get_first_value("PRAGMA user_version").to_i >= 2
351
+
352
+ @db.transaction do
353
+ %w[started_at finished_at].each do |column|
354
+ @db.execute(<<~SQL)
355
+ UPDATE profiler_profiles
356
+ SET #{column} = substr(#{column}, 1, length(#{column}) - 1) || '.000000Z'
357
+ WHERE #{column} LIKE '____-__-__T__:__:__Z'
358
+ SQL
359
+ end
360
+ @db.execute("PRAGMA user_version = 2")
361
+ end
362
+ end
363
+
346
364
  def row_to_profile(row, load_blobs: true)
347
365
  collectors_meta = parse_json(row["collectors_meta"], {})
348
366
 
@@ -396,12 +414,13 @@ module Profiler
396
414
  nil
397
415
  end
398
416
 
399
- # The times are stored in UTC, all in the same form, for the cleanup and the lists to compare
400
- # them as text; a profile gives them with the offset of the process that recorded it.
417
+ # The times are stored in UTC, all in the same fixed-width form (microseconds, so profiles
418
+ # started within the same second keep their order and their MCP cursor), for the cleanup and
419
+ # the lists to compare them as text; a profile gives them with the offset of the process that recorded it.
401
420
  def utc_iso8601(value)
402
421
  return value if value.nil?
403
422
 
404
- Time.iso8601(value.to_s).utc.iso8601
423
+ Time.iso8601(value.to_s).utc.iso8601(6)
405
424
  rescue ArgumentError
406
425
  value
407
426
  end
@@ -410,7 +429,7 @@ module Profiler
410
429
  def local_iso8601(value)
411
430
  return value if value.nil?
412
431
 
413
- Time.iso8601(value).localtime.iso8601
432
+ Time.iso8601(value).localtime.iso8601(6)
414
433
  rescue ArgumentError
415
434
  value
416
435
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.44.26"
4
+ VERSION = "0.44.28"
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.26
4
+ version: 0.44.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy