14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/dead_bro/job_subscriber.rb', line 14
def self.subscribe!(client: Client.new)
ActiveSupport::Notifications.subscribe(JOB_EVENT_NAME) do |name, started, finished, _unique_id, data|
begin
job_class_name = data[:job].class.name
if DeadBro.configuration.excluded_job?(job_class_name)
drain_job_tracking
next
end
unless DeadBro.configuration.exclusive_job?(job_class_name)
drain_job_tracking
next
end
rescue
end
unless DeadBro.configuration.should_sample?
drain_job_tracking
next
end
duration_ms = ((finished - started) * 1000.0).round(2)
unless DeadBro::SqlSubscriber.tracking_active?
DeadBro.logger.clear
Thread.current[DeadBro::TRACKING_START_TIME_KEY] = Time.now
DeadBro::SqlSubscriber.start_request_tracking
if DeadBro.configuration.allocation_tracking_enabled && defined?(DeadBro::MemoryTrackingSubscriber)
DeadBro::MemoryTrackingSubscriber.start_request_tracking
else
DeadBro::LightweightMemoryTracker.start_request_tracking if defined?(DeadBro::LightweightMemoryTracker)
end
end
sql_queries = DeadBro::SqlSubscriber.stop_request_tracking
if DeadBro.configuration.allocation_tracking_enabled && defined?(DeadBro::MemoryTrackingSubscriber)
detailed_memory = DeadBro::MemoryTrackingSubscriber.stop_request_tracking
memory_performance = DeadBro::MemoryTrackingSubscriber.analyze_memory_performance(detailed_memory)
memory_events = {
memory_before: detailed_memory[:memory_before],
memory_after: detailed_memory[:memory_after],
duration_seconds: detailed_memory[:duration_seconds],
allocations_count: (detailed_memory[:allocations] || []).length,
memory_snapshots_count: (detailed_memory[:memory_snapshots] || []).length,
large_objects_count: (detailed_memory[:large_objects] || []).length
}
else
lightweight_memory = DeadBro::LightweightMemoryTracker.stop_request_tracking
memory_events = {
memory_before: lightweight_memory[:memory_before],
memory_after: lightweight_memory[:memory_after]
}
memory_performance = {
memory_growth_mb: lightweight_memory[:memory_growth_mb],
gc_count_increase: lightweight_memory[:gc_count_increase],
heap_pages_increase: lightweight_memory[:heap_pages_increase],
duration_seconds: lightweight_memory[:duration_seconds]
}
end
payload = {
job_class: data[:job].class.name,
job_id: data[:job].job_id,
queue_name: data[:job].queue_name,
arguments: safe_arguments(data[:job].arguments),
duration_ms: duration_ms,
status: "completed",
sql_queries: sql_queries,
rails_env: DeadBro.env,
host: safe_host,
memory_usage: memory_usage_mb,
gc_stats: gc_stats,
memory_events: memory_events,
memory_performance: memory_performance,
logs: DeadBro.logger.logs
}
client.post_metric(event_name: name, payload: payload)
end
ActiveSupport::Notifications.subscribe(JOB_EXCEPTION_EVENT_NAME) do |name, started, finished, _unique_id, data|
begin
job_class_name = data[:job].class.name
if DeadBro.configuration.excluded_job?(job_class_name)
next
end
unless DeadBro.configuration.exclusive_job?(job_class_name)
next
end
rescue
end
duration_ms = ((finished - started) * 1000.0).round(2)
exception = data[:exception_object]
data[:job].class.name
unless DeadBro::SqlSubscriber.tracking_active?
DeadBro.logger.clear
Thread.current[DeadBro::TRACKING_START_TIME_KEY] = Time.now
DeadBro::SqlSubscriber.start_request_tracking
if DeadBro.configuration.allocation_tracking_enabled && defined?(DeadBro::MemoryTrackingSubscriber)
DeadBro::MemoryTrackingSubscriber.start_request_tracking
else
DeadBro::LightweightMemoryTracker.start_request_tracking if defined?(DeadBro::LightweightMemoryTracker)
end
end
sql_queries = DeadBro::SqlSubscriber.stop_request_tracking
if DeadBro.configuration.allocation_tracking_enabled && defined?(DeadBro::MemoryTrackingSubscriber)
detailed_memory = DeadBro::MemoryTrackingSubscriber.stop_request_tracking
memory_performance = DeadBro::MemoryTrackingSubscriber.analyze_memory_performance(detailed_memory)
memory_events = {
memory_before: detailed_memory[:memory_before],
memory_after: detailed_memory[:memory_after],
duration_seconds: detailed_memory[:duration_seconds],
allocations_count: (detailed_memory[:allocations] || []).length,
memory_snapshots_count: (detailed_memory[:memory_snapshots] || []).length,
large_objects_count: (detailed_memory[:large_objects] || []).length
}
else
lightweight_memory = DeadBro::LightweightMemoryTracker.stop_request_tracking
memory_events = {
memory_before: lightweight_memory[:memory_before],
memory_after: lightweight_memory[:memory_after]
}
memory_performance = {
memory_growth_mb: lightweight_memory[:memory_growth_mb],
gc_count_increase: lightweight_memory[:gc_count_increase],
heap_pages_increase: lightweight_memory[:heap_pages_increase],
duration_seconds: lightweight_memory[:duration_seconds]
}
end
payload = {
job_class: data[:job].class.name,
job_id: data[:job].job_id,
queue_name: data[:job].queue_name,
arguments: safe_arguments(data[:job].arguments),
duration_ms: duration_ms,
status: "failed",
sql_queries: sql_queries,
exception_class: exception&.class&.name,
message: exception&.message&.to_s&.[](0, 1000),
backtrace: Array(exception&.backtrace).first(50),
rails_env: DeadBro.env,
host: safe_host,
memory_usage: memory_usage_mb,
gc_stats: gc_stats,
memory_events: memory_events,
memory_performance: memory_performance,
logs: DeadBro.logger.logs
}
event_name = exception&.class&.name || "ActiveJob::Exception"
client.post_metric(event_name: event_name, payload: payload, force: true)
end
rescue
end
|