Skip to content

Commit

Permalink
Skip all solid cable queries by default (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdaugs authored Nov 11, 2024
1 parent c20054c commit 0d72e79
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ Rails.application.configure do
end
```

### Filtering sql queries by name

You can filter out queries using the `query_name_denylist` configuration.
This takes an array of regular expressions to match against the query name. If the query name matches any of the regular expressions, it will be ignored. By default, `lograge-sql` ignores queries named `SCHEMA` and queries from the `SolidCable` namespace.
If you are using Solid Cable in your project, be careful when removing this default value as it will cause a [memory leak](https://github.com/iMacTia/lograge-sql/issues/59).

```ruby
# config/initializers/lograge.rb
Rails.application.configure do
# Defaults is [/\ASCHEMA\z/, /\ASolidCable::/]
config.lograge_sql.query_name_denylist << /\AEXACT NAME TO IGNORE\z/
end
```

### Output Customization

By default, the format is a string concatenation of the query name, the query duration and the query itself joined by `\n` newline:
Expand Down
2 changes: 1 addition & 1 deletion lib/lograge/active_record_log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def filter_query(event)
end

def valid?(event)
return false if event.payload[:name] == 'SCHEMA'
return false if event.payload[:name]&.match?(Regexp.union(Lograge::Sql.query_name_denylist))

# Only store SQL events if `event.duration` is greater than the configured +min_duration+
# No need to check if +min_duration+ is present before as it defaults to 0
Expand Down
3 changes: 3 additions & 0 deletions lib/lograge/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ class << self
attr_accessor :min_duration_ms
# Filter SQL query
attr_accessor :query_filter
# Filter wich SQL queries to store
attr_accessor :query_name_denylist

# Initialise configuration with fallback to default values
def setup(config)
Lograge::Sql.formatter = config.formatter || default_formatter
Lograge::Sql.extract_event = config.extract_event || default_extract_event
Lograge::Sql.min_duration_ms = config.min_duration_ms || 0
Lograge::Sql.query_filter = config.query_filter
Lograge::Sql.query_name_denylist = config.query_name_denylist || [/\ASCHEMA\z/, /\ASolidCable::/]

# Disable existing ActiveRecord logging
unsubscribe_log_subscribers unless config.keep_default_active_record_log
Expand Down
40 changes: 40 additions & 0 deletions spec/lograge/active_record_log_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
stub_const('ActiveRecord::RuntimeRegistry', ar_runtime_registry)
Lograge::Sql.extract_event = proc {}
Lograge::Sql.store.clear
Lograge::Sql.query_name_denylist = [/\ASCHEMA\z/, /\ASolidCable::/]
end

context 'with keep_default_active_record_log not set' do
Expand Down Expand Up @@ -75,5 +76,44 @@
expect(query_filter).to have_received(:call).with('foo')
end
end

context 'with default name denylist' do
before do
allow(Rails).to receive_message_chain('application.config.lograge_sql.keep_default_active_record_log') { true }
end

context 'with SCHEMA as name' do
before do
allow(event).to receive(:payload).and_return({ name: 'SCHEMA' })
end

it 'does not store the event' do
log_subscriber.sql(event)
expect(Lograge::Sql.store[:lograge_sql_queries]).to be_nil
end
end

context 'with name starting with SCHEMA' do
before do
allow(event).to receive(:payload).and_return({ name: 'SCHEMA foo' })
end

it 'stores the event' do
log_subscriber.sql(event)
expect(Lograge::Sql.store[:lograge_sql_queries]).not_to be_nil
end
end

context 'with name starting with SolidCable::' do
before do
allow(event).to receive(:payload).and_return({ name: 'SolidCable::Message Maximum' })
end

it 'does not store the event' do
log_subscriber.sql(event)
expect(Lograge::Sql.store[:lograge_sql_queries]).to be_nil
end
end
end
end
end

0 comments on commit 0d72e79

Please sign in to comment.