deprecation_collector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7936a3e78d550c3940f0abe364171df3b39cc1e19931cfba23b8cfdb060122ca
4
+ data.tar.gz: 2a4d06a0baee48c61c027c7791fef7894b5af5d1791fe0e3c1bd3afaf2fa61ab
5
+ SHA512:
6
+ metadata.gz: c5a10c07173fe8b73003630c2fa0839b07c62f0471b72269aabdcece19935f6693fc6b0a3a3c8634b1731b7cc9db0aae186ff2067a43047692e15f32180d5210
7
+ data.tar.gz: b6e598c579eb46756b9a78336fd3b223d4224b01f5a677b55375732279564c9b609dc2e87083a6cb47cb5e86c567c73102465080e275818564d94c1d29b29408
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+
2
+
3
+ == 0.0.1
4
+
5
+ Initial release
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in deprecation_collector.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+ gem "timecop"
12
+
13
+ gem "rubocop", "~> 1.21"
14
+
15
+ # TODO: appraisals
16
+ gem "rails", '6.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,184 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ deprecation_collector (0.0.1)
5
+ redis (>= 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.0.0)
11
+ actionpack (= 6.0.0)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (6.0.0)
15
+ actionpack (= 6.0.0)
16
+ activejob (= 6.0.0)
17
+ activerecord (= 6.0.0)
18
+ activestorage (= 6.0.0)
19
+ activesupport (= 6.0.0)
20
+ mail (>= 2.7.1)
21
+ actionmailer (6.0.0)
22
+ actionpack (= 6.0.0)
23
+ actionview (= 6.0.0)
24
+ activejob (= 6.0.0)
25
+ mail (~> 2.5, >= 2.5.4)
26
+ rails-dom-testing (~> 2.0)
27
+ actionpack (6.0.0)
28
+ actionview (= 6.0.0)
29
+ activesupport (= 6.0.0)
30
+ rack (~> 2.0)
31
+ rack-test (>= 0.6.3)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
34
+ actiontext (6.0.0)
35
+ actionpack (= 6.0.0)
36
+ activerecord (= 6.0.0)
37
+ activestorage (= 6.0.0)
38
+ activesupport (= 6.0.0)
39
+ nokogiri (>= 1.8.5)
40
+ actionview (6.0.0)
41
+ activesupport (= 6.0.0)
42
+ builder (~> 3.1)
43
+ erubi (~> 1.4)
44
+ rails-dom-testing (~> 2.0)
45
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
+ activejob (6.0.0)
47
+ activesupport (= 6.0.0)
48
+ globalid (>= 0.3.6)
49
+ activemodel (6.0.0)
50
+ activesupport (= 6.0.0)
51
+ activerecord (6.0.0)
52
+ activemodel (= 6.0.0)
53
+ activesupport (= 6.0.0)
54
+ activestorage (6.0.0)
55
+ actionpack (= 6.0.0)
56
+ activejob (= 6.0.0)
57
+ activerecord (= 6.0.0)
58
+ marcel (~> 0.3.1)
59
+ activesupport (6.0.0)
60
+ concurrent-ruby (~> 1.0, >= 1.0.2)
61
+ i18n (>= 0.7, < 2)
62
+ minitest (~> 5.1)
63
+ tzinfo (~> 1.1)
64
+ zeitwerk (~> 2.1, >= 2.1.8)
65
+ ast (2.4.2)
66
+ builder (3.2.4)
67
+ concurrent-ruby (1.1.10)
68
+ crass (1.0.6)
69
+ diff-lcs (1.5.0)
70
+ erubi (1.10.0)
71
+ globalid (1.0.0)
72
+ activesupport (>= 5.0)
73
+ i18n (1.10.0)
74
+ concurrent-ruby (~> 1.0)
75
+ loofah (2.18.0)
76
+ crass (~> 1.0.2)
77
+ nokogiri (>= 1.5.9)
78
+ mail (2.7.1)
79
+ mini_mime (>= 0.1.1)
80
+ marcel (0.3.3)
81
+ mimemagic (~> 0.3.2)
82
+ method_source (1.0.0)
83
+ mimemagic (0.3.10)
84
+ nokogiri (~> 1)
85
+ rake
86
+ mini_mime (1.1.2)
87
+ minitest (5.15.0)
88
+ nio4r (2.5.8)
89
+ nokogiri (1.13.6-x86_64-darwin)
90
+ racc (~> 1.4)
91
+ parallel (1.22.1)
92
+ parser (3.1.2.0)
93
+ ast (~> 2.4.1)
94
+ racc (1.6.0)
95
+ rack (2.2.3.1)
96
+ rack-test (1.1.0)
97
+ rack (>= 1.0, < 3)
98
+ rails (6.0.0)
99
+ actioncable (= 6.0.0)
100
+ actionmailbox (= 6.0.0)
101
+ actionmailer (= 6.0.0)
102
+ actionpack (= 6.0.0)
103
+ actiontext (= 6.0.0)
104
+ actionview (= 6.0.0)
105
+ activejob (= 6.0.0)
106
+ activemodel (= 6.0.0)
107
+ activerecord (= 6.0.0)
108
+ activestorage (= 6.0.0)
109
+ activesupport (= 6.0.0)
110
+ bundler (>= 1.3.0)
111
+ railties (= 6.0.0)
112
+ sprockets-rails (>= 2.0.0)
113
+ rails-dom-testing (2.0.3)
114
+ activesupport (>= 4.2.0)
115
+ nokogiri (>= 1.6)
116
+ rails-html-sanitizer (1.4.2)
117
+ loofah (~> 2.3)
118
+ railties (6.0.0)
119
+ actionpack (= 6.0.0)
120
+ activesupport (= 6.0.0)
121
+ method_source
122
+ rake (>= 0.8.7)
123
+ thor (>= 0.20.3, < 2.0)
124
+ rainbow (3.1.1)
125
+ rake (13.0.6)
126
+ redis (4.6.0)
127
+ regexp_parser (2.5.0)
128
+ rexml (3.2.5)
129
+ rspec (3.11.0)
130
+ rspec-core (~> 3.11.0)
131
+ rspec-expectations (~> 3.11.0)
132
+ rspec-mocks (~> 3.11.0)
133
+ rspec-core (3.11.0)
134
+ rspec-support (~> 3.11.0)
135
+ rspec-expectations (3.11.0)
136
+ diff-lcs (>= 1.2.0, < 2.0)
137
+ rspec-support (~> 3.11.0)
138
+ rspec-mocks (3.11.1)
139
+ diff-lcs (>= 1.2.0, < 2.0)
140
+ rspec-support (~> 3.11.0)
141
+ rspec-support (3.11.0)
142
+ rubocop (1.30.0)
143
+ parallel (~> 1.10)
144
+ parser (>= 3.1.0.0)
145
+ rainbow (>= 2.2.2, < 4.0)
146
+ regexp_parser (>= 1.8, < 3.0)
147
+ rexml (>= 3.2.5, < 4.0)
148
+ rubocop-ast (>= 1.18.0, < 2.0)
149
+ ruby-progressbar (~> 1.7)
150
+ unicode-display_width (>= 1.4.0, < 3.0)
151
+ rubocop-ast (1.18.0)
152
+ parser (>= 3.1.1.0)
153
+ ruby-progressbar (1.11.0)
154
+ sprockets (4.0.3)
155
+ concurrent-ruby (~> 1.0)
156
+ rack (> 1, < 3)
157
+ sprockets-rails (3.4.2)
158
+ actionpack (>= 5.2)
159
+ activesupport (>= 5.2)
160
+ sprockets (>= 3.0.0)
161
+ thor (1.2.1)
162
+ thread_safe (0.3.6)
163
+ timecop (0.9.5)
164
+ tzinfo (1.2.9)
165
+ thread_safe (~> 0.1)
166
+ unicode-display_width (2.1.0)
167
+ websocket-driver (0.7.5)
168
+ websocket-extensions (>= 0.1.0)
169
+ websocket-extensions (0.1.5)
170
+ zeitwerk (2.5.4)
171
+
172
+ PLATFORMS
173
+ x86_64-darwin-21
174
+
175
+ DEPENDENCIES
176
+ deprecation_collector!
177
+ rails (= 6.0)
178
+ rake (~> 13.0)
179
+ rspec (~> 3.0)
180
+ rubocop (~> 1.21)
181
+ timecop
182
+
183
+ BUNDLED WITH
184
+ 2.3.10
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Vasily Fedoseyev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Vasily Fedoseyev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # DeprecationCollector
2
+
3
+ Collects ruby and rails deprecation warnings.
4
+ (gem is a work-in-process, documentation will come later)
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add deprecation_collector
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install deprecation_collector
15
+
16
+ ## Usage
17
+
18
+
19
+
20
+ ## Development
21
+
22
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
23
+
24
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
25
+
26
+ ## Contributing
27
+
28
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/deprecation_collector.
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/deprecation_collector/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "deprecation_collector"
7
+ spec.version = DeprecationCollector::VERSION
8
+ spec.authors = ["Vasily Fedoseyev"]
9
+ spec.email = ["vasilyfedoseyev@gmail.com"]
10
+
11
+ spec.summary = "Collector for ruby/rails deprecations and warnings, suitable for production"
12
+ spec.description = "Collects and aggregates warnings and deprecations. Optimized for production environment."
13
+ spec.homepage = "https://github.com/Vasfed/deprecation_collector"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/Vasfed/deprecation_collector"
19
+ spec.metadata["changelog_uri"] = "https://github.com/Vasfed/deprecation_collector/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "redis", ">= 2.0" # TODO: check exact minimum version
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeprecationCollector
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,395 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "deprecation_collector/version"
4
+ require 'time'
5
+ require 'redis'
6
+
7
+ class DeprecationCollector
8
+ # NB: in production with hugreds of workers may easily overload redis with writes, so more delay needed:
9
+ FLUSH_INTERVAL = (::Rails.env.production? && 15.minutes) || 1.minute
10
+
11
+ @instance_mutex = Mutex.new
12
+ @installed = false
13
+ private_class_method :new
14
+
15
+ def self.instance
16
+ return @instance if defined?(@instance) && @instance
17
+
18
+ @instance_mutex.synchronize do
19
+ # переиспользовать мутекс не обязательно, но он используется ровно один раз
20
+ @instance ||= new($redis, mutex: @instance_mutex)
21
+ end
22
+ @instance
23
+ end
24
+
25
+ def self.collect(message, backtrace, realm)
26
+ instance.collect(message, backtrace, realm)
27
+ end
28
+
29
+ # inside dev env may be called multiple times
30
+ def self.install
31
+ instance # to make it created, configuration comes later
32
+
33
+ @instance_mutex.synchronize do
34
+ unless @installed
35
+ at_exit { instance.write_to_redis(force: true) }
36
+ @installed = true
37
+ end
38
+
39
+ yield instance if block_given?
40
+
41
+ # TODO: a more polite hook
42
+ ActiveSupport::Deprecation.behavior = lambda do |message, callstack, deprecation_horizon, gem_name|
43
+ # not polite to turn off all other possible behaviors, but otherwise may get duplicate calls
44
+ DeprecationCollector.collect(message, callstack, :rails)
45
+ ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[Rails.application&.config&.active_support&.deprecation || :log].call(
46
+ message, callstack, deprecation_horizon, gem_name
47
+ )
48
+ end
49
+
50
+ Kernel.class_eval do
51
+ # module is included in others thus prepend does not work
52
+ remove_method :warn
53
+ class << self
54
+ remove_method :warn
55
+ end
56
+ module_function(define_method(:warn) do |*messages, **kwargs|
57
+ KernelWarningCollector.warn(*messages, backtrace: caller, **kwargs)
58
+ end)
59
+ end
60
+
61
+ Warning.singleton_class.prepend(DeprecationCollector::WarningCollector)
62
+ Warning[:deprecated] = true if Warning.respond_to?(:[]=) # turn on ruby 2.7 deprecations
63
+ end
64
+
65
+ @instance
66
+ end
67
+
68
+ module MultipartWarningJoiner
69
+ module_function
70
+
71
+ # Ruby sometimes has two warnings for one actual occurence
72
+ # Example:
73
+ # caller.rb:1: warning: Passing the keyword argument as the last hash parameter is deprecated
74
+ # calleee.rb:1: warning: The called method `method_name' is defined here
75
+ def two_part_warning?(str)
76
+ # see ruby src - `rb_warn`, `rb_compile_warn`
77
+ str.end_with?(
78
+ "uses the deprecated method signature, which takes one parameter\n", # respond_to?
79
+ # 2.7 kwargs:
80
+ "maybe ** should be added to the call\n",
81
+ "Passing the keyword argument as the last hash parameter is deprecated\n", # бывает и не двойной
82
+ "Splitting the last argument into positional and keyword parameters is deprecated\n"
83
+ ) ||
84
+ str.include?("warning: already initialized constant") ||
85
+ str.include?("warning: method redefined; discarding old")
86
+ end
87
+
88
+ def handle(new_str)
89
+ old_str = Thread.current[:multipart_warning_str]
90
+ Thread.current[:multipart_warning_str] = nil
91
+ if old_str
92
+ return yield(old_str + new_str) if new_str.include?('is defined here') || new_str.include?(' was here')
93
+ yield(old_str)
94
+ end
95
+
96
+ if two_part_warning?(new_str)
97
+ Thread.current[:multipart_warning_str] = new_str
98
+ return
99
+ end
100
+
101
+ yield(new_str)
102
+ end
103
+ end
104
+
105
+ # taps into ruby core Warning#warn
106
+ module WarningCollector
107
+ def warn(str)
108
+ backtrace = caller
109
+ MultipartWarningJoiner.handle(str) do |multi_str|
110
+ DeprecationCollector.collect(multi_str, backtrace, :warning)
111
+ end
112
+ end
113
+ end
114
+
115
+ module KernelWarningCollector
116
+ module_function
117
+
118
+ def warn(*messages, backtrace: nil, **_kwargs)
119
+ backtrace ||= caller
120
+ str = messages.map(&:to_s).join("\n").strip
121
+ DeprecationCollector.collect(str, backtrace, :kernel)
122
+ # not passing to `super` - it will pass to Warning#warn, we do not want that
123
+ end
124
+ end
125
+
126
+ class Deprecation
127
+ attr_reader :message, :realm, :gem_traceline, :app_traceline, :occurences, :full_backtrace
128
+
129
+ def initialize(message, realm = nil, backtrace = [], cleanup_prefixes = [])
130
+ # backtrace is Thread::Backtrace::Location or array of strings for other realms
131
+ @message = message.dup
132
+ @realm = realm
133
+ @occurences = 0
134
+ @gem_traceline = backtrace.find { |line| !line.to_s.include?('kernel_warn') }&.to_s&.dup ||
135
+ backtrace.first.to_s.dup
136
+ cleanup_prefixes.each do |path|
137
+ @gem_traceline.delete_prefix!(path)
138
+ @message.gsub!(path, '')
139
+ end
140
+
141
+ app_root = "#{DeprecationCollector.instance.app_root}/" # rubocop:disable Rails/FilePath
142
+ @app_traceline = backtrace.find do |line|
143
+ line = line.to_s
144
+ (!line.start_with?('/') || line.start_with?(app_root)) && !line.include?('/gems/')
145
+ end&.to_s&.dup&.delete_prefix(app_root)
146
+
147
+ # rails views generated methods names are unique per-worker
148
+ @gem_traceline&.gsub!(/_app_views_(\w+)__(\d+)_(\d+)/, "_app_views_\\1__")
149
+ @app_traceline&.gsub!(/_app_views_(\w+)__(\d+)_(\d+)/, "_app_views_\\1__")
150
+
151
+ # repl line numbers are not important, may be ignore all repl at all
152
+ @app_traceline&.gsub!(/\A\((pry|irb)\):\d+/, '(\1)')
153
+ @gem_traceline&.gsub!(/\A\((pry|irb)\):\d+/, '(\1)') # may contain app traceline, so filter too
154
+
155
+ @full_backtrace = backtrace.map(&:to_s) if DeprecationCollector.instance.save_full_backtrace
156
+ end
157
+
158
+ def touch
159
+ @occurences += 1
160
+ end
161
+
162
+ def ignored?
163
+ false
164
+ end
165
+
166
+ def message_for_digest
167
+ # some gems like rest-client put data in warnings, need to aggregate
168
+ # + some bactrace per-worker unique method names may be there
169
+ @message.gsub(/"(?:[^"\\]|\\.)*"/, '""').gsub(/__\d+_\d+/, '___').gsub(/\((pry|irb)\):\d+/, '(\1)')
170
+ end
171
+
172
+ def digest
173
+ @digest ||= Digest::MD5.hexdigest(digest_base)
174
+ end
175
+
176
+ def digest_base
177
+ "1:#{RUBY_VERSION}:#{Rails.version}:#{message_for_digest}:#{gem_traceline}:#{app_traceline}"
178
+ end
179
+
180
+ def as_json(_options = {})
181
+ {
182
+ message: message,
183
+ realm: realm,
184
+ app_traceline: app_traceline,
185
+ gem_traceline: (gem_traceline != app_traceline && gem_traceline) || nil,
186
+ full_backtrace: full_backtrace,
187
+ ruby_version: RUBY_VERSION,
188
+ rails_version: Rails.version,
189
+ hostname: Socket.gethostname,
190
+ revision: DeprecationCollector.instance.app_revision,
191
+ count: @occurences, # output anyway for frequency estimation (during write_interval inside single process)
192
+ digest_base: digest_base # for debug purposes
193
+ }.compact
194
+ end
195
+ end
196
+
197
+ attr_accessor :count, :raise_on_deprecation, :save_full_backtrace,
198
+ :exclude_realms,
199
+ :write_interval, :write_interval_jitter,
200
+ :app_revision, :app_root
201
+
202
+ def initialize(redis, mutex: nil)
203
+ @redis = redis
204
+ # on cruby hash itself is threadsafe, but we need to prevent races
205
+ @deprecations_mutex = mutex || Mutex.new
206
+ @deprecations = {}
207
+ @known_digests = Set.new
208
+ @last_write_time = current_time
209
+
210
+ # default config:
211
+ @count = false
212
+ @raise_on_deprecation = false
213
+ @exclude_realms = []
214
+ @write_interval = FLUSH_INTERVAL
215
+ @write_interval_jitter = 60
216
+ @enabled = true
217
+ @ignore_message_regexp = nil
218
+ @app_root = defined?(Rails) && Rails.root.present? || Dir.pwd
219
+
220
+ fetch_known_digests # prevent fresh process from wiring frequent already known messages
221
+ end
222
+
223
+ def ignored_messages=(val)
224
+ @ignore_message_regexp = (val && Regexp.union(val)) || nil
225
+ end
226
+
227
+ def cleanup_prefixes
228
+ @cleanup_prefixes ||= Gem.path + ["#{app_root}/"] # rubocop:disable Rails/FilePath
229
+ end
230
+
231
+ def collect(message, backtrace, realm = :unknown)
232
+ return unless @enabled
233
+ return if exclude_realms.include?(realm)
234
+ return if @ignore_message_regexp&.match?(message)
235
+ raise "Deprecation: #{message}" if @raise_on_deprecation
236
+
237
+ deprecation = Deprecation.new(message, realm, backtrace, cleanup_prefixes)
238
+ return if deprecation.ignored?
239
+
240
+ @deprecations_mutex.synchronize do
241
+ (@deprecations[deprecation.digest] ||= deprecation).touch
242
+ end
243
+
244
+ write_to_redis if current_time - @last_write_time > (@write_interval + rand(@write_interval_jitter))
245
+
246
+ $stderr.puts(message) if Rails.env.development? # rubocop:disable Style/StderrPuts
247
+ end
248
+
249
+ def unsent_data?
250
+ @deprecations.any?
251
+ end
252
+
253
+ def count?
254
+ @count
255
+ end
256
+
257
+ def fetch_known_digests
258
+ @known_digests.merge(@redis.hkeys('deprecations:data'))
259
+ end
260
+
261
+ def write_to_redis(force: false)
262
+ return unless @enabled || force
263
+
264
+ deprecations_to_flush = nil
265
+ @deprecations_mutex.synchronize do
266
+ # check in this section to prevent multiple check requests
267
+ unless enabled_in_redis?
268
+ @enabled = false
269
+ @deprecations = {}
270
+ return
271
+ end
272
+
273
+ return unless force || current_time > @last_write_time + @write_interval
274
+
275
+ deprecations_to_flush = @deprecations
276
+ @deprecations = {}
277
+ @last_write_time = current_time
278
+ end
279
+
280
+ # count is expensive in production env (but possible if needed) - a lot of redis writes
281
+ if count?
282
+ @redis.pipelined do
283
+ deprecations_to_flush.each_pair do |digest, deprecation|
284
+ @redis.hincrby("deprecations:counter", digest, deprecation.occurences)
285
+ end
286
+ end
287
+ end
288
+
289
+ # make as few writes as possible, other workers may already have reported our warning
290
+ # TODO: at some point turn off writes?
291
+ fetch_known_digests
292
+ deprecations_to_flush.reject! { |digest, _val| @known_digests.include?(digest) }
293
+ return unless deprecations_to_flush.any?
294
+
295
+ @known_digests.merge(deprecations_to_flush.keys)
296
+ @redis.mapped_hmset("deprecations:data", deprecations_to_flush.transform_values(&:to_json))
297
+ end
298
+
299
+ def flush_redis(enable: false)
300
+ @redis.del("deprecations:data", "deprecations:counter", "deprecations:notes")
301
+ @redis.del("deprecations:enabled") if enable
302
+ @deprecations.clear
303
+ @known_digests.clear
304
+ end
305
+
306
+ def enabled_in_redis?
307
+ @redis.get("deprecations:enabled") != "false"
308
+ end
309
+
310
+ def enable
311
+ @enabled = true
312
+ @redis.set("deprecations:enabled", "true")
313
+ end
314
+
315
+ def disable
316
+ @enabled = false
317
+ @redis.set("deprecations:enabled", "false")
318
+ end
319
+
320
+ def read_each
321
+ return to_enum(:read_each) unless block_given?
322
+
323
+ cursor = 0
324
+ loop do
325
+ cursor, data_pairs = @redis.hscan("deprecations:data", cursor)
326
+
327
+ if data_pairs.any?
328
+ data_pairs.zip(
329
+ @redis.hmget("deprecations:counter", data_pairs.map(&:first)),
330
+ @redis.hmget("deprecations:notes", data_pairs.map(&:first))
331
+ ).each do |(digest, data), count, notes|
332
+ yield decode_deprecation(digest, data, count, notes)
333
+ end
334
+ end
335
+ break if cursor == "0"
336
+ end
337
+ end
338
+
339
+ def read_one(digest)
340
+ decode_deprecation(
341
+ digest,
342
+ @redis.hget("deprecations:data", digest),
343
+ @redis.hget("deprecations:counter", digest),
344
+ @redis.hget("deprecations:notes", digest)
345
+ )
346
+ end
347
+
348
+ def delete_deprecations(remove_digests)
349
+ @redis.pipelined do
350
+ @redis.hdel("deprecations:data", *remove_digests)
351
+ @redis.hdel("deprecations:notes", *remove_digests)
352
+ @redis.hdel("deprecations:counter", *remove_digests) if @count
353
+ end
354
+ end
355
+
356
+ def cleanup
357
+ cursor = 0
358
+ removed = 0
359
+ total = 0
360
+ loop do
361
+ cursor, data_pairs = @redis.hscan("deprecations:data", cursor)
362
+
363
+ if data_pairs.any?
364
+ remove_digests = []
365
+ total += data_pairs.size
366
+ data_pairs.each do |(digest, data)|
367
+ data = JSON.parse(data, symbolize_names: true)
368
+ remove_digests << digest if !block_given? || yield(data)
369
+ end
370
+
371
+ if remove_digests.any?
372
+ delete_deprecations(remove_digests)
373
+ removed += remove_digests.size
374
+ end
375
+ end
376
+ break if cursor == "0"
377
+ end
378
+ "#{removed} removed, #{total - removed} left"
379
+ end
380
+
381
+ private
382
+
383
+ def current_time
384
+ return Time.zone.now if Time.respond_to?(:zone) && Time.zone
385
+ Time.now
386
+ end
387
+
388
+ def decode_deprecation(digest, data, count, notes)
389
+ data = JSON.parse(data, symbolize_names: true)
390
+ data[:digest] = digest
391
+ data[:notes] = JSON.parse(notes, symbolize_names: true) if notes
392
+ data[:count] = count.to_i if count
393
+ data
394
+ end
395
+ end
@@ -0,0 +1,4 @@
1
+ module DeprecationCollector
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deprecation_collector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vasily Fedoseyev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Collects and aggregates warnings and deprecations. Optimized for production
28
+ environment.
29
+ email:
30
+ - vasilyfedoseyev@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CHANGELOG.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - deprecation_collector.gemspec
45
+ - lib/deprecation_collector.rb
46
+ - lib/deprecation_collector/version.rb
47
+ - sig/deprecation_collector.rbs
48
+ homepage: https://github.com/Vasfed/deprecation_collector
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/Vasfed/deprecation_collector
53
+ source_code_uri: https://github.com/Vasfed/deprecation_collector
54
+ changelog_uri: https://github.com/Vasfed/deprecation_collector/blob/main/CHANGELOG.md
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.4.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Collector for ruby/rails deprecations and warnings, suitable for production
74
+ test_files: []