peak_flow_utils 0.1.19 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bbb8a48ea294481df8df3c87b5b17eca7c7b2715027a512655f66a318acb8e2e
4
- data.tar.gz: 1970d5b4843db5df6396e12d6cacfa70049af0ac8946f4e7243d3a7f70b5d981
3
+ metadata.gz: '081868f49ad19b34e2f89ed3e5eb2e9c0c2339272ad28e4f321d8c6413a503e7'
4
+ data.tar.gz: 579842b671495755e32aa6c3d9a87fdbaf7cf1882106ef5e827f6aae67604cbb
5
5
  SHA512:
6
- metadata.gz: d1866c116955f38e9f2dc3c2e06fbeca908ed4c0bb425348a82fa8f1522fcfb1e47d1c96fe1a32328bd44f439f26dfddb97285dd0c19b0725f5241dcdb671d51
7
- data.tar.gz: ac9fa1d930c30b5d32d450b10bd2af6ec95ead713b64e686ab07de7cbec5db94e5b4048e0a16e13657d06f1d483a5d4252d1c3d4068e188cfcd4586cc55a1349
6
+ metadata.gz: c8a0320f19c325c9c2332a5f8ea005d7419df8f81e1056dde736eccf14feb7c6a71bda53de9b1826d7273367835ec06e9a825f1afe5495f38758e9a49be415f8
7
+ data.tar.gz: 1332c002f5b338ff04dcabec111e0404741832610f0df4a3eace91bcf85aeedb6b73962dfb69a8a5d84508cd0ddb5ede1d65732836f6578d4bf409862410819b
@@ -0,0 +1,33 @@
1
+ class PeakFlowUtils::ActiveRecordQuery
2
+ class SlowSQLError < RuntimeError; end
3
+
4
+ attr_reader :event
5
+
6
+ def initialize(event)
7
+ @event = event
8
+ end
9
+
10
+ def perform
11
+ report_slow_sql if duration_seconds >= 3
12
+ end
13
+
14
+ def duration_seconds
15
+ @duration_seconds ||= event.duration / 1000
16
+ end
17
+
18
+ def report_slow_sql
19
+ PeakFlowUtils::Notifier.with_parameters(sql: sql, duration_seconds: duration_seconds) do
20
+ raise SlowSQLError, "Slow SQL: #{sql_as_single_line}"
21
+ rescue StandardError => e
22
+ PeakFlowUtils::Notifier.notify(error: e)
23
+ end
24
+ end
25
+
26
+ def sql
27
+ @sql ||= event.payload.fetch(:sql)
28
+ end
29
+
30
+ def sql_as_single_line
31
+ @sql_as_single_line ||= sql.tr("\n", " ")
32
+ end
33
+ end
@@ -2,7 +2,9 @@ require "monitor"
2
2
  require_relative "thread_callbacks_patch"
3
3
 
4
4
  Thread.on_initialize do |parent:, thread:|
5
- thread.instance_variable_set(:@_inherited_local_vars, parent.instance_variable_get(:@_inherited_local_vars))
5
+ parent_vars = parent.instance_variable_get(:@_inherited_local_vars)
6
+ new_cloned_vars = PeakFlowUtils::DeepMerger.execute!(hashes: [parent_vars])
7
+ thread.instance_variable_set(:@_inherited_local_vars, new_cloned_vars)
6
8
  end
7
9
 
8
10
  Thread.class_eval do
@@ -57,8 +59,13 @@ end
57
59
  class PeakFlowUtils::InheritedLocalVar
58
60
  attr_reader :identifier
59
61
 
62
+ def self.identifier_for_object_id(object_id)
63
+ "inherited_local_var_#{object_id}"
64
+ end
65
+
60
66
  def self.finalize(inherited_local_var_object_id)
61
- Thread.inherited_local_vars_delete("inherited_local_var_#{inherited_local_var_object_id}")
67
+ identifier = PeakFlowUtils::InheritedLocalVar.identifier_for_object_id(inherited_local_var_object_id)
68
+ Thread.inherited_local_vars_delete(identifier)
62
69
  rescue Exception => e # rubocop:disable Lint/RescueException
63
70
  puts e.inspect # rubocop:disable Rails/Output
64
71
  puts e.backtrace # rubocop:disable Rails/Output
@@ -68,9 +75,7 @@ class PeakFlowUtils::InheritedLocalVar
68
75
 
69
76
  def initialize(new_value = nil)
70
77
  ObjectSpace.define_finalizer(self, PeakFlowUtils::InheritedLocalVar.method(:finalize))
71
-
72
- @identifier = "inherited_local_var_#{__id__}"
73
-
78
+ @identifier = PeakFlowUtils::InheritedLocalVar.identifier_for_object_id(__id__)
74
79
  Thread.inherited_local_vars_set(identifier => new_value)
75
80
  end
76
81
 
@@ -54,6 +54,7 @@ class PeakFlowUtils::Notifier
54
54
  def initialize(auth_token:)
55
55
  @auth_token = auth_token
56
56
  @mutex = ::Mutex.new
57
+ @on_notify_callbacks = []
57
58
  @parameters = ::PeakFlowUtils::InheritedLocalVar.new({})
58
59
  end
59
60
 
@@ -90,6 +91,10 @@ class PeakFlowUtils::Notifier
90
91
 
91
92
  uri = URI("https://www.peakflow.io/errors/reports")
92
93
 
94
+ @on_notify_callbacks.each do |on_notify_callback|
95
+ on_notify_callback.call(parameters: merged_parameters)
96
+ end
97
+
93
98
  data = {
94
99
  auth_token: auth_token,
95
100
  error: {
@@ -115,6 +120,10 @@ class PeakFlowUtils::Notifier
115
120
  notify(error: e, **opts)
116
121
  end
117
122
 
123
+ def on_notify(&blk)
124
+ @on_notify_callbacks << blk
125
+ end
126
+
118
127
  def send_notify_request(data:, uri:)
119
128
  https = ::Net::HTTP.new(uri.host, uri.port)
120
129
  https.use_ssl = true
@@ -0,0 +1,8 @@
1
+ class PeakFlowUtils::NotifierActiveRecord
2
+ def self.configure
3
+ ActiveSupport::Notifications.subscribe("sql.active_record") do |*args|
4
+ event = ActiveSupport::Notifications::Event.new(*args)
5
+ PeakFlowUtils::ActiveRecordQuery.new(event).perform
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module PeakFlowUtils
2
- VERSION = "0.1.19".freeze
2
+ VERSION = "0.1.20".freeze
3
3
  end
@@ -10,6 +10,7 @@ module PeakFlowUtils
10
10
 
11
11
  autoload :InheritedLocalVar, "#{path}/inherited_local_var"
12
12
  autoload :Notifier, "#{path}/notifier"
13
+ autoload :NotifierActiveRecord, "#{path}/notifier_active_record"
13
14
  autoload :NotifierErrorParser, "#{path}/notifier_error_parser"
14
15
  autoload :NotifierRack, "#{path}/notifier_rack"
15
16
  autoload :NotifierRails, "#{path}/notifier_rails"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peak_flow_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-13 00:00:00.000000000 Z
11
+ date: 2025-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -158,6 +158,7 @@ files:
158
158
  - app/handlers/peak_flow_utils/validations_handler.rb
159
159
  - app/handlers/peak_flow_utils/will_paginate_handler.rb
160
160
  - app/services/peak_flow_utils/active_job_parameters_logging.rb
161
+ - app/services/peak_flow_utils/active_record_query.rb
161
162
  - app/services/peak_flow_utils/application_migration.rb
162
163
  - app/services/peak_flow_utils/application_service.rb
163
164
  - app/services/peak_flow_utils/attribute_service.rb
@@ -193,6 +194,7 @@ files:
193
194
  - lib/peak_flow_utils/models/translation_key.rb
194
195
  - lib/peak_flow_utils/models/translation_value.rb
195
196
  - lib/peak_flow_utils/notifier.rb
197
+ - lib/peak_flow_utils/notifier_active_record.rb
196
198
  - lib/peak_flow_utils/notifier_error_parser.rb
197
199
  - lib/peak_flow_utils/notifier_rack.rb
198
200
  - lib/peak_flow_utils/notifier_rails.rb
@@ -205,7 +207,7 @@ homepage: https://github.com/kaspernj/peak_flow_utils
205
207
  licenses:
206
208
  - MIT
207
209
  metadata: {}
208
- post_install_message:
210
+ post_install_message:
209
211
  rdoc_options: []
210
212
  require_paths:
211
213
  - lib
@@ -220,8 +222,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
222
  - !ruby/object:Gem::Version
221
223
  version: '0'
222
224
  requirements: []
223
- rubygems_version: 3.5.3
224
- signing_key:
225
+ rubygems_version: 3.2.32
226
+ signing_key:
225
227
  specification_version: 4
226
228
  summary: Utilities to be used with PeakFlow.
227
229
  test_files: []