|
11 | 11 | # |
12 | 12 | # Data is stored in Redis and never sent externally. |
13 | 13 |
|
14 | | -# Disable the resource monitor logging (CPU/memory/disk every minute) |
15 | | -# The data is still collected for the dashboard, but not logged to stdout |
16 | | -RailsPerformance._resource_monitor_enabled = false unless ENV['RAILS_PERFORMANCE_RESOURCE_MONITOR'] == 'true' |
| 14 | +# Disable the resource monitor logging (CPU/memory/disk every minute) by default |
| 15 | +# This prevents noisy "Server: ..., Context: rails, Role: web, data: ..." logs |
| 16 | +# To enable, set RAILS_PERFORMANCE_RESOURCE_MONITOR=true |
| 17 | +RailsPerformance._resource_monitor_enabled = ENV['RAILS_PERFORMANCE_RESOURCE_MONITOR'] == 'true' |
| 18 | + |
| 19 | +# If the engine already started the monitor, stop it |
| 20 | +if !RailsPerformance._resource_monitor_enabled && RailsPerformance._resource_monitor |
| 21 | + RailsPerformance._resource_monitor.stop_monitoring rescue nil |
| 22 | +end |
17 | 23 |
|
18 | 24 | RailsPerformance.setup do |config| |
19 | 25 | # Redis configuration - uses the same Redis as the rest of the app |
|
25 | 31 | # Default is 4 hours, we keep 7 days for trend analysis |
26 | 32 | config.duration = 168.hours |
27 | 33 |
|
28 | | - # Enable debug mode in development |
29 | | - config.debug = Rails.env.development? |
| 34 | + # Debug mode enables verbose logging (e.g. [SAVE] entries for every record) |
| 35 | + # Controlled via environment variable for easier toggling without code changes |
| 36 | + config.debug = ENV['RAILS_PERFORMANCE_DEBUG'] == 'true' |
30 | 37 |
|
31 | 38 | # Ignore certain paths from tracking |
32 | 39 | config.ignored_endpoints = [ |
|
81 | 88 | # Enable custom events tracking |
82 | 89 | config.include_custom_events = true |
83 | 90 | end |
| 91 | + |
| 92 | +# Fine-grained logging control for Rails Performance |
| 93 | +# This allows us to suppress noisy [SAVE] messages while still seeing other debug info |
| 94 | +module RailsPerformance |
| 95 | + class << self |
| 96 | + def log(message) |
| 97 | + return unless RailsPerformance.debug |
| 98 | + |
| 99 | + # Skip noisy [SAVE] logs unless specifically requested via environment variable |
| 100 | + # These logs occur every time a record is saved to Redis |
| 101 | + if message.include?("[SAVE]") && ENV['RAILS_PERFORMANCE_VERBOSE'] != 'true' |
| 102 | + return |
| 103 | + end |
| 104 | + |
| 105 | + # Prefix the log message for easier filtering/identification |
| 106 | + formatted_message = "[RailsPerformance] #{message}" |
| 107 | + |
| 108 | + if ::Rails.logger |
| 109 | + ::Rails.logger.debug(formatted_message) |
| 110 | + else |
| 111 | + puts(formatted_message) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | +end |
| 116 | + |
| 117 | +# Monkey-patch ResourceMonitor to use our unified logging |
| 118 | +# This ensures "Server: ..., Context: rails..." logs respect the debug setting |
| 119 | +if defined?(RailsPerformance::SystemMonitor::ResourcesMonitor) |
| 120 | + module RailsPerformance |
| 121 | + module SystemMonitor |
| 122 | + class ResourcesMonitor |
| 123 | + def store_data(data) |
| 124 | + # Use our unified log method instead of direct Rails.logger.info |
| 125 | + # This allows us to suppress these logs unless RAILS_PERFORMANCE_DEBUG is true |
| 126 | + RailsPerformance.log("Server: #{server_id}, Context: #{context}, Role: #{role}, data: #{data}") |
| 127 | + |
| 128 | + now = RailsPerformance::Utils.kind_of_now |
| 129 | + now = now.change(sec: 0, usec: 0) |
| 130 | + RailsPerformance::Models::ResourceRecord.new( |
| 131 | + server: server_id, |
| 132 | + context: context, |
| 133 | + role: role, |
| 134 | + datetime: now.strftime(RailsPerformance::FORMAT), |
| 135 | + datetimei: now.to_i, |
| 136 | + json: data |
| 137 | + ).save |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | +end |
0 commit comments