peak_flow_utils 0.1.18 → 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 +4 -4
- data/app/services/peak_flow_utils/active_record_query.rb +33 -0
- data/app/services/peak_flow_utils/parse_json.rb +39 -0
- data/lib/peak_flow_utils/inherited_local_var.rb +10 -5
- data/lib/peak_flow_utils/notifier.rb +10 -1
- data/lib/peak_flow_utils/notifier_active_record.rb +8 -0
- data/lib/peak_flow_utils/version.rb +1 -1
- data/lib/peak_flow_utils.rb +2 -0
- metadata +9 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '081868f49ad19b34e2f89ed3e5eb2e9c0c2339272ad28e4f321d8c6413a503e7'
|
4
|
+
data.tar.gz: 579842b671495755e32aa6c3d9a87fdbaf7cf1882106ef5e827f6aae67604cbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class PeakFlowUtils::ParseJson
|
2
|
+
def initialize(object)
|
3
|
+
@object = object
|
4
|
+
end
|
5
|
+
|
6
|
+
def parse
|
7
|
+
parse_to_json(@object)
|
8
|
+
end
|
9
|
+
|
10
|
+
def active_record?(object)
|
11
|
+
object.class.ancestors.any? do |ancestor|
|
12
|
+
ancestor.name == "ActiveRecord::Base"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse_to_json(object)
|
17
|
+
if object.is_a?(Hash)
|
18
|
+
result = {}
|
19
|
+
|
20
|
+
object.each do |key, value|
|
21
|
+
result[key] = parse_to_json(value)
|
22
|
+
end
|
23
|
+
|
24
|
+
return result
|
25
|
+
elsif object.is_a?(Array)
|
26
|
+
return object.map do |value|
|
27
|
+
parse_to_json(value)
|
28
|
+
end
|
29
|
+
elsif active_record?(object)
|
30
|
+
result = "#<#{object.class.name} id: #{object.id}"
|
31
|
+
result << " name: \"#{object.name}\"" if object.respond_to?(:name)
|
32
|
+
result << ">"
|
33
|
+
|
34
|
+
return result
|
35
|
+
end
|
36
|
+
|
37
|
+
object
|
38
|
+
end
|
39
|
+
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
|
-
|
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
|
-
|
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: {
|
@@ -106,7 +111,7 @@ class PeakFlowUtils::Notifier
|
|
106
111
|
}
|
107
112
|
}
|
108
113
|
|
109
|
-
send_notify_request(data: data, uri: uri)
|
114
|
+
send_notify_request(data: PeakFlowUtils::ParseJson.new(data).parse, uri: uri)
|
110
115
|
end
|
111
116
|
|
112
117
|
def notify_message(message, **opts)
|
@@ -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
|
data/lib/peak_flow_utils.rb
CHANGED
@@ -10,12 +10,14 @@ 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"
|
16
17
|
autoload :NotifierResponse, "#{path}/notifier_response"
|
17
18
|
autoload :NotifierSidekiq, "#{path}/notifier_sidekiq"
|
18
19
|
autoload :HandlerHelper, "#{path}/handler_helper"
|
20
|
+
autoload :ParseJson, "#{path}/parse_json"
|
19
21
|
|
20
22
|
autoload :ApplicationRecord, "#{models_path}/application_record"
|
21
23
|
autoload :Group, "#{models_path}/group"
|
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.
|
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:
|
11
|
+
date: 2025-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -138,7 +138,7 @@ dependencies:
|
|
138
138
|
version: '0'
|
139
139
|
description: Utilities to be used with PeakFlow.
|
140
140
|
email:
|
141
|
-
-
|
141
|
+
- k@spernj.org
|
142
142
|
executables: []
|
143
143
|
extensions: []
|
144
144
|
extra_rdoc_files: []
|
@@ -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
|
@@ -170,6 +171,7 @@ files:
|
|
170
171
|
- app/services/peak_flow_utils/group_service.rb
|
171
172
|
- app/services/peak_flow_utils/handlers_finder_service.rb
|
172
173
|
- app/services/peak_flow_utils/model_inspector.rb
|
174
|
+
- app/services/peak_flow_utils/parse_json.rb
|
173
175
|
- app/services/peak_flow_utils/sidekiq_parameters_logging.rb
|
174
176
|
- app/services/peak_flow_utils/translation_service.rb
|
175
177
|
- app/services/peak_flow_utils/translations_parser_service.rb
|
@@ -192,6 +194,7 @@ files:
|
|
192
194
|
- lib/peak_flow_utils/models/translation_key.rb
|
193
195
|
- lib/peak_flow_utils/models/translation_value.rb
|
194
196
|
- lib/peak_flow_utils/notifier.rb
|
197
|
+
- lib/peak_flow_utils/notifier_active_record.rb
|
195
198
|
- lib/peak_flow_utils/notifier_error_parser.rb
|
196
199
|
- lib/peak_flow_utils/notifier_rack.rb
|
197
200
|
- lib/peak_flow_utils/notifier_rails.rb
|
@@ -204,7 +207,7 @@ homepage: https://github.com/kaspernj/peak_flow_utils
|
|
204
207
|
licenses:
|
205
208
|
- MIT
|
206
209
|
metadata: {}
|
207
|
-
post_install_message:
|
210
|
+
post_install_message:
|
208
211
|
rdoc_options: []
|
209
212
|
require_paths:
|
210
213
|
- lib
|
@@ -220,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
220
223
|
version: '0'
|
221
224
|
requirements: []
|
222
225
|
rubygems_version: 3.2.32
|
223
|
-
signing_key:
|
226
|
+
signing_key:
|
224
227
|
specification_version: 4
|
225
228
|
summary: Utilities to be used with PeakFlow.
|
226
229
|
test_files: []
|