apm_bro 0.1.13 → 0.1.15
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/README.md +0 -35
- data/lib/apm_bro/cache_subscriber.rb +15 -25
- data/lib/apm_bro/circuit_breaker.rb +9 -17
- data/lib/apm_bro/client.rb +28 -36
- data/lib/apm_bro/configuration.rb +44 -62
- data/lib/apm_bro/error_middleware.rb +16 -30
- data/lib/apm_bro/http_instrumentation.rb +22 -16
- data/lib/apm_bro/job_sql_tracking_middleware.rb +1 -1
- data/lib/apm_bro/job_subscriber.rb +41 -25
- data/lib/apm_bro/lightweight_memory_tracker.rb +8 -8
- data/lib/apm_bro/logger.rb +8 -9
- data/lib/apm_bro/memory_helpers.rb +13 -13
- data/lib/apm_bro/memory_leak_detector.rb +37 -37
- data/lib/apm_bro/memory_tracking_subscriber.rb +62 -54
- data/lib/apm_bro/railtie.rb +28 -30
- data/lib/apm_bro/redis_subscriber.rb +34 -37
- data/lib/apm_bro/sql_subscriber.rb +60 -59
- data/lib/apm_bro/sql_tracking_middleware.rb +11 -11
- data/lib/apm_bro/subscriber.rb +53 -39
- data/lib/apm_bro/version.rb +1 -1
- data/lib/apm_bro/view_rendering_subscriber.rb +23 -23
- metadata +4 -4
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
require "
|
|
3
|
+
begin
|
|
4
|
+
require "active_support/notifications"
|
|
5
|
+
rescue LoadError
|
|
6
|
+
# ActiveSupport not available
|
|
7
|
+
end
|
|
5
8
|
|
|
6
9
|
module ApmBro
|
|
7
10
|
class SqlSubscriber
|
|
8
|
-
SQL_EVENT_NAME = "sql.active_record"
|
|
11
|
+
SQL_EVENT_NAME = "sql.active_record"
|
|
9
12
|
THREAD_LOCAL_KEY = :apm_bro_sql_queries
|
|
10
13
|
THREAD_LOCAL_ALLOC_START_KEY = :apm_bro_sql_alloc_start
|
|
11
14
|
THREAD_LOCAL_ALLOC_RESULTS_KEY = :apm_bro_sql_alloc_results
|
|
@@ -16,7 +19,7 @@ module ApmBro
|
|
|
16
19
|
if ActiveSupport::Notifications.notifier.respond_to?(:subscribe)
|
|
17
20
|
begin
|
|
18
21
|
ActiveSupport::Notifications.notifier.subscribe(SQL_EVENT_NAME, SqlAllocListener.new)
|
|
19
|
-
rescue
|
|
22
|
+
rescue
|
|
20
23
|
end
|
|
21
24
|
end
|
|
22
25
|
|
|
@@ -30,11 +33,11 @@ module ApmBro
|
|
|
30
33
|
begin
|
|
31
34
|
alloc_results = Thread.current[THREAD_LOCAL_ALLOC_RESULTS_KEY]
|
|
32
35
|
allocations = alloc_results && alloc_results.delete(unique_id)
|
|
33
|
-
|
|
36
|
+
|
|
34
37
|
# Get the captured backtrace from when the query started
|
|
35
38
|
backtrace_map = Thread.current[THREAD_LOCAL_BACKTRACE_KEY]
|
|
36
39
|
captured_backtrace = backtrace_map && backtrace_map.delete(unique_id)
|
|
37
|
-
rescue
|
|
40
|
+
rescue
|
|
38
41
|
end
|
|
39
42
|
|
|
40
43
|
query_info = {
|
|
@@ -48,7 +51,6 @@ module ApmBro
|
|
|
48
51
|
}
|
|
49
52
|
# Add to thread-local storage
|
|
50
53
|
Thread.current[THREAD_LOCAL_KEY] << query_info
|
|
51
|
-
|
|
52
54
|
end
|
|
53
55
|
end
|
|
54
56
|
|
|
@@ -74,14 +76,14 @@ module ApmBro
|
|
|
74
76
|
# Remove sensitive data patterns
|
|
75
77
|
sql = sql.gsub(/\b(password|token|secret|key|ssn|credit_card)\s*=\s*['"][^'"]*['"]/i, '\1 = ?')
|
|
76
78
|
sql = sql.gsub(/\b(password|token|secret|key|ssn|credit_card)\s*=\s*[^'",\s)]+/i, '\1 = ?')
|
|
77
|
-
|
|
79
|
+
|
|
78
80
|
# Remove specific values in WHERE clauses that might be sensitive
|
|
79
81
|
sql = sql.gsub(/WHERE\s+[^=]+=\s*['"][^'"]*['"]/i) do |match|
|
|
80
|
-
match.gsub(/=\s*['"][^'"]*['"]/,
|
|
82
|
+
match.gsub(/=\s*['"][^'"]*['"]/, "= ?")
|
|
81
83
|
end
|
|
82
84
|
|
|
83
85
|
# Limit query length to prevent huge payloads
|
|
84
|
-
sql.length > 1000 ? sql[0..1000] + "..." : sql
|
|
86
|
+
(sql.length > 1000) ? sql[0..1000] + "..." : sql
|
|
85
87
|
end
|
|
86
88
|
|
|
87
89
|
def self.safe_query_trace(data, captured_backtrace = nil)
|
|
@@ -89,94 +91,93 @@ module ApmBro
|
|
|
89
91
|
|
|
90
92
|
# Build trace from available data fields
|
|
91
93
|
trace = []
|
|
92
|
-
|
|
94
|
+
|
|
93
95
|
# Use filename, line, and method if available
|
|
94
96
|
if data[:filename] && data[:line] && data[:method]
|
|
95
97
|
trace << "#{data[:filename]}:#{data[:line]}:in `#{data[:method]}'"
|
|
96
98
|
end
|
|
97
|
-
|
|
99
|
+
|
|
98
100
|
# Use the captured backtrace from when the query started (most accurate)
|
|
99
101
|
if captured_backtrace && captured_backtrace.is_a?(Array) && !captured_backtrace.empty?
|
|
100
102
|
# Filter to only include frames that contain "app/" (application code)
|
|
101
103
|
app_frames = captured_backtrace.select do |frame|
|
|
102
|
-
frame.include?(
|
|
104
|
+
frame.include?("app/") && !frame.include?("/vendor/")
|
|
103
105
|
end
|
|
104
|
-
|
|
106
|
+
|
|
105
107
|
caller_trace = app_frames.map do |line|
|
|
106
108
|
# Remove any potential sensitive information from file paths
|
|
107
|
-
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i,
|
|
109
|
+
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i, "/[FILTERED]/")
|
|
108
110
|
end
|
|
109
|
-
|
|
111
|
+
|
|
110
112
|
trace.concat(caller_trace)
|
|
111
113
|
else
|
|
112
114
|
# Fallback: try to get backtrace from current context
|
|
113
115
|
begin
|
|
114
116
|
# Get all available frames - we'll filter to find application code
|
|
115
117
|
all_frames = Thread.current.backtrace || []
|
|
116
|
-
|
|
118
|
+
|
|
117
119
|
if all_frames.empty?
|
|
118
120
|
# Fallback to caller_locations if backtrace is empty
|
|
119
121
|
locations = caller_locations(1, 50)
|
|
120
122
|
all_frames = locations.map { |loc| "#{loc.path}:#{loc.lineno}:in `#{loc.label}'" } if locations
|
|
121
123
|
end
|
|
122
|
-
|
|
124
|
+
|
|
123
125
|
# Filter to only include frames that contain "app/" (application code)
|
|
124
126
|
app_frames = all_frames.select do |frame|
|
|
125
|
-
frame.include?(
|
|
127
|
+
frame.include?("app/") && !frame.include?("/vendor/")
|
|
126
128
|
end
|
|
127
|
-
|
|
129
|
+
|
|
128
130
|
caller_trace = app_frames.map do |line|
|
|
129
|
-
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i,
|
|
131
|
+
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i, "/[FILTERED]/")
|
|
130
132
|
end
|
|
131
|
-
|
|
133
|
+
|
|
132
134
|
trace.concat(caller_trace)
|
|
133
|
-
rescue
|
|
135
|
+
rescue
|
|
134
136
|
# If backtrace fails, try caller as fallback
|
|
135
137
|
begin
|
|
136
138
|
caller_stack = caller(20, 50) # Get more frames to find app/ frames
|
|
137
|
-
app_frames = caller_stack.select { |frame| frame.include?(
|
|
139
|
+
app_frames = caller_stack.select { |frame| frame.include?("app/") && !frame.include?("/vendor/") }
|
|
138
140
|
caller_trace = app_frames.map do |line|
|
|
139
|
-
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i,
|
|
141
|
+
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i, "/[FILTERED]/")
|
|
140
142
|
end
|
|
141
143
|
trace.concat(caller_trace)
|
|
142
|
-
rescue
|
|
144
|
+
rescue
|
|
143
145
|
# If caller also fails, we still have the immediate location
|
|
144
146
|
end
|
|
145
147
|
end
|
|
146
148
|
end
|
|
147
|
-
|
|
149
|
+
|
|
148
150
|
# If we have a backtrace in the data, use it (but it's usually nil for SQL events)
|
|
149
151
|
if data[:backtrace] && data[:backtrace].is_a?(Array)
|
|
150
152
|
# Filter to only include frames that contain "app/"
|
|
151
153
|
app_backtrace = data[:backtrace].select do |line|
|
|
152
|
-
line.is_a?(String) && line.include?(
|
|
154
|
+
line.is_a?(String) && line.include?("app/") && !line.include?("/vendor/")
|
|
153
155
|
end
|
|
154
|
-
|
|
156
|
+
|
|
155
157
|
backtrace_trace = app_backtrace.map do |line|
|
|
156
158
|
case line
|
|
157
159
|
when String
|
|
158
|
-
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i,
|
|
160
|
+
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i, "/[FILTERED]/")
|
|
159
161
|
else
|
|
160
162
|
line.to_s
|
|
161
163
|
end
|
|
162
164
|
end
|
|
163
165
|
trace.concat(backtrace_trace)
|
|
164
166
|
end
|
|
165
|
-
|
|
167
|
+
|
|
166
168
|
# Remove duplicates and return all app/ frames (no limit)
|
|
167
169
|
trace.uniq.map do |line|
|
|
168
170
|
case line
|
|
169
171
|
when String
|
|
170
172
|
# Remove any potential sensitive information from file paths
|
|
171
|
-
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i,
|
|
173
|
+
line.gsub(/\/[^\/]*(password|secret|key|token)[^\/]*\//i, "/[FILTERED]/")
|
|
172
174
|
else
|
|
173
175
|
line.to_s
|
|
174
176
|
end
|
|
175
177
|
end
|
|
176
|
-
rescue
|
|
178
|
+
rescue
|
|
177
179
|
[]
|
|
178
180
|
end
|
|
179
|
-
|
|
180
181
|
end
|
|
181
182
|
end
|
|
182
183
|
|
|
@@ -184,36 +185,36 @@ module ApmBro
|
|
|
184
185
|
# Listener that records GC allocation deltas per SQL event id
|
|
185
186
|
class SqlAllocListener
|
|
186
187
|
def start(name, id, payload)
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
backtrace_map[id] = captured_backtrace[5..-1] || captured_backtrace
|
|
198
|
-
end
|
|
199
|
-
rescue StandardError
|
|
188
|
+
map = (Thread.current[ApmBro::SqlSubscriber::THREAD_LOCAL_ALLOC_START_KEY] ||= {})
|
|
189
|
+
map[id] = GC.stat[:total_allocated_objects] if defined?(GC) && GC.respond_to?(:stat)
|
|
190
|
+
|
|
191
|
+
# Capture the backtrace at query start time (before notification system processes it)
|
|
192
|
+
# This gives us the actual call stack where the SQL was executed
|
|
193
|
+
backtrace_map = (Thread.current[ApmBro::SqlSubscriber::THREAD_LOCAL_BACKTRACE_KEY] ||= {})
|
|
194
|
+
captured_backtrace = Thread.current.backtrace
|
|
195
|
+
if captured_backtrace && captured_backtrace.is_a?(Array)
|
|
196
|
+
# Skip the first few frames (our listener code) to get to the actual query execution
|
|
197
|
+
backtrace_map[id] = captured_backtrace[5..-1] || captured_backtrace
|
|
200
198
|
end
|
|
199
|
+
rescue
|
|
201
200
|
end
|
|
202
201
|
|
|
203
202
|
def finish(name, id, payload)
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
delta = end_count - start_count
|
|
213
|
-
results = (Thread.current[ApmBro::SqlSubscriber::THREAD_LOCAL_ALLOC_RESULTS_KEY] ||= {})
|
|
214
|
-
results[id] = delta
|
|
215
|
-
rescue StandardError
|
|
203
|
+
start_map = Thread.current[ApmBro::SqlSubscriber::THREAD_LOCAL_ALLOC_START_KEY]
|
|
204
|
+
return unless start_map && start_map.key?(id)
|
|
205
|
+
|
|
206
|
+
start_count = start_map.delete(id)
|
|
207
|
+
end_count = begin
|
|
208
|
+
GC.stat[:total_allocated_objects]
|
|
209
|
+
rescue
|
|
210
|
+
nil
|
|
216
211
|
end
|
|
212
|
+
return unless start_count && end_count
|
|
213
|
+
|
|
214
|
+
delta = end_count - start_count
|
|
215
|
+
results = (Thread.current[ApmBro::SqlSubscriber::THREAD_LOCAL_ALLOC_RESULTS_KEY] ||= {})
|
|
216
|
+
results[id] = delta
|
|
217
|
+
rescue
|
|
217
218
|
end
|
|
218
219
|
end
|
|
219
220
|
end
|
|
@@ -12,37 +12,37 @@ module ApmBro
|
|
|
12
12
|
|
|
13
13
|
# Start SQL tracking for this request
|
|
14
14
|
if defined?(ApmBro::SqlSubscriber)
|
|
15
|
-
puts "Starting SQL tracking for request: #{env[
|
|
15
|
+
puts "Starting SQL tracking for request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}"
|
|
16
16
|
ApmBro::SqlSubscriber.start_request_tracking
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
# Start cache tracking for this request
|
|
20
20
|
if defined?(ApmBro::CacheSubscriber)
|
|
21
|
-
puts "Starting cache tracking for request: #{env[
|
|
21
|
+
puts "Starting cache tracking for request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}"
|
|
22
22
|
ApmBro::CacheSubscriber.start_request_tracking
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
# Start Redis tracking for this request
|
|
26
26
|
if defined?(ApmBro::RedisSubscriber)
|
|
27
|
-
puts "Starting redis tracking for request: #{env[
|
|
27
|
+
puts "Starting redis tracking for request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}"
|
|
28
28
|
ApmBro::RedisSubscriber.start_request_tracking
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
# Start view rendering tracking for this request
|
|
32
32
|
if defined?(ApmBro::ViewRenderingSubscriber)
|
|
33
|
-
puts "Starting view rendering tracking for request: #{env[
|
|
33
|
+
puts "Starting view rendering tracking for request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}"
|
|
34
34
|
ApmBro::ViewRenderingSubscriber.start_request_tracking
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
# Start lightweight memory tracking for this request
|
|
38
38
|
if defined?(ApmBro::LightweightMemoryTracker)
|
|
39
|
-
puts "Starting lightweight memory tracking for request: #{env[
|
|
39
|
+
puts "Starting lightweight memory tracking for request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}"
|
|
40
40
|
ApmBro::LightweightMemoryTracker.start_request_tracking
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
# Start detailed memory tracking when allocation tracking is enabled
|
|
44
44
|
if ApmBro.configuration.allocation_tracking_enabled && defined?(ApmBro::MemoryTrackingSubscriber)
|
|
45
|
-
puts "Starting detailed memory tracking for request: #{env[
|
|
45
|
+
puts "Starting detailed memory tracking for request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}"
|
|
46
46
|
ApmBro::MemoryTrackingSubscriber.start_request_tracking
|
|
47
47
|
end
|
|
48
48
|
|
|
@@ -53,27 +53,27 @@ module ApmBro
|
|
|
53
53
|
ensure
|
|
54
54
|
# Clean up thread-local storage
|
|
55
55
|
if defined?(ApmBro::SqlSubscriber)
|
|
56
|
-
|
|
56
|
+
Thread.current[:apm_bro_sql_queries]
|
|
57
57
|
Thread.current[:apm_bro_sql_queries] = nil
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
if defined?(ApmBro::CacheSubscriber)
|
|
61
|
-
|
|
61
|
+
Thread.current[:apm_bro_cache_events]
|
|
62
62
|
Thread.current[:apm_bro_cache_events] = nil
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
if defined?(ApmBro::RedisSubscriber)
|
|
66
|
-
|
|
66
|
+
Thread.current[:apm_bro_redis_events]
|
|
67
67
|
Thread.current[:apm_bro_redis_events] = nil
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
if defined?(ApmBro::ViewRenderingSubscriber)
|
|
71
|
-
|
|
71
|
+
Thread.current[:apm_bro_view_events]
|
|
72
72
|
Thread.current[:apm_bro_view_events] = nil
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
if defined?(ApmBro::LightweightMemoryTracker)
|
|
76
|
-
|
|
76
|
+
Thread.current[:apm_bro_lightweight_memory]
|
|
77
77
|
Thread.current[:apm_bro_lightweight_memory] = nil
|
|
78
78
|
end
|
|
79
79
|
|
data/lib/apm_bro/subscriber.rb
CHANGED
|
@@ -4,7 +4,7 @@ require "active_support/notifications"
|
|
|
4
4
|
|
|
5
5
|
module ApmBro
|
|
6
6
|
class Subscriber
|
|
7
|
-
EVENT_NAME = "process_action.action_controller"
|
|
7
|
+
EVENT_NAME = "process_action.action_controller"
|
|
8
8
|
|
|
9
9
|
def self.subscribe!(client: Client.new)
|
|
10
10
|
ActiveSupport::Notifications.subscribe(EVENT_NAME) do |name, started, finished, _unique_id, data|
|
|
@@ -15,13 +15,13 @@ module ApmBro
|
|
|
15
15
|
if ApmBro.configuration.excluded_controller_action?(controller_name, action_name)
|
|
16
16
|
next
|
|
17
17
|
end
|
|
18
|
-
rescue
|
|
18
|
+
rescue
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
duration_ms = ((finished - started) * 1000.0).round(2)
|
|
22
22
|
# Stop SQL tracking and get collected queries (this was started by the request)
|
|
23
23
|
sql_queries = ApmBro::SqlSubscriber.stop_request_tracking
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
# Stop cache and redis tracking
|
|
26
26
|
cache_events = defined?(ApmBro::CacheSubscriber) ? ApmBro::CacheSubscriber.stop_request_tracking : []
|
|
27
27
|
redis_events = defined?(ApmBro::RedisSubscriber) ? ApmBro::RedisSubscriber.stop_request_tracking : []
|
|
@@ -29,7 +29,7 @@ module ApmBro
|
|
|
29
29
|
# Stop view rendering tracking and get collected view events
|
|
30
30
|
view_events = ApmBro::ViewRenderingSubscriber.stop_request_tracking
|
|
31
31
|
view_performance = ApmBro::ViewRenderingSubscriber.analyze_view_performance(view_events)
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
# Stop memory tracking and get collected memory data
|
|
34
34
|
if ApmBro.configuration.allocation_tracking_enabled && defined?(ApmBro::MemoryTrackingSubscriber)
|
|
35
35
|
detailed_memory = ApmBro::MemoryTrackingSubscriber.stop_request_tracking
|
|
@@ -57,7 +57,7 @@ module ApmBro
|
|
|
57
57
|
duration_seconds: lightweight_memory[:duration_seconds]
|
|
58
58
|
}
|
|
59
59
|
end
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
# Record memory sample for leak detection (only if memory tracking enabled)
|
|
62
62
|
if ApmBro.configuration.memory_tracking_enabled
|
|
63
63
|
ApmBro::MemoryLeakDetector.record_memory_sample({
|
|
@@ -70,7 +70,7 @@ module ApmBro
|
|
|
70
70
|
action: data[:action]
|
|
71
71
|
})
|
|
72
72
|
end
|
|
73
|
-
|
|
73
|
+
|
|
74
74
|
# Report exceptions attached to this action (e.g. controller/view errors)
|
|
75
75
|
if data[:exception] || data[:exception_object]
|
|
76
76
|
begin
|
|
@@ -90,9 +90,8 @@ module ApmBro
|
|
|
90
90
|
host: safe_host,
|
|
91
91
|
params: safe_params(data),
|
|
92
92
|
user_agent: safe_user_agent(data),
|
|
93
|
-
user_email: extract_user_email(data),
|
|
94
93
|
user_id: extract_user_id(data),
|
|
95
|
-
exception_class:
|
|
94
|
+
exception_class: exception_class || exception_obj&.class&.name,
|
|
96
95
|
message: (exception_message || exception_obj&.message).to_s[0, 1000],
|
|
97
96
|
backtrace: backtrace,
|
|
98
97
|
error: true,
|
|
@@ -101,7 +100,7 @@ module ApmBro
|
|
|
101
100
|
|
|
102
101
|
event_name = (exception_class || exception_obj&.class&.name || "exception").to_s
|
|
103
102
|
client.post_metric(event_name: event_name, payload: error_payload)
|
|
104
|
-
rescue
|
|
103
|
+
rescue
|
|
105
104
|
ensure
|
|
106
105
|
next
|
|
107
106
|
end
|
|
@@ -121,13 +120,12 @@ module ApmBro
|
|
|
121
120
|
rails_env: rails_env,
|
|
122
121
|
params: safe_params(data),
|
|
123
122
|
user_agent: safe_user_agent(data),
|
|
124
|
-
user_email: extract_user_email(data),
|
|
125
123
|
user_id: extract_user_id(data),
|
|
126
124
|
memory_usage: memory_usage_mb,
|
|
127
125
|
gc_stats: gc_stats,
|
|
128
126
|
sql_count: sql_count(data),
|
|
129
127
|
sql_queries: sql_queries,
|
|
130
|
-
http_outgoing:
|
|
128
|
+
http_outgoing: Thread.current[:apm_bro_http_events] || [],
|
|
131
129
|
cache_events: cache_events,
|
|
132
130
|
redis_events: redis_events,
|
|
133
131
|
cache_hits: cache_hits(data),
|
|
@@ -145,13 +143,17 @@ module ApmBro
|
|
|
145
143
|
def self.safe_path(data)
|
|
146
144
|
path = data[:path] || (data[:request] && data[:request].path)
|
|
147
145
|
path.to_s
|
|
148
|
-
rescue
|
|
146
|
+
rescue
|
|
149
147
|
""
|
|
150
148
|
end
|
|
151
149
|
|
|
152
150
|
def self.safe_host
|
|
153
151
|
if defined?(Rails) && Rails.respond_to?(:application)
|
|
154
|
-
|
|
152
|
+
begin
|
|
153
|
+
Rails.application.class.module_parent_name
|
|
154
|
+
rescue
|
|
155
|
+
""
|
|
156
|
+
end
|
|
155
157
|
else
|
|
156
158
|
""
|
|
157
159
|
end
|
|
@@ -171,7 +173,7 @@ module ApmBro
|
|
|
171
173
|
params = data[:params]
|
|
172
174
|
begin
|
|
173
175
|
params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
|
|
174
|
-
rescue
|
|
176
|
+
rescue
|
|
175
177
|
end
|
|
176
178
|
|
|
177
179
|
unless params.is_a?(Hash)
|
|
@@ -190,7 +192,7 @@ module ApmBro
|
|
|
190
192
|
|
|
191
193
|
# Truncate deeply to keep payload small and safe
|
|
192
194
|
truncate_value(filtered)
|
|
193
|
-
rescue
|
|
195
|
+
rescue
|
|
194
196
|
{}
|
|
195
197
|
end
|
|
196
198
|
|
|
@@ -198,7 +200,7 @@ module ApmBro
|
|
|
198
200
|
def self.truncate_value(value, max_str: 200, max_array: 20, max_hash_keys: 30)
|
|
199
201
|
case value
|
|
200
202
|
when String
|
|
201
|
-
value.length > max_str ? value[0, max_str] + "…" : value
|
|
203
|
+
(value.length > max_str) ? value[0, max_str] + "…" : value
|
|
202
204
|
when Numeric, TrueClass, FalseClass, NilClass
|
|
203
205
|
value
|
|
204
206
|
when Array
|
|
@@ -209,7 +211,7 @@ module ApmBro
|
|
|
209
211
|
memo[k] = truncate_value(v, max_str: max_str, max_array: max_array, max_hash_keys: max_hash_keys)
|
|
210
212
|
end
|
|
211
213
|
else
|
|
212
|
-
value.to_s.length > max_str ? value.to_s[0, max_str] + "…" : value.to_s
|
|
214
|
+
(value.to_s.length > max_str) ? value.to_s[0, max_str] + "…" : value.to_s
|
|
213
215
|
end
|
|
214
216
|
end
|
|
215
217
|
|
|
@@ -233,7 +235,11 @@ module ApmBro
|
|
|
233
235
|
ua = headers["HTTP_USER_AGENT"] || headers["User-Agent"] || headers["user-agent"]
|
|
234
236
|
return ua.to_s[0..200]
|
|
235
237
|
elsif headers.respond_to?(:to_h)
|
|
236
|
-
h =
|
|
238
|
+
h = begin
|
|
239
|
+
headers.to_h
|
|
240
|
+
rescue
|
|
241
|
+
{}
|
|
242
|
+
end
|
|
237
243
|
ua = h["HTTP_USER_AGENT"] || h["User-Agent"] || h["user-agent"]
|
|
238
244
|
return ua.to_s[0..200]
|
|
239
245
|
end
|
|
@@ -246,22 +252,26 @@ module ApmBro
|
|
|
246
252
|
end
|
|
247
253
|
|
|
248
254
|
""
|
|
249
|
-
rescue
|
|
255
|
+
rescue
|
|
250
256
|
""
|
|
251
257
|
end
|
|
252
|
-
rescue
|
|
258
|
+
rescue
|
|
253
259
|
""
|
|
254
260
|
end
|
|
255
261
|
|
|
256
262
|
def self.memory_usage_mb
|
|
257
263
|
if defined?(GC) && GC.respond_to?(:stat)
|
|
258
264
|
# Get memory usage in MB
|
|
259
|
-
memory_kb =
|
|
265
|
+
memory_kb = begin
|
|
266
|
+
`ps -o rss= -p #{Process.pid}`.to_i
|
|
267
|
+
rescue
|
|
268
|
+
0
|
|
269
|
+
end
|
|
260
270
|
(memory_kb / 1024.0).round(2)
|
|
261
271
|
else
|
|
262
272
|
0
|
|
263
273
|
end
|
|
264
|
-
rescue
|
|
274
|
+
rescue
|
|
265
275
|
0
|
|
266
276
|
end
|
|
267
277
|
|
|
@@ -277,7 +287,7 @@ module ApmBro
|
|
|
277
287
|
else
|
|
278
288
|
{}
|
|
279
289
|
end
|
|
280
|
-
rescue
|
|
290
|
+
rescue
|
|
281
291
|
{}
|
|
282
292
|
end
|
|
283
293
|
|
|
@@ -287,11 +297,15 @@ module ApmBro
|
|
|
287
297
|
data[:sql_count]
|
|
288
298
|
elsif defined?(ActiveRecord) && ActiveRecord::Base.connection
|
|
289
299
|
# Try to get from ActiveRecord connection
|
|
290
|
-
|
|
300
|
+
begin
|
|
301
|
+
ActiveRecord::Base.connection.query_cache.size
|
|
302
|
+
rescue
|
|
303
|
+
0
|
|
304
|
+
end
|
|
291
305
|
else
|
|
292
306
|
0
|
|
293
307
|
end
|
|
294
|
-
rescue
|
|
308
|
+
rescue
|
|
295
309
|
0
|
|
296
310
|
end
|
|
297
311
|
|
|
@@ -299,11 +313,15 @@ module ApmBro
|
|
|
299
313
|
if data[:cache_hits]
|
|
300
314
|
data[:cache_hits]
|
|
301
315
|
elsif defined?(Rails) && Rails.cache.respond_to?(:stats)
|
|
302
|
-
|
|
316
|
+
begin
|
|
317
|
+
Rails.cache.stats[:hits]
|
|
318
|
+
rescue
|
|
319
|
+
0
|
|
320
|
+
end
|
|
303
321
|
else
|
|
304
322
|
0
|
|
305
323
|
end
|
|
306
|
-
rescue
|
|
324
|
+
rescue
|
|
307
325
|
0
|
|
308
326
|
end
|
|
309
327
|
|
|
@@ -311,26 +329,22 @@ module ApmBro
|
|
|
311
329
|
if data[:cache_misses]
|
|
312
330
|
data[:cache_misses]
|
|
313
331
|
elsif defined?(Rails) && Rails.cache.respond_to?(:stats)
|
|
314
|
-
|
|
332
|
+
begin
|
|
333
|
+
Rails.cache.stats[:misses]
|
|
334
|
+
rescue
|
|
335
|
+
0
|
|
336
|
+
end
|
|
315
337
|
else
|
|
316
338
|
0
|
|
317
339
|
end
|
|
318
|
-
rescue
|
|
340
|
+
rescue
|
|
319
341
|
0
|
|
320
342
|
end
|
|
321
343
|
|
|
322
|
-
def self.extract_user_email(data)
|
|
323
|
-
data[:headers].env['warden'].user.email
|
|
324
|
-
rescue StandardError
|
|
325
|
-
nil
|
|
326
|
-
end
|
|
327
|
-
|
|
328
344
|
def self.extract_user_id(data)
|
|
329
|
-
data[:headers].env[
|
|
330
|
-
rescue
|
|
345
|
+
data[:headers].env["warden"].user.id
|
|
346
|
+
rescue
|
|
331
347
|
nil
|
|
332
348
|
end
|
|
333
349
|
end
|
|
334
350
|
end
|
|
335
|
-
|
|
336
|
-
|
data/lib/apm_bro/version.rb
CHANGED