lograge-sql 2.0.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '066994b5ce1417ea75cdcb9328eb91c0deb1a2e4530119a2a698356aaccedb28'
4
- data.tar.gz: 151e948e7fbd209d7048b4c8fe92afa83d1f9012974abc7d2daf24c29caa2da7
3
+ metadata.gz: 6df7e9d756f02019ce93cbac6c0b19a275035a9d0fe74c631aa699eecf149f6f
4
+ data.tar.gz: d34996be2888e4d234d914de95ee092e2b2380181dd70f4b70b2fe84fdc7cc97
5
5
  SHA512:
6
- metadata.gz: 3ade265153ed67c440a4135daf39ffe9479adeb5c89b3780f0f91fabf28f96199be19aa646fe1170c0a67a9505fdfcf4172df2d085d0b8f178fb4994888217f2
7
- data.tar.gz: 1372c45b1944a73f5a209e9d6581856bb16819a90c3ffc6ab5e0e75158e0e48783ec236dd11aa6883af5c0d7731913a5fe3e1dfddf5e665312fe73821a32e804
6
+ metadata.gz: 9ef329876869d54eab7e71bf64345917185dfc2ad06d33cb3649689ddaa51ea7b257a425f5b1fb2c39da57b6b26e051d2a13a2d1687b51e99b8813f39be7e12f
7
+ data.tar.gz: c15d0baabaaede38e6d949fe64d1abf1f14229ac2160b3d8b6a3b70a2ea117e49f0b4c36693f467622d42af862a1dd27d89e3066d44938a4d83bf35633cc170c
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Lograge::Sql
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/lograge-sql.svg)](https://badge.fury.io/rb/lograge-sql)
4
- [![Build Status](https://travis-ci.org/iMacTia/lograge-sql.svg)](https://travis-ci.org/iMacTia/lograge-sql)
4
+ [![CI](https://github.com/iMacTia/lograge-sql/actions/workflows/ci.yml/badge.svg)](https://github.com/iMacTia/lograge-sql/actions/workflows/ci.yml)
5
5
 
6
6
  Lograge::Sql is an extension to the famous [Lograge](https://github.com/roidrage/lograge) gem, which adds SQL queries to the Lograge Event and disable default ActiveRecord logging.
7
7
  This is extremely useful if you're using Lograge together with the ELK stack.
@@ -29,7 +29,23 @@ By default, Lograge::Sql disables default logging on ActiveRecord. To preserve d
29
29
  config.lograge_sql.keep_default_active_record_log = true
30
30
  ```
31
31
 
32
- ## Customization
32
+ ## Configuration
33
+
34
+ ### Minimum query duration threshold
35
+
36
+ By default, `lograge-sql` stores all queries, but you can set a `min_duration_ms` config.
37
+ When you do so, only queries that run for AT LEAST `min_duration_ms` milliseconds will be logged, and all others will be ignored.
38
+ This can be really helpful if you want to detect `Slow SQL queries`.
39
+
40
+ ```ruby
41
+ # config/initializers/lograge.rb
42
+ Rails.application.configure do
43
+ # Defaults is zero
44
+ config.lograge_sql.min_duration_ms = 5000 # milliseconds
45
+ end
46
+ ```
47
+
48
+ ### Output Customization
33
49
 
34
50
  By default, the format is a string concatenation of the query name, the query duration and the query itself joined by `\n` newline:
35
51
 
@@ -66,11 +82,10 @@ Rails.application.configure do
66
82
  end
67
83
  ```
68
84
 
69
- #### Thread-safety
85
+ ### Thread-safety
70
86
 
71
87
  [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`.
72
88
 
73
89
  ## Contributing
74
90
 
75
91
  Bug reports and pull requests are welcome on GitHub at https://github.com/iMacTia/lograge-sql.
76
-
@@ -6,11 +6,26 @@ module Lograge
6
6
  # Every time there's an SQL query, stores it into the Thread.
7
7
  # They'll later be access from the RequestLogSubscriber.
8
8
  def sql(event)
9
- ActiveRecord::LogSubscriber.runtime += event.duration
9
+ increase_runtime_duration(event)
10
10
  return if event.payload[:name] == 'SCHEMA'
11
11
 
12
+ # Only store SQL events if `event.duration` is greater than the configured +min_duration+
13
+ # No need to check if +min_duration+ is present before as it defaults to 0
14
+ return if event.duration.to_f.round(2) < Lograge::Sql.min_duration_ms.to_f
15
+
12
16
  Lograge::Sql.store[:lograge_sql_queries] ||= []
13
17
  Lograge::Sql.store[:lograge_sql_queries] << Lograge::Sql.extract_event.call(event)
14
18
  end
19
+
20
+ private
21
+
22
+ # Add the event duration to the overall ActiveRecord::LogSubscriber.runtime;
23
+ # note we don't do this when `keep_default_active_record_log` is enabled,
24
+ # as ActiveRecord is already adding the duration.
25
+ def increase_runtime_duration(event)
26
+ return if Rails.application.config.lograge_sql.keep_default_active_record_log
27
+
28
+ ActiveRecord::LogSubscriber.runtime += event.duration
29
+ end
15
30
  end
16
31
  end
@@ -3,6 +3,6 @@
3
3
  module Lograge
4
4
  module Sql
5
5
  # Gem version
6
- VERSION = '2.0.0'
6
+ VERSION = '2.2.0'
7
7
  end
8
8
  end
data/lib/lograge/sql.rb CHANGED
@@ -11,18 +11,17 @@ module Lograge
11
11
  attr_accessor :formatter
12
12
  # Extract information from SQL event
13
13
  attr_accessor :extract_event
14
+ # Filter SQL events by duration
15
+ attr_accessor :min_duration_ms
14
16
 
15
17
  # Initialise configuration with fallback to default values
16
18
  def setup(config)
17
- Lograge::Sql.formatter = config.formatter || default_formatter
18
- Lograge::Sql.extract_event = config.extract_event || default_extract_event
19
+ Lograge::Sql.formatter = config.formatter || default_formatter
20
+ Lograge::Sql.extract_event = config.extract_event || default_extract_event
21
+ Lograge::Sql.min_duration_ms = config.min_duration_ms || 0
19
22
 
20
23
  # Disable existing ActiveRecord logging
21
- unless config.keep_default_active_record_log
22
- ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
23
- Lograge.unsubscribe(:active_record, subscriber) if subscriber.is_a?(ActiveRecord::LogSubscriber)
24
- end
25
- end
24
+ unsubscribe_log_subscribers unless config.keep_default_active_record_log
26
25
 
27
26
  return unless defined?(Lograge::ActiveRecordLogSubscriber)
28
27
 
@@ -51,6 +50,12 @@ module Lograge
51
50
  "#{event.payload[:name]} (#{event.duration.to_f.round(2)}) #{event.payload[:sql]}"
52
51
  end
53
52
  end
53
+
54
+ def unsubscribe_log_subscribers
55
+ ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
56
+ Lograge.unsubscribe(:active_record, subscriber) if subscriber.is_a?(ActiveRecord::LogSubscriber)
57
+ end
58
+ end
54
59
  end
55
60
  end
56
61
  end
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.0.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Giuffrida
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-13 00:00:00.000000000 Z
11
+ date: 2023-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.1'
22
+ version: '7.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.1'
32
+ version: '7.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: lograge
35
35
  requirement: !ruby/object:Gem::Requirement