lograge-sql 1.3.1 → 2.1.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: c5983447366cf1c996e3f4a6571646b4d54298df7e8c54417c02537c23b5132f
4
- data.tar.gz: 48b16b073f9681e809eb3d1bcf3bb0f661ca2ae80a2a2958ef1d9ce495a1147e
3
+ metadata.gz: '0794c5325c6cc647ab9e133b0ac1ebfee558311f12b26861c0776866a6cc27fb'
4
+ data.tar.gz: b2bae9007ad19926f3ba0fed85bb6a965b942e70a537ed50b4230f071834af8f
5
5
  SHA512:
6
- metadata.gz: 1d53aade24130437f98e9bd7d06c448be1364cff17c6dc18c44da939723145a938246c15407fdb8644447d2be31824d34f52cafc0260dc3528b4674cf1a888b9
7
- data.tar.gz: 9a8b3bbaa5abd569373b97ffbda2f1c8b2493be8c35695935587ae7726ac7af35adeb99caa01e2a03e753e26777d077c8f561fed1fa7d357568771094dcd4928
6
+ metadata.gz: 72222b28288cc38a14533c3a3babe408aa2d834dc19ba7f323ac9f7aeca167d55ad7ba2fc3af777e35fa643f7850901378282ee6865bbe4246edc410dff68ad2
7
+ data.tar.gz: f4a956079f4562b3f24737adc851fb78ca41d70b6e8555e3eda42c436b0c88af28a109d3db4e54c2b36c1d0d21ef67a8c344bcb5a5b22b7884d2b9268fbca0cd
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.
@@ -66,6 +66,18 @@ Rails.application.configure do
66
66
  end
67
67
  ```
68
68
 
69
+ `Lograge-sql` only stores any events by `min_duration_ms` condition.
70
+ It very helpful if you want to detect `Slow SQL queries`
71
+
72
+ ```ruby
73
+ # config/initializers/lograge.rb
74
+ Rails.application.configure do
75
+ # Set limitted of SQL duration if you want to filter (unit: milliseconds)
76
+ # Defaults is zero
77
+ config.lograge_sql.min_duration_ms = 5000
78
+ end
79
+ ```
80
+
69
81
  #### Thread-safety
70
82
 
71
83
  [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`.
@@ -73,4 +85,3 @@ end
73
85
  ## Contributing
74
86
 
75
87
  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 = '1.3.1'
6
+ VERSION = '2.1.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: 1.3.1
4
+ version: 2.1.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: 2020-11-16 00:00:00.000000000 Z
11
+ date: 2023-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,34 +16,34 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4'
19
+ version: '5'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.0'
22
+ version: '7.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '4'
29
+ version: '5'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.0'
32
+ version: '7.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: lograge
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0.4'
39
+ version: '0.11'
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '0.4'
46
+ version: '0.11'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -84,14 +84,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
85
  - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: 2.4.0
87
+ version: 2.5.0
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
90
  - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
- rubygems_version: 3.1.4
94
+ rubygems_version: 3.1.6
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: An extension for Lograge to log SQL queries