lograge-sql 2.2.0 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/lib/lograge/active_record_log_subscriber.rb +28 -10
- data/lib/lograge/sql/version.rb +1 -1
- data/lib/lograge/sql.rb +3 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d435c21129588de106963c13febc8cd259660430a9bfdb95894cd626bd658f6c
|
4
|
+
data.tar.gz: 5f942aaff99e144669f4b0b1c7d280fd11a800ab8f7f5ffc37197d891ae5c6b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 628036a6a1f5a00e27a67b6c056d883dc183a58ffddb01bf63f717790f77182221301dc7582cf570dd718efa6d73993cb1dec04c3a6ecf3b8b453a06a25d5061
|
7
|
+
data.tar.gz: 405f2fe165ffe7ddbe80816e9ca3b980886d25331986d8b9b0084ffb2b9b656c6f8327d0f03aac930e099333b2c1291f0e5c1f7d2ef1b3b5f8def06260e7f3c5
|
data/README.md
CHANGED
@@ -82,6 +82,15 @@ Rails.application.configure do
|
|
82
82
|
end
|
83
83
|
```
|
84
84
|
|
85
|
+
### Filtering out sensitive info in SQL logs
|
86
|
+
By default, `lograge-sql` will log full query but if you have sensitive data that need to be filtered out, you can set `query_filter` config:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
Rails.application.configure do
|
90
|
+
config.lograge_sql.query_filter = ->(query) { query.gsub(/custom_regexp/, "[FILTERED]".freeze) }
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
85
94
|
### Thread-safety
|
86
95
|
|
87
96
|
[Depending on the web server in your project](https://github.com/steveklabnik/request_store#the-problem) you might benefit from improved thread-safety by adding [`request_store`](https://github.com/steveklabnik/request_store) to your Gemfile. It will be automatically picked up by `lograge-sql`.
|
@@ -4,28 +4,46 @@ module Lograge
|
|
4
4
|
# Log subscriber to replace ActiveRecord's default one
|
5
5
|
class ActiveRecordLogSubscriber < ActiveSupport::LogSubscriber
|
6
6
|
# Every time there's an SQL query, stores it into the Thread.
|
7
|
-
# They'll later be
|
7
|
+
# They'll later be accessed from the RequestLogSubscriber.
|
8
8
|
def sql(event)
|
9
9
|
increase_runtime_duration(event)
|
10
|
-
return
|
10
|
+
return unless valid?(event)
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
return if event.duration.to_f.round(2) < Lograge::Sql.min_duration_ms.to_f
|
15
|
-
|
16
|
-
Lograge::Sql.store[:lograge_sql_queries] ||= []
|
17
|
-
Lograge::Sql.store[:lograge_sql_queries] << Lograge::Sql.extract_event.call(event)
|
12
|
+
filter_query(event)
|
13
|
+
store(event)
|
18
14
|
end
|
19
15
|
|
20
16
|
private
|
21
17
|
|
22
|
-
# Add the event duration to the overall ActiveRecord::
|
18
|
+
# Add the event duration to the overall ActiveRecord::RuntimeRegistry.sql_runtime;
|
23
19
|
# note we don't do this when `keep_default_active_record_log` is enabled,
|
24
20
|
# as ActiveRecord is already adding the duration.
|
25
21
|
def increase_runtime_duration(event)
|
26
22
|
return if Rails.application.config.lograge_sql.keep_default_active_record_log
|
27
23
|
|
28
|
-
ActiveRecord::
|
24
|
+
ActiveRecord::RuntimeRegistry.sql_runtime += event.duration
|
25
|
+
end
|
26
|
+
|
27
|
+
def filter_query(event)
|
28
|
+
return unless Lograge::Sql.query_filter
|
29
|
+
|
30
|
+
# Filtering out sensitive info in SQL query if custom query_filter is provided
|
31
|
+
event.payload[:sql] = Lograge::Sql.query_filter.call(event.payload[:sql])
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid?(event)
|
35
|
+
return false if event.payload[:name] == 'SCHEMA'
|
36
|
+
|
37
|
+
# Only store SQL events if `event.duration` is greater than the configured +min_duration+
|
38
|
+
# No need to check if +min_duration+ is present before as it defaults to 0
|
39
|
+
return false if event.duration.to_f.round(2) < Lograge::Sql.min_duration_ms.to_f
|
40
|
+
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
def store(event)
|
45
|
+
Lograge::Sql.store[:lograge_sql_queries] ||= []
|
46
|
+
Lograge::Sql.store[:lograge_sql_queries] << Lograge::Sql.extract_event.call(event)
|
29
47
|
end
|
30
48
|
end
|
31
49
|
end
|
data/lib/lograge/sql/version.rb
CHANGED
data/lib/lograge/sql.rb
CHANGED
@@ -13,12 +13,15 @@ module Lograge
|
|
13
13
|
attr_accessor :extract_event
|
14
14
|
# Filter SQL events by duration
|
15
15
|
attr_accessor :min_duration_ms
|
16
|
+
# Filter SQL query
|
17
|
+
attr_accessor :query_filter
|
16
18
|
|
17
19
|
# Initialise configuration with fallback to default values
|
18
20
|
def setup(config)
|
19
21
|
Lograge::Sql.formatter = config.formatter || default_formatter
|
20
22
|
Lograge::Sql.extract_event = config.extract_event || default_extract_event
|
21
23
|
Lograge::Sql.min_duration_ms = config.min_duration_ms || 0
|
24
|
+
Lograge::Sql.query_filter = config.query_filter
|
22
25
|
|
23
26
|
# Disable existing ActiveRecord logging
|
24
27
|
unsubscribe_log_subscribers unless config.keep_default_active_record_log
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lograge-sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Giuffrida
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|