lograge-sql 2.0.0 → 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: '066994b5ce1417ea75cdcb9328eb91c0deb1a2e4530119a2a698356aaccedb28'
4
- data.tar.gz: 151e948e7fbd209d7048b4c8fe92afa83d1f9012974abc7d2daf24c29caa2da7
3
+ metadata.gz: '0794c5325c6cc647ab9e133b0ac1ebfee558311f12b26861c0776866a6cc27fb'
4
+ data.tar.gz: b2bae9007ad19926f3ba0fed85bb6a965b942e70a537ed50b4230f071834af8f
5
5
  SHA512:
6
- metadata.gz: 3ade265153ed67c440a4135daf39ffe9479adeb5c89b3780f0f91fabf28f96199be19aa646fe1170c0a67a9505fdfcf4172df2d085d0b8f178fb4994888217f2
7
- data.tar.gz: 1372c45b1944a73f5a209e9d6581856bb16819a90c3ffc6ab5e0e75158e0e48783ec236dd11aa6883af5c0d7731913a5fe3e1dfddf5e665312fe73821a32e804
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 = '2.0.0'
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: 2.0.0
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: 2022-01-13 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