pg-locks-monitor 0.1.2 → 0.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: d1cdcf1754671326b02e939ec99c90c2cd8d696bdcbdc82c6c84b378e0365f3b
4
- data.tar.gz: ad862bd65e1db50465ee1456ac18619abeb9f9f10dded487f335348f036b5d1c
3
+ metadata.gz: f95c5c531f0c74143c8fa74417344e1e5b3e400b06f910528541a69ee89f0aaa
4
+ data.tar.gz: fd704b6c9562678541aaaa1cbd98ab997281a529cbfba35f0235466000de0599
5
5
  SHA512:
6
- metadata.gz: f20cb497a1ba37843441cb244fb581ab26ad7181ea1310c4c8d1db75f243fdac3ea05c9ec0b460b1c9bfe23827cb46360885fb24fef8747879d57342d4ad28cd
7
- data.tar.gz: 9e9493e65efff88e07b33c08f8c7886b7b42b45939d07afe904600c7a9036896255756bcb7a7451754482f03bd150feb89db052a4766e8716e9f230832ed4b61
6
+ metadata.gz: 0c467b40b8114d8ca11a5aa89b91e5d0d3637a287d041e8f2d383f0465798d38e8d20f0248408fa4dbce90ef6eb74ef3bb022f78815fb3d676c07272279892b6
7
+ data.tar.gz: 9fdcecb73e646f43a2143db60a12d01f0cc766ed9ee633a63ca1254cce82c7de5688c1594c518b7adf269aed67a77d8da2c4e25a363372beaf19fa9d91b253c5
data/README.md CHANGED
@@ -36,6 +36,9 @@ PgLocksMonitor.configure do |config|
36
36
  config.slack_channel = ""
37
37
 
38
38
  config.notifier_class = PgLocksMonitor::DefaultNotifier
39
+
40
+ config.locks_filter_proc = ->(lock) { true }
41
+ config.blocking_filter_proc = ->(lock) { true }
39
42
  end
40
43
  ```
41
44
 
@@ -49,7 +52,8 @@ end
49
52
  - `slack_webhook_url` - webhook necessary for Slack notification to work
50
53
  - `slack_channel` - the name of the target Slack channel
51
54
  - `notifier_class` - customizable notifier class
52
-
55
+ - `locks_filter_proc` - configurable filter to exclude locks based on any custom logic
56
+ - `blocking_filter_proc` - configurable filter to exclude blocking locks based on any custom logic
53
57
 
54
58
  ## Testing the notification channels
55
59
 
@@ -177,6 +181,36 @@ A background job that schedules itself is not the cleanest pattern. So alternati
177
181
 
178
182
  A recommended frequency of invocation depends on your app's traffic. From my experience, even 1 minute apart snapshots can provide a lot of valuable data, but it all depends on how often the locks are occurring in your Rails application.
179
183
 
184
+ ## Filter procs
185
+
186
+ You can modify `locks_filter_proc` and `blocking_filter_proc` to exclude locks from getting reported. For example, here's how you can report only locks that originated from the Puma server process:
187
+
188
+ `config/initializers/pg_locks_monitor.rb`
189
+ ```ruby
190
+ PgLocksMonitor.configure do |config|
191
+ # ...
192
+
193
+ config.locks_filter_proc = -> (lock) {
194
+ lock.fetch("application").downcase.include?("puma")
195
+ }
196
+ end
197
+ ```
198
+
199
+ or exclude blocking locks which are affecting only the Sidekiq process:
200
+
201
+ `config/initializers/pg_locks_monitor.rb`
202
+ ```ruby
203
+ PgLocksMonitor.configure do |config|
204
+ # ...
205
+
206
+ config.blocking_filter_proc = -> (lock) {
207
+ lock.fetch("blocked_sql_app").downcase.include?("sidekiq")
208
+ }
209
+ end
210
+ ```
211
+
212
+ Please beware that configuring these procs does not overwrite the min duration settings, i.e., `locks_min_duration_ms` and `blocking_min_duration_ms`.
213
+
180
214
  ## Custom notifier class
181
215
 
182
216
  `PgLocksMonitor::DefaultNotifier` supports sending lock notifications with `Rails.logger` or to a Slack channel. If you want to use different notification channels you can define your custom notifier like that:
@@ -6,14 +6,13 @@ require "pg"
6
6
  module PgLocksMonitor
7
7
  def self.snapshot!
8
8
  locks = RailsPgExtras.locks(
9
- in_format: :hash, args: {
10
- limit: configuration.locks_limit,
11
- },
9
+ in_format: :hash,
12
10
  ).select do |lock|
13
11
  if (age = lock.fetch("age"))
14
12
  (ActiveSupport::Duration.parse(age).to_f * 1000) > configuration.locks_min_duration_ms
15
13
  end
16
- end
14
+ end.select(&configuration.locks_filter_proc)
15
+ .first(configuration.locks_limit)
17
16
 
18
17
  if locks.present? && configuration.monitor_locks
19
18
  configuration.notifier_class.call(locks)
@@ -21,7 +20,8 @@ module PgLocksMonitor
21
20
 
22
21
  blocking = RailsPgExtras.blocking(in_format: :hash).select do |block|
23
22
  (ActiveSupport::Duration.parse(block.fetch("blocking_duration")).to_f * 1000) > configuration.blocking_min_duration_ms
24
- end
23
+ end.select(&configuration.blocking_filter_proc)
24
+ .first(configuration.locks_limit)
25
25
 
26
26
  if blocking.present? && configuration.monitor_blocking
27
27
  configuration.notifier_class.call(blocking)
@@ -13,6 +13,8 @@ module PgLocksMonitor
13
13
  slack_webhook_url: nil,
14
14
  slack_channel: nil,
15
15
  notifier_class: PgLocksMonitor::DefaultNotifier,
16
+ locks_filter_proc: ->(lock) { true },
17
+ blocking_filter_proc: ->(lock) { true },
16
18
  }
17
19
 
18
20
  attr_accessor *DEFAULT.keys
@@ -43,6 +45,9 @@ PgLocksMonitor.configure do |config|
43
45
  config.slack_channel = "#{DEFAULT[:slack_channel]}"
44
46
 
45
47
  config.notifier_class = #{DEFAULT[:notifier_class]}
48
+
49
+ config.locks_filter_proc = ->(lock) { true }
50
+ config.blocking_filter_proc = ->(lock) { true }
46
51
  end
47
52
  CONFIG
48
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgLocksMonitor
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-locks-monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb