Module: DeadBro
- Defined in:
- lib/dead_bro.rb,
lib/dead_bro/client.rb,
lib/dead_bro/logger.rb,
lib/dead_bro/monitor.rb,
lib/dead_bro/railtie.rb,
lib/dead_bro/version.rb,
lib/dead_bro/collectors.rb,
lib/dead_bro/dispatcher.rb,
lib/dead_bro/gc_tracker.rb,
lib/dead_bro/subscriber.rb,
lib/dead_bro/configuration.rb,
lib/dead_bro/job_subscriber.rb,
lib/dead_bro/memory_details.rb,
lib/dead_bro/memory_helpers.rb,
lib/dead_bro/sql_subscriber.rb,
lib/dead_bro/sql_subscriber.rb,
lib/dead_bro/circuit_breaker.rb,
lib/dead_bro/collectors/jobs.rb,
lib/dead_bro/cache_subscriber.rb,
lib/dead_bro/error_middleware.rb,
lib/dead_bro/redis_subscriber.rb,
lib/dead_bro/ar_object_tracker.rb,
lib/dead_bro/collectors/system.rb,
lib/dead_bro/collectors/network.rb,
lib/dead_bro/collectors/database.rb,
lib/dead_bro/http_instrumentation.rb,
lib/dead_bro/memory_leak_detector.rb,
lib/dead_bro/collectors/filesystem.rb,
lib/dead_bro/collectors/process_info.rb,
lib/dead_bro/collectors/sample_store.rb,
lib/dead_bro/sql_tracking_middleware.rb,
lib/dead_bro/db_connection_subscriber.rb,
lib/dead_bro/elasticsearch_subscriber.rb,
lib/dead_bro/view_rendering_subscriber.rb,
lib/dead_bro/lightweight_memory_tracker.rb,
lib/dead_bro/memory_tracking_subscriber.rb,
lib/dead_bro/job_sql_tracking_middleware.rb
Defined Under Namespace
Modules: ArObjectTracker, Collectors, DbConnectionSubscriber, GcTracker, HttpInstrumentation, MemoryDetails, MemoryHelpers Classes: AnalysisResult, CacheSubscriber, CircuitBreaker, Client, Configuration, Dispatcher, ElasticsearchSubscriber, Error, ErrorMiddleware, JobSqlTrackingMiddleware, JobSubscriber, LightweightMemoryTracker, Logger, MemoryLeakDetector, MemoryTrackingSubscriber, Monitor, Railtie, RedisSubscriber, SqlAllocListener, SqlSubscriber, SqlTrackingMiddleware, Subscriber, ViewRenderingSubscriber
Constant Summary collapse
- TRACKING_START_TIME_KEY =
Shared constant for tracking start time (used by all subscribers)
:dead_bro_tracking_start_time- MAX_TRACKING_DURATION_SECONDS =
1 hour
3600- VERSION =
"0.2.18"
Class Method Summary collapse
-
.analyze(label = nil, verbose: false) ⇒ Object
Analyze a block of code by tracking its runtime, SQL queries, and memory usage.
-
.client ⇒ Object
Returns a shared Client instance for use across the application.
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.env ⇒ Object
Returns the current environment (Rails.env or ENV fallback).
-
.logger ⇒ Object
Returns the logger instance for storing and retrieving log messages.
-
.monitor ⇒ Object
Returns the monitor instance.
-
.monitor=(monitor) ⇒ Object
Sets the monitor instance.
-
.process_deploy_id ⇒ Object
Returns a process-stable deploy identifier used when none is configured.
- .reset_configuration! ⇒ Object
-
.track(error, **context) ⇒ Object
Manually report a rescued exception to the DeadBro backend.
Class Method Details
.analyze(label = nil, verbose: false) ⇒ Object
Analyze a block of code by tracking its runtime, SQL queries, and memory usage.
Usage:
DeadBro.analyze("load users") do
User.where(active: true).to_a
end
This will print a summary to the console (or Rails logger) including:
-
total time the block took
-
number of SQL queries executed
-
total SQL time
-
breakdown of distinct SQL query patterns (count and total time)
-
memory before/after and delta
-
when detailed memory tracking is enabled, GC and allocation stats
The return value is a DeadBro::AnalysisResult struct. sql_queries is omitted from inspect/to_s but accessible via .sql_queries. Other members:
-
:label
-
:total_time_ms
-
:sql_count
-
:sql_time_ms
-
:sql_queries (array of distinct query patterns with counts and timings)
-
:memory_before_mb
-
:memory_after_mb
-
:memory_delta_mb
-
:memory_details (detailed GC/allocation stats when available)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/dead_bro.rb', line 225 def self.analyze(label = nil, verbose: false) raise ArgumentError, "DeadBro.analyze requires a block" unless block_given? label ||= "block" # Lower Rails log level to DEBUG and enable ActiveRecord verbose_query_logs # so Rails' own SQL logging (including ↳ caller frames) is visible. original_log_level = nil original_verbose_query_logs = nil if verbose begin if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger.respond_to?(:level) original_log_level = Rails.logger.level Rails.logger.level = 0 # Logger::DEBUG end rescue end begin if defined?(ActiveRecord) && ActiveRecord.respond_to?(:verbose_query_logs) original_verbose_query_logs = ActiveRecord.verbose_query_logs ActiveRecord.verbose_query_logs = true end rescue end end # Capture baseline memory stats — config-independent, analyze is debug-only. gc_before = begin; GC.stat; rescue; {}; end memory_before_mb = begin; DeadBro::MemoryHelpers.rss_mb; rescue; 0.0; end object_counts_before = begin defined?(ObjectSpace) && ObjectSpace.respond_to?(:count_objects) ? ObjectSpace.count_objects.dup : {} rescue; {}; end # Local SQL tracking just for this block. # We subscribe directly to ActiveSupport::Notifications instead of relying # on DeadBro's global SqlSubscriber tracking so we don't interfere with or # depend on request/job instrumentation. current_thread = Thread.current local_sql_queries = [] sql_notification_subscription = nil begin if defined?(ActiveSupport) && defined?(ActiveSupport::Notifications) # Ensure SqlSubscriber is loaded so SQL_EVENT_NAME is defined event_name = DeadBro::SqlSubscriber::SQL_EVENT_NAME sql_notification_subscription = ActiveSupport::Notifications.subscribe(event_name) do |_name, started, finished, _id, data| # Only count queries executed on this thread and skip schema queries next unless Thread.current == current_thread next if data[:name] == "SCHEMA" duration_ms = begin ((finished - started) * 1000.0).round(2) rescue 0.0 end raw_sql = data[:sql].to_s # Normalize SQL so identical logical queries group together normalized_sql = begin sql = DeadBro::SqlSubscriber.sanitize_sql(raw_sql) # Collapse whitespace sql = sql.gsub(/\s+/, " ").strip # Normalize numeric literals and quoted strings to '?' sql = sql.gsub(/=\s*\d+(\.\d+)?/i, "= ?") sql = sql.gsub(/=\s*'[^']*'/i, "= ?") sql rescue raw_sql.to_s.strip end query_type = begin raw_sql.strip.split.first.to_s.upcase rescue "SQL" end local_sql_queries << {duration_ms: duration_ms, sql: normalized_sql, query_type: query_type} end end rescue sql_notification_subscription = nil end block_start = Process.clock_gettime(Process::CLOCK_MONOTONIC) error = nil begin yield rescue => e error = e ensure # Restore Rails log level before any output begin if verbose && original_log_level Rails.logger.level = original_log_level end rescue end begin if verbose && !original_verbose_query_logs.nil? ActiveRecord.verbose_query_logs = original_verbose_query_logs end rescue end # Always unsubscribe our local SQL subscriber begin if sql_notification_subscription && defined?(ActiveSupport) && defined?(ActiveSupport::Notifications) ActiveSupport::Notifications.unsubscribe(sql_notification_subscription) end rescue end total_time_ms = begin elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - block_start (elapsed * 1000.0).round(2) rescue 0.0 end # Aggregate SQL metrics from our local subscription sql_count = local_sql_queries.length sql_time_ms = local_sql_queries.sum { |q| (q[:duration_ms] || 0.0).to_f }.round(2) # Group SQL queries by normalized pattern to show frequency and cost query_signatures = Hash.new { |h, k| h[k] = {count: 0, total_time_ms: 0.0, type: nil} } local_sql_queries.each do |q| sig = (q[:sql] || "UNKNOWN").to_s entry = query_signatures[sig] entry[:count] += 1 entry[:total_time_ms] += (q[:duration_ms] || 0.0).to_f entry[:type] ||= q[:query_type] end # Capture post-block memory state — always, regardless of config. gc_after = begin; GC.stat; rescue; {}; end memory_after_mb = begin; DeadBro::MemoryHelpers.rss_mb; rescue; memory_before_mb; end object_counts_after = begin defined?(ObjectSpace) && ObjectSpace.respond_to?(:count_objects) ? ObjectSpace.count_objects.dup : {} rescue; {}; end memory_delta_mb = (memory_after_mb - memory_before_mb).round(2) # Large object scan — full ObjectSpace walk. analyze is debug-only, not hot path. large_objects = begin if defined?(ObjectSpace) && ObjectSpace.respond_to?(:each_object) && ObjectSpace.respond_to?(:memsize_of) found = [] ObjectSpace.each_object do |obj| size = begin; ObjectSpace.memsize_of(obj); rescue; 0; end next unless size > 1_000_000 klass = begin; obj.class.name || "Unknown"; rescue; "Unknown"; end found << {class_name: klass, size_mb: (size / 1_000_000.0).round(2)} break if found.length >= 50 end found.sort_by { |h| -h[:size_mb] } else [] end rescue; []; end detailed_memory_summary = DeadBro::MemoryDetails.build( gc_before: gc_before, gc_after: gc_after, memory_before_mb: memory_before_mb, memory_after_mb: memory_after_mb, object_counts_before: object_counts_before, object_counts_after: object_counts_after, large_objects: large_objects ) # Build structured result hash to return to the caller sql_queries_detail = query_signatures.map do |sig, data| { sql: sig, query_type: data[:type] || "SQL", count: data[:count], total_time_ms: data[:total_time_ms].round(2) } end analysis_result = AnalysisResult.new( label: label, total_time_ms: total_time_ms, sql_count: sql_count, sql_time_ms: sql_time_ms, sql_queries: sql_queries_detail, memory_before_mb: memory_before_mb, memory_after_mb: memory_after_mb, memory_delta_mb: memory_delta_mb, memory_details: detailed_memory_summary, verbose: verbose ) end raise error if error analysis_result end |
.client ⇒ Object
Returns a shared Client instance for use across the application
101 102 103 |
# File 'lib/dead_bro.rb', line 101 def self.client @client ||= Client.new end |
.configuration ⇒ Object
91 92 93 |
# File 'lib/dead_bro.rb', line 91 def self.configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
87 88 89 |
# File 'lib/dead_bro.rb', line 87 def self.configure yield configuration end |
.env ⇒ Object
Returns the current environment (Rails.env or ENV fallback)
120 121 122 123 124 125 126 127 128 |
# File 'lib/dead_bro.rb', line 120 def self.env if defined?(Rails) && Rails.respond_to?(:env) Rails.env else ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development" end rescue "development" end |
.logger ⇒ Object
Returns the logger instance for storing and retrieving log messages
115 116 117 |
# File 'lib/dead_bro.rb', line 115 def self.logger @logger ||= Logger.new end |
.monitor ⇒ Object
Returns the monitor instance
131 132 133 |
# File 'lib/dead_bro.rb', line 131 def self.monitor @monitor end |
.monitor=(monitor) ⇒ Object
Sets the monitor instance
136 137 138 |
# File 'lib/dead_bro.rb', line 136 def self.monitor=(monitor) @monitor = monitor end |
.process_deploy_id ⇒ Object
Returns a process-stable deploy identifier used when none is configured. Memoized per-Ruby process to avoid generating a new UUID per request.
107 108 109 110 111 112 |
# File 'lib/dead_bro.rb', line 107 def self.process_deploy_id @process_deploy_id ||= begin require "securerandom" SecureRandom.uuid end end |
.reset_configuration! ⇒ Object
95 96 97 98 |
# File 'lib/dead_bro.rb', line 95 def self.reset_configuration! @configuration = nil @client = nil end |
.track(error, **context) ⇒ Object
Manually report a rescued exception to the DeadBro backend.
Call this from any rescue block to track an error even when your app handles it gracefully and doesn’t re-raise.
Usage:
begin
charge_card(user)
rescue Stripe::CardError => e
DeadBro.track(e, user_id: current_user.id, plan: "pro")
render json: { error: "Card declined" }, status: 422
end
Any keyword arguments are forwarded as a :context hash in the payload.
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 193 |
# File 'lib/dead_bro.rb', line 154 def self.track(error, **context) return unless error.is_a?(Exception) begin exception_class = error.class.name.to_s event_name = exception_class.empty? ? "exception.tracked" : exception_class payload = { exception_class: exception_class, message: error..to_s[0, 1000], backtrace: Array(error.backtrace).first(50), occurred_at: Process.clock_gettime(Process::CLOCK_REALTIME).to_i, # report time, not raise time tracked: true, rails_env: env, app: begin if defined?(Rails) && Rails.respond_to?(:application) Rails.application.class.module_parent_name else "" end rescue StandardError "" end, pid: Process.pid, logs: begin logger.logs rescue StandardError [] end } payload[:context] = context unless context.empty? client.post_metric(event_name: event_name, payload: payload, force: true) rescue StandardError => _e # Never let APM reporting interfere with the host app end nil end |