Class: ActiveRecord::SqlAnalyzer::Configuration
- Inherits:
-
Object
- Object
- ActiveRecord::SqlAnalyzer::Configuration
- Defined in:
- lib/active_record/sql_analyzer/configuration.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#add_ambiguous_tracers(list) ⇒ Object
If the first line in the backtrace matches the regex given, we switch to ambiguous tracing mode for that call where we log more of the backtrace.
-
#add_analyzer(result) ⇒ Object
Setup a new analyzer for monitoring tables.
-
#add_backtrace_redactors(list) ⇒ Object
Backtrace redactors filter out data in the backtrace useful if you want to get rid of lines numbers.
-
#add_sql_redactors(list) ⇒ Object
Additional redactors to filter out data in SQL you don’t want logged.
-
#ambiguous_backtrace_lines(lines) ⇒ Object
How many total lines to log when the caller is ambiguous.
-
#backtrace_filter_proc(proc) ⇒ Object
Setup a custom proc that filters out lines before passing them to the loggers.
-
#complex_sql_redactor_proc(proc) ⇒ Object
For hooking in more complicated redactions beyond a simple find/replace.
-
#disable_consolidate_transactions ⇒ Object
Disable transaction consolidation.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#log_sample_proc(proc) ⇒ Object
Set a proc that determines whether or not to log a single event.
-
#logger_root_path(path) ⇒ Object
Root path where all logs go.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
6 7 8 9 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 6 def initialize @options = {} setup_defaults end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 4 def @options end |
Instance Method Details
#[](key) ⇒ Object
126 127 128 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 126 def [](key) @options[key] end |
#add_ambiguous_tracers(list) ⇒ Object
If the first line in the backtrace matches the regex given, we switch to ambiguous tracing mode for that call where we log more of the backtrace.
As an example, if you find you’re only getting middleware, you could use:
%r‘call’z
Which would log up to ambiguous_backtrace_lines (default 3) total lines, rather than the default 1.
99 100 101 102 103 104 105 106 107 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 99 def add_ambiguous_tracers(list) list.each do |row| unless row.is_a?(Regexp) raise ArgumentError, "Tracing filters must be a Regexp to match on" end end @options[:ambiguous_tracers].concat(list) end |
#add_analyzer(result) ⇒ Object
Setup a new analyzer for monitoring tables
add_analyzer(
name: 'users',
tables: %w(users permissions),
logger: ActiveRecord::SqlAnalyzer::RedactedLogger
)
Will setup an analyzer that looks at the tables users and permissions when it finds relevant data, it passes it through the ‘RedactedLogger` class. When calling the proc passed to `log_sample_proc`, it will use the name `users` to help identify it, as well as when logging to disk.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 31 def add_analyzer(result) analyzer = Analyzer.new analyzer.name(result[:name]) analyzer.tables(result[:tables]) analyzer.logger(result[:logger]) analyzer.setup @options[:analyzers] << analyzer analyzer end |
#add_backtrace_redactors(list) ⇒ Object
Backtrace redactors filter out data in the backtrace useful if you want to get rid of lines numbers
86 87 88 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 86 def add_backtrace_redactors(list) @options[:backtrace_redactors].concat(create_redactors(list)) end |
#add_sql_redactors(list) ⇒ Object
Additional redactors to filter out data in SQL you don’t want logged
80 81 82 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 80 def add_sql_redactors(list) @options[:sql_redactors].concat(create_redactors(list)) end |
#ambiguous_backtrace_lines(lines) ⇒ Object
How many total lines to log when the caller is ambiguous
110 111 112 113 114 115 116 117 118 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 110 def ambiguous_backtrace_lines(lines) if !lines.is_a?(Fixnum) raise ArgumentError, "Lines must be a Fixnum" elsif lines <= 1 raise ArgumentError, "Lines cannot be <= 1" end @options[:ambiguous_backtrace_lines] = lines end |
#backtrace_filter_proc(proc) ⇒ Object
Setup a custom proc that filters out lines before passing them to the loggers. By default, this attempts to filter out all non-app code lines.
13 14 15 16 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 13 def backtrace_filter_proc(proc) check_proc(proc, 1, "the backtrace lines") @options[:backtrace_filter_proc] = proc end |
#complex_sql_redactor_proc(proc) ⇒ Object
For hooking in more complicated redactions beyond a simple find/replace.
74 75 76 77 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 74 def complex_sql_redactor_proc(proc) check_proc(proc, 1, "the SQL statement") @options[:sql_redactor_complex_proc] = proc end |
#disable_consolidate_transactions ⇒ Object
Disable transaction consolidation. With transaction consolidation enabled, the logger will log full transactions as single statements.
122 123 124 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 122 def disable_consolidate_transactions @config[:consolidate_transactions] = false end |
#log_sample_proc(proc) ⇒ Object
Set a proc that determines whether or not to log a single event. This must be set to log anything, and controls how many SQL queries you look at.
Proc.new { |name| true }
Will log everything no matter what
Proc.new do |name|
rand(1..100) <= 50
end
Will only log 50% of queries.
You can hook this into something like Redis to allow dynamic control of the ratio without having to redeploy/restart your application.
68 69 70 71 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 68 def log_sample_proc(proc) check_proc(proc, 1, "the analyzer name") @options[:should_log_sample_proc] = proc end |
#logger_root_path(path) ⇒ Object
Root path where all logs go. Defaults to ‘Rails.root.join(’log’)‘
44 45 46 47 48 49 50 |
# File 'lib/active_record/sql_analyzer/configuration.rb', line 44 def logger_root_path(path) unless Dir.exist?(path) raise ArgumentError, "Path '#{path}' is not a directory" end @options[:logger_root_path] = path end |