rails-profiler 0.44.27 → 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 +4 -4
- data/CHANGELOG.md +11 -0
- data/lib/profiler/cluster/token_cache.rb +23 -1
- data/lib/profiler/collectors/database_collector.rb +1 -1
- data/lib/profiler/collectors/flamegraph_collector.rb +2 -1
- data/lib/profiler/current_context.rb +20 -0
- data/lib/profiler/instrumentation/net_http_instrumentation.rb +5 -3
- data/lib/profiler/middleware/profiler_middleware.rb +2 -0
- data/lib/profiler/middleware/streamed_profile.rb +9 -2
- data/lib/profiler/models/sql_query.rb +3 -1
- data/lib/profiler/sql_statement.rb +5 -2
- data/lib/profiler/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: db4bcba43c1c1b5d0870757e25bc084c42ce53150ca7e61e2f9cf18404a82314
|
|
4
|
+
data.tar.gz: d777658db9176d4e5b1d08cf3e2b5eba2c4c7a6477329d3873170377c1aa0463
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1c27ced14b0f9b1dc252242dce5d3e525116c0030e19209ca6c9e01067fe9c970f740e7333cbfb27216bef74e33ba426bc98717e23f3253bcde775cad1abb232
|
|
7
|
+
data.tar.gz: eea5be61d9ca47fcecf1454e1d6a7bf960c76671eaea78e9d62839407d8ab7e6b226983c4d02a85a85bffb5fc3698f6a00a09e77f93ccc589cc5621542dcbc2e
|
data/CHANGELOG.md
CHANGED
|
@@ -19,6 +19,17 @@ 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
|
+
|
|
22
33
|
## [0.44.27] - 2026-10-26
|
|
23
34
|
|
|
24
35
|
<!-- 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?
|
|
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] =~
|
|
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] =~
|
|
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
|
|
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
|
-
|
|
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 =~
|
|
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.
|
|
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) }
|
data/lib/profiler/version.rb
CHANGED