Class: MemHealth::Tracker
- Inherits:
-
Object
- Object
- MemHealth::Tracker
- Defined in:
- lib/mem_health/tracker.rb
Class Method Summary collapse
-
.clear_all_data ⇒ Object
Clear all memory tracking data.
- .items_above_threshold(threshold_mb, type: :web) ⇒ Object
- .jobs_above_threshold(threshold_mb) ⇒ Object
-
.max_memory_diff(type: :web) ⇒ Object
Generic methods for web and worker tracking.
-
.max_memory_diff_worker ⇒ Object
Convenience methods with backward compatibility.
- .max_memory_item(type: :web) ⇒ Object
- .max_memory_job ⇒ Object
- .max_memory_url ⇒ Object
-
.print_stats ⇒ Object
Pretty print stats for console use.
-
.stats ⇒ Object
Get stats summary.
- .top_memory_items(type: :web, limit: config.max_stored_urls) ⇒ Object
- .top_memory_jobs(limit: config.max_stored_urls) ⇒ Object
- .top_memory_urls(limit: config.max_stored_urls) ⇒ Object (also: high_usage_urls)
- .urls_above_threshold(threshold_mb) ⇒ Object
-
.worker_stats ⇒ Object
Get worker stats summary.
Class Method Details
.clear_all_data ⇒ Object
Clear all memory tracking data
66 67 68 69 70 |
# File 'lib/mem_health/tracker.rb', line 66 def clear_all_data redis.del(redis_max_diff_key, redis_max_diff_url_key, redis_high_usage_urls_key, redis_max_diff_worker_key, redis_max_diff_job_key, redis_high_usage_jobs_key) MemHealth::Middleware.reset_data end |
.items_above_threshold(threshold_mb, type: :web) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/mem_health/tracker.rb', line 18 def items_above_threshold(threshold_mb, type: :web) key = type == :worker ? redis_high_usage_jobs_key : redis_high_usage_urls_key redis.zrangebyscore(key, threshold_mb, "+inf", with_scores: true).map do |json_data, score| data = JSON.parse(json_data) data.merge("memory_diff" => score) end end |
.jobs_above_threshold(threshold_mb) ⇒ Object
53 54 55 |
# File 'lib/mem_health/tracker.rb', line 53 def jobs_above_threshold(threshold_mb) items_above_threshold(threshold_mb, type: :worker) end |
.max_memory_diff(type: :web) ⇒ Object
Generic methods for web and worker tracking
5 6 7 8 |
# File 'lib/mem_health/tracker.rb', line 5 def max_memory_diff(type: :web) key = type == :worker ? redis_max_diff_worker_key : redis_max_diff_key redis.get(key)&.to_f || 0.0 end |
.max_memory_diff_worker ⇒ Object
Convenience methods with backward compatibility
35 36 37 |
# File 'lib/mem_health/tracker.rb', line 35 def max_memory_diff_worker max_memory_diff(type: :worker) end |
.max_memory_item(type: :web) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/mem_health/tracker.rb', line 26 def max_memory_item(type: :web) key = type == :worker ? redis_max_diff_job_key : redis_max_diff_url_key json_data = redis.get(key) return nil if json_data.nil? JSON.parse(json_data) end |
.max_memory_job ⇒ Object
61 62 63 |
# File 'lib/mem_health/tracker.rb', line 61 def max_memory_job max_memory_item(type: :worker) end |
.max_memory_url ⇒ Object
57 58 59 |
# File 'lib/mem_health/tracker.rb', line 57 def max_memory_url max_memory_item(type: :web) end |
.print_stats ⇒ Object
Pretty print stats for console use
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mem_health/tracker.rb', line 104 def print_stats stats_data = stats puts "\n=== Memory Usage Stats ===" puts "Max memory difference: #{stats_data[:max_memory_diff]} MB" puts "Stored URLs: #{stats_data[:stored_urls_count]}/#{stats_data[:max_stored_urls]}" puts "Tracking: #{stats_data[:requests_tracked]}" if stats_data[:stored_urls_count] > 0 puts "\nTop 10 memory usage URLs:" top_memory_urls(limit: 10).each_with_index do |url_data, index| if url_data["ram_before"] && url_data["ram_after"] puts "#{index + 1}. #{url_data["memory_diff"]} MB (#{url_data["ram_before"]} → #{url_data["ram_after"]} MB) - #{url_data["url"]} (#{url_data["recorded_at"]})" else puts "#{index + 1}. #{url_data["memory_diff"]} MB - #{url_data["url"]} (#{url_data["recorded_at"]})" end end end end |
.stats ⇒ Object
Get stats summary
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mem_health/tracker.rb', line 73 def stats max_diff = max_memory_diff stored_urls_count = redis.zcard(redis_high_usage_urls_key) tracked_count = MemHealth::Middleware.tracked_requests_count total_count = MemHealth::Middleware.total_requests_count skipped_count = MemHealth::Middleware.skipped_requests_count { max_memory_diff: max_diff, stored_urls_count: stored_urls_count, max_stored_urls: config.max_stored_urls, tracked_requests_count: tracked_count, total_requests_count: total_count, skipped_requests_count: skipped_count, requests_tracked: "#{tracked_count} requests tracked (#{skipped_count} skipped) out of #{total_count} total requests" } end |
.top_memory_items(type: :web, limit: config.max_stored_urls) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/mem_health/tracker.rb', line 10 def top_memory_items(type: :web, limit: config.max_stored_urls) key = type == :worker ? redis_high_usage_jobs_key : redis_high_usage_urls_key redis.zrevrange(key, 0, limit - 1, with_scores: true).map do |json_data, score| data = JSON.parse(json_data) data.merge("memory_diff" => score) end end |
.top_memory_jobs(limit: config.max_stored_urls) ⇒ Object
45 46 47 |
# File 'lib/mem_health/tracker.rb', line 45 def top_memory_jobs(limit: config.max_stored_urls) top_memory_items(type: :worker, limit: limit) end |
.top_memory_urls(limit: config.max_stored_urls) ⇒ Object Also known as: high_usage_urls
39 40 41 |
# File 'lib/mem_health/tracker.rb', line 39 def top_memory_urls(limit: config.max_stored_urls) top_memory_items(type: :web, limit: limit) end |
.urls_above_threshold(threshold_mb) ⇒ Object
49 50 51 |
# File 'lib/mem_health/tracker.rb', line 49 def urls_above_threshold(threshold_mb) items_above_threshold(threshold_mb, type: :web) end |
.worker_stats ⇒ Object
Get worker stats summary
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mem_health/tracker.rb', line 92 def worker_stats max_diff = max_memory_diff_worker stored_jobs_count = redis.zcard(redis_high_usage_jobs_key) { max_memory_diff: max_diff, stored_jobs_count: stored_jobs_count, max_stored_jobs: config.max_stored_urls } end |