super_tools 4.0.3 → 4.1.0
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/.github/workflows/publish.yml +1 -1
- data/.github/workflows/test.yml +23 -2
- data/CHANGELOG.md +107 -0
- data/CLAUDE.md +21 -0
- data/Gemfile.lock +1 -1
- data/README.md +352 -74
- data/Rakefile +8 -1
- data/docs/super_airbrake.md +463 -0
- data/gemfiles/super_airbrake_legacy.gemfile +15 -0
- data/lib/super_airbrake/configuration.rb +276 -0
- data/lib/super_airbrake/installer.rb +108 -0
- data/lib/super_airbrake/matcher.rb +134 -0
- data/lib/super_airbrake/rule.rb +106 -0
- data/lib/super_airbrake/rules/blocklist.rb +57 -0
- data/lib/super_airbrake/rules/current_session.rb +19 -0
- data/lib/super_airbrake/rules/ignore.rb +16 -0
- data/lib/super_airbrake/rules/invalid_mime.rb +19 -0
- data/lib/super_airbrake/rules/performance_threshold.rb +22 -0
- data/lib/super_airbrake/rules/proxy_timeout.rb +64 -0
- data/lib/super_airbrake/rules/referer_blocklist.rb +51 -0
- data/lib/super_airbrake/rules/retryable_jobs.rb +23 -0
- data/lib/super_airbrake/rules/signal_exception.rb +19 -0
- data/lib/super_airbrake/rules/web_not_found.rb +23 -0
- data/lib/super_airbrake/safe_filter.rb +25 -0
- data/lib/super_airbrake/skip_fast_query.rb +39 -0
- data/lib/super_airbrake/testing.rb +98 -0
- data/lib/super_airbrake.rb +98 -0
- data/lib/super_tools/version.rb +1 -1
- data/super_tools.gemspec +3 -0
- metadata +63 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
module Rules
|
|
5
|
+
# c.ignore 產生的規則:條件都符合就 notice.ignore!
|
|
6
|
+
class Ignore < Rule
|
|
7
|
+
rule_name :ignore
|
|
8
|
+
enabled_by_default true
|
|
9
|
+
required_options :matcher
|
|
10
|
+
|
|
11
|
+
def call(notice)
|
|
12
|
+
notice.ignore! if options[:matcher].match?(notice)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
module Rules
|
|
5
|
+
# 無效的 MIME type,通常來自信件掃描機器人或惡意請求,不通報
|
|
6
|
+
class InvalidMime < Rule
|
|
7
|
+
rule_name :invalid_mime
|
|
8
|
+
enabled_by_default true
|
|
9
|
+
defaults exceptions: ["ActionDispatch::Http::MimeNegotiation::InvalidType", "Mime::Type::InvalidMimeType"]
|
|
10
|
+
|
|
11
|
+
def call(notice)
|
|
12
|
+
exception = notice.stash[:exception]
|
|
13
|
+
return unless exception
|
|
14
|
+
|
|
15
|
+
notice.ignore! if Array(options[:exceptions]).any? { |name| Matcher.kind_of_name?(exception, name) }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
module Rules
|
|
5
|
+
# 效能事件:低於門檻(c.performance_threshold_ms,或規則參數 threshold_ms)不上報
|
|
6
|
+
class PerformanceThreshold < Rule
|
|
7
|
+
rule_name :performance_threshold
|
|
8
|
+
kind :performance
|
|
9
|
+
enabled_by_default true
|
|
10
|
+
defaults threshold_ms: nil
|
|
11
|
+
|
|
12
|
+
def threshold_ms
|
|
13
|
+
options[:threshold_ms] || (config && config.performance_threshold_ms)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call(metric)
|
|
17
|
+
threshold = threshold_ms
|
|
18
|
+
metric.ignore! if threshold && metric.timing < threshold
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/number_helper"
|
|
4
|
+
|
|
5
|
+
module SuperAirbrake
|
|
6
|
+
module Rules
|
|
7
|
+
# request 跑贏反向代理(kamal-proxy)response_timeout:使用者已看到 504,Rails 仍默默做完。
|
|
8
|
+
# 在效能事件裡偵測,另發一筆 error_class 的錯誤通報。
|
|
9
|
+
# 只看 PerformanceBreakdown(帶 db/view 耗時);沒碰 DB 也沒 render 的 request 不會產生它。
|
|
10
|
+
# 需要 performance_stats 開著才會跑。
|
|
11
|
+
class ProxyTimeout < Rule
|
|
12
|
+
VIA = [:performance_filter].freeze
|
|
13
|
+
|
|
14
|
+
rule_name :proxy_timeout
|
|
15
|
+
kind :performance
|
|
16
|
+
enabled_by_default false
|
|
17
|
+
defaults via: :performance_filter
|
|
18
|
+
required_options :threshold_ms, :error_class
|
|
19
|
+
|
|
20
|
+
def build(installer)
|
|
21
|
+
@notifier = installer.notifier
|
|
22
|
+
self
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def call(metric)
|
|
26
|
+
return unless metric.is_a?(::Airbrake::PerformanceBreakdown) && metric.timing >= options[:threshold_ms]
|
|
27
|
+
|
|
28
|
+
notify(metric)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def validate!
|
|
34
|
+
super
|
|
35
|
+
raise ArgumentError, "proxy_timeout 的 via 只能是 #{VIA.inspect}" unless VIA.include?(options[:via])
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def notify(metric)
|
|
39
|
+
exception = options[:error_class].new(
|
|
40
|
+
"用時 #{ActiveSupport::NumberHelper.number_to_delimited(metric.timing.round)}ms " \
|
|
41
|
+
"(超過 kamal proxy timeout #{options[:threshold_ms] / 1000}s)"
|
|
42
|
+
)
|
|
43
|
+
@notifier.notify(exception) do |notice|
|
|
44
|
+
notice[:context][:httpMethod] = metric.method
|
|
45
|
+
notice[:context][:route] = metric.route
|
|
46
|
+
# GitHub issue 本文只顯示 URL 欄,不顯示 route
|
|
47
|
+
notice[:context][:url] = url_for(metric)
|
|
48
|
+
notice[:context][:responseType] = metric.response_type
|
|
49
|
+
notice[:params][:timing_ms] = metric.timing.round
|
|
50
|
+
# db + view 通常湊不滿 timing_ms,差額是等外部服務與 Ruby 自身運算
|
|
51
|
+
notice[:params][:db_ms] = metric.groups[:db] && metric.groups[:db].round
|
|
52
|
+
notice[:params][:view_ms] = metric.groups[:view] && metric.groups[:view].round
|
|
53
|
+
notice[:params][:request_time] = metric.time
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# 多網域的 route 樣板會相同,補上網域才分得開
|
|
58
|
+
def url_for(metric)
|
|
59
|
+
request = metric.stash[:request]
|
|
60
|
+
request ? "#{request.base_url}#{metric.route}" : metric.route
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
module Rules
|
|
5
|
+
# KeysBlocklist 只遮 context.url;這條把 context[:referer] 與 headers 裡 HTTP_REFERER 的 query 也遮掉
|
|
6
|
+
# (例如重設密碼頁的 reset_password_token)。keys 沿用 blocklist 規則的設定。
|
|
7
|
+
class RefererBlocklist < Rule
|
|
8
|
+
rule_name :referer_blocklist
|
|
9
|
+
enabled_by_default true
|
|
10
|
+
|
|
11
|
+
class Filter < ::Airbrake::Filters::KeysBlocklist
|
|
12
|
+
def initialize(patterns_source)
|
|
13
|
+
super([])
|
|
14
|
+
@patterns_source = patterns_source
|
|
15
|
+
@patterns_loaded = false
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def call(notice)
|
|
19
|
+
load_patterns!
|
|
20
|
+
context = notice[:context]
|
|
21
|
+
context[:referer] = filter_referer(context[:referer]) if context[:referer]
|
|
22
|
+
|
|
23
|
+
headers = context[:headers]
|
|
24
|
+
return unless headers.is_a?(Hash) && headers["HTTP_REFERER"]
|
|
25
|
+
|
|
26
|
+
context[:headers] = headers.merge("HTTP_REFERER" => filter_referer(headers["HTTP_REFERER"]))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def load_patterns!
|
|
32
|
+
return if @patterns_loaded
|
|
33
|
+
|
|
34
|
+
@patterns = @patterns_source.call
|
|
35
|
+
@patterns_loaded = true
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def filter_referer(referer)
|
|
39
|
+
url = URI(referer)
|
|
40
|
+
url.query ? filter_url_params(url) : referer
|
|
41
|
+
rescue URI::InvalidURIError
|
|
42
|
+
referer
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def build(installer)
|
|
47
|
+
Filter.new(-> { installer.blocklist_patterns })
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
module Rules
|
|
5
|
+
# Sidekiq 原生 retry(sidekiq_options retry: N)重試期間的失敗不通報,次數用完才通報。
|
|
6
|
+
# 只影響 Sidekiq middleware 那筆 notice(params[:job]);沒載入 Sidekiq 的專案不會掛。
|
|
7
|
+
class RetryableJobs < Rule
|
|
8
|
+
rule_name :retryable_jobs
|
|
9
|
+
enabled_by_default true
|
|
10
|
+
defaults max_retries: nil
|
|
11
|
+
|
|
12
|
+
def self.available?
|
|
13
|
+
defined?(::Airbrake::Sidekiq::RetryableJobsFilter) ? true : false
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def build(_installer)
|
|
17
|
+
return nil unless self.class.available?
|
|
18
|
+
|
|
19
|
+
::Airbrake::Sidekiq::RetryableJobsFilter.new(max_retries: options[:max_retries])
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
module Rules
|
|
5
|
+
# Puma worker 重啟、Kamal rolling deploy 對舊容器發 SIGTERM,屬正常行為,不通報
|
|
6
|
+
class SignalException < Rule
|
|
7
|
+
rule_name :signal_exception
|
|
8
|
+
enabled_by_default true
|
|
9
|
+
defaults exceptions: ["SignalException"]
|
|
10
|
+
|
|
11
|
+
def call(notice)
|
|
12
|
+
exception = notice.stash[:exception]
|
|
13
|
+
return unless exception
|
|
14
|
+
|
|
15
|
+
notice.ignore! if Array(options[:exceptions]).any? { |name| Matcher.kind_of_name?(exception, name) }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
module Rules
|
|
5
|
+
# 網頁請求裡 Rails 回 404 的例外不通報(使用者已經看到 404 頁)。
|
|
6
|
+
# status 依 ActionDispatch 的 rescue_responses 判斷,專案自己加的對應(例如 MissingController → :not_found)也算。
|
|
7
|
+
# web_only: true 時只看有 rack_request 的 notice,job 裡的 RecordNotFound 照報。
|
|
8
|
+
# except: ->(notice) { ... } 回 true 的照報。
|
|
9
|
+
class WebNotFound < Rule
|
|
10
|
+
rule_name :web_not_found
|
|
11
|
+
enabled_by_default false
|
|
12
|
+
defaults statuses: [404], web_only: true, except: nil
|
|
13
|
+
|
|
14
|
+
def call(notice)
|
|
15
|
+
return if options[:web_only] && Matcher.rack_request(notice).nil?
|
|
16
|
+
return unless Array(options[:statuses]).include?(Matcher.status_for(notice.stash[:exception]))
|
|
17
|
+
return if options[:except] && options[:except].call(notice)
|
|
18
|
+
|
|
19
|
+
notice.ignore!
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
# 包住每一條規則:保留 weight 讓 Airbrake 排序,safe 時規則出錯只寫 log、不影響通報與 request。
|
|
5
|
+
class SafeFilter
|
|
6
|
+
attr_reader :name, :filter, :weight
|
|
7
|
+
|
|
8
|
+
def initialize(name, filter, logger:, weight:, safe:)
|
|
9
|
+
@name = name
|
|
10
|
+
@filter = filter
|
|
11
|
+
@logger = logger
|
|
12
|
+
@weight = weight
|
|
13
|
+
@safe = safe
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call(target)
|
|
17
|
+
@filter.call(target)
|
|
18
|
+
rescue StandardError => e
|
|
19
|
+
raise unless @safe
|
|
20
|
+
|
|
21
|
+
@logger.error("[SuperAirbrake] 規則 #{@name} 出錯:#{e.class}: #{e.message}") if @logger
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperAirbrake
|
|
4
|
+
# c.query_stats = :skip_fast
|
|
5
|
+
# 低於效能門檻的 SQL 不跑 Airbrake 的 Kernel.caller(那是每句 SQL 都要付的成本),門檻以上照舊上報。
|
|
6
|
+
# 門檻與 performance_threshold 相同,否則慢查詢會漏報。
|
|
7
|
+
module SkipFastQuery
|
|
8
|
+
class << self
|
|
9
|
+
attr_accessor :threshold_ms
|
|
10
|
+
|
|
11
|
+
def install!(threshold_ms)
|
|
12
|
+
require "airbrake/rails/active_record_subscriber"
|
|
13
|
+
|
|
14
|
+
self.threshold_ms = threshold_ms
|
|
15
|
+
subscriber = ::Airbrake::Rails::ActiveRecordSubscriber
|
|
16
|
+
subscriber.prepend(self) unless subscriber.ancestors.include?(self)
|
|
17
|
+
silence_backtrace!
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
# 慢查詢的呼叫位置不能指到這支檔案
|
|
23
|
+
def silence_backtrace!
|
|
24
|
+
return if (defined?(@silenced) && @silenced) || !defined?(::Rails) || !::Rails.respond_to?(:backtrace_cleaner)
|
|
25
|
+
|
|
26
|
+
file = __FILE__
|
|
27
|
+
::Rails.backtrace_cleaner.add_silencer { |line| line.include?(file) }
|
|
28
|
+
@silenced = true
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def call(*args)
|
|
33
|
+
threshold = SuperAirbrake::SkipFastQuery.threshold_ms
|
|
34
|
+
return if threshold && ActiveSupport::Notifications::Event.new(*args).duration < threshold
|
|
35
|
+
|
|
36
|
+
super
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "super_airbrake"
|
|
4
|
+
|
|
5
|
+
module SuperAirbrake
|
|
6
|
+
# 測試用:不碰全域 Airbrake,用 airbrake-ruby 真的 FilterChain 跑規則(含 weight 排序)。
|
|
7
|
+
#
|
|
8
|
+
# require "super_airbrake/testing"
|
|
9
|
+
# notifier = SuperAirbrake::Testing.install { |c| c.rule :web_not_found }
|
|
10
|
+
# notice = notifier.build_notice(ActiveRecord::RecordNotFound.new, rack_request: request)
|
|
11
|
+
# assert notifier.refine(notice).ignored?
|
|
12
|
+
module Testing
|
|
13
|
+
class Notifier
|
|
14
|
+
attr_reader :notified, :config, :configure_calls, :filters, :performance_filters
|
|
15
|
+
attr_accessor :super_airbrake_config
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@notice_chain = ::Airbrake::FilterChain.new
|
|
19
|
+
@performance_chain = ::Airbrake::FilterChain.new
|
|
20
|
+
@filters = []
|
|
21
|
+
@performance_filters = []
|
|
22
|
+
@notified = []
|
|
23
|
+
@config = ::Airbrake::Config.new
|
|
24
|
+
@configure_calls = 0
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def configure
|
|
28
|
+
@configure_calls += 1
|
|
29
|
+
yield @config
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def configured?
|
|
33
|
+
@configure_calls > 0
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def add_filter(filter = nil, &block)
|
|
37
|
+
filter ||= block
|
|
38
|
+
@filters << filter
|
|
39
|
+
@notice_chain.add_filter(filter)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def add_performance_filter(filter = nil, &block)
|
|
43
|
+
filter ||= block
|
|
44
|
+
@performance_filters << filter
|
|
45
|
+
@performance_chain.add_filter(filter)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def filter_names
|
|
49
|
+
@filters.map { |filter| filter.respond_to?(:name) ? filter.name : filter.class.name }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def performance_filter_names
|
|
53
|
+
@performance_filters.map { |filter| filter.respond_to?(:name) ? filter.name : filter.class.name }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def notify(exception, params = {})
|
|
57
|
+
notice = ::Airbrake::Notice.new(exception, params)
|
|
58
|
+
yield notice if block_given?
|
|
59
|
+
refine(notice)
|
|
60
|
+
@notified << notice unless notice.ignored?
|
|
61
|
+
notice
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def refine(notice)
|
|
65
|
+
@notice_chain.refine(notice)
|
|
66
|
+
notice
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def refine_metric(metric)
|
|
70
|
+
@performance_chain.refine(metric)
|
|
71
|
+
metric
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# rack_request 可以直接給 request;或給 controller 由這裡組一個帶 action_controller.instance 的 request
|
|
75
|
+
def build_notice(exception, rack_request: nil, controller: nil, params: nil)
|
|
76
|
+
notice = ::Airbrake::Notice.new(exception)
|
|
77
|
+
if controller
|
|
78
|
+
require "rack/mock"
|
|
79
|
+
env = ::Rack::MockRequest.env_for("/")
|
|
80
|
+
env["action_controller.instance"] = controller
|
|
81
|
+
rack_request ||= ::Rack::Request.new(env)
|
|
82
|
+
end
|
|
83
|
+
notice.stash[:rack_request] = rack_request if rack_request
|
|
84
|
+
notice[:params] = params if params
|
|
85
|
+
notice
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# 建一個獨立的 Configuration 並裝到測試用 Notifier,不影響 SuperAirbrake.configure 的全域狀態
|
|
90
|
+
def self.install(notifier = Notifier.new)
|
|
91
|
+
config = Configuration.new
|
|
92
|
+
yield config if block_given?
|
|
93
|
+
Installer.new(config, notifier: notifier, airbrake_config: notifier.config).install
|
|
94
|
+
notifier.super_airbrake_config = config
|
|
95
|
+
notifier
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SuperAirbrake:把 Airbrake 的共用通報規則收在同一個地方,各專案只寫自己的差異。
|
|
4
|
+
# 不會被 `require "super_tools"` 自動載入,使用的專案要自己 `require "super_airbrake"`。
|
|
5
|
+
# 本目錄的程式維持 Ruby 2.6/ActiveSupport 5.2/airbrake 13.0.1 相容(一頁商店仍在這個組合)。
|
|
6
|
+
begin
|
|
7
|
+
require "airbrake"
|
|
8
|
+
rescue LoadError
|
|
9
|
+
raise LoadError, "SuperAirbrake 需要 gem 'airbrake',請在 Gemfile 加入 gem \"airbrake\""
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
require "logger"
|
|
13
|
+
require "active_support"
|
|
14
|
+
require "active_support/core_ext/object/blank"
|
|
15
|
+
|
|
16
|
+
module SuperAirbrake
|
|
17
|
+
class Error < StandardError; end
|
|
18
|
+
# 同一個 process 只能 configure 一次,避免同一批 filter 重複掛兩次
|
|
19
|
+
class AlreadyConfigured < Error; end
|
|
20
|
+
# configure 結束後設定就凍結
|
|
21
|
+
class ConfigurationFrozen < Error; end
|
|
22
|
+
class UnknownRule < Error; end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
require "super_airbrake/rule"
|
|
26
|
+
require "super_airbrake/matcher"
|
|
27
|
+
require "super_airbrake/safe_filter"
|
|
28
|
+
require "super_airbrake/skip_fast_query"
|
|
29
|
+
require "super_airbrake/rules/blocklist"
|
|
30
|
+
require "super_airbrake/rules/referer_blocklist"
|
|
31
|
+
require "super_airbrake/rules/signal_exception"
|
|
32
|
+
require "super_airbrake/rules/invalid_mime"
|
|
33
|
+
require "super_airbrake/rules/retryable_jobs"
|
|
34
|
+
require "super_airbrake/rules/web_not_found"
|
|
35
|
+
require "super_airbrake/rules/current_session"
|
|
36
|
+
require "super_airbrake/rules/performance_threshold"
|
|
37
|
+
require "super_airbrake/rules/proxy_timeout"
|
|
38
|
+
require "super_airbrake/rules/ignore"
|
|
39
|
+
require "super_airbrake/configuration"
|
|
40
|
+
require "super_airbrake/installer"
|
|
41
|
+
|
|
42
|
+
module SuperAirbrake
|
|
43
|
+
# 內建規則,順序就是 explain 與掛載的順序
|
|
44
|
+
BUILTIN_RULES = [
|
|
45
|
+
Rules::Blocklist,
|
|
46
|
+
Rules::RefererBlocklist,
|
|
47
|
+
Rules::SignalException,
|
|
48
|
+
Rules::InvalidMime,
|
|
49
|
+
Rules::RetryableJobs,
|
|
50
|
+
Rules::WebNotFound,
|
|
51
|
+
Rules::CurrentSession,
|
|
52
|
+
Rules::PerformanceThreshold,
|
|
53
|
+
Rules::ProxyTimeout
|
|
54
|
+
].freeze
|
|
55
|
+
|
|
56
|
+
class << self
|
|
57
|
+
# 專案的 config/initializers/airbrake.rb 呼叫一次。
|
|
58
|
+
# notifier/airbrake_config 只在測試時替換(見 SuperAirbrake::Testing)。
|
|
59
|
+
def configure(notifier: ::Airbrake, airbrake_config: nil)
|
|
60
|
+
raise AlreadyConfigured, "SuperAirbrake.configure 已經執行過,同一個 process 只能呼叫一次" if @config
|
|
61
|
+
|
|
62
|
+
config = Configuration.new
|
|
63
|
+
yield config if block_given?
|
|
64
|
+
Installer.new(config, notifier: notifier, airbrake_config: airbrake_config || ::Airbrake::Config.instance).install
|
|
65
|
+
@config = config
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def config
|
|
69
|
+
@config
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def configured?
|
|
73
|
+
!@config.nil?
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# [{ name:, kind:, enabled:, source:, options: }, ...]
|
|
77
|
+
def rules
|
|
78
|
+
@config ? @config.rules_summary : []
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def explain
|
|
82
|
+
@config ? @config.explain : "SuperAirbrake 尚未設定"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# 只給測試用:清掉設定,讓下一個測試能再 configure。已經掛到 Airbrake 的 filter 不會拿掉。
|
|
86
|
+
def reset!
|
|
87
|
+
@config = nil
|
|
88
|
+
SkipFastQuery.threshold_ms = nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# 註冊專案自訂的規則類別,之後就能用 c.rule :name 開啟
|
|
92
|
+
def register(rule_class)
|
|
93
|
+
Rule.register(rule_class)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
BUILTIN_RULES.each { |rule_class| register(rule_class) }
|
|
98
|
+
end
|
data/lib/super_tools/version.rb
CHANGED
data/super_tools.gemspec
CHANGED
|
@@ -30,6 +30,9 @@ Gem::Specification.new do |spec|
|
|
|
30
30
|
spec.add_development_dependency "warning"
|
|
31
31
|
spec.add_development_dependency "sqlite3", ">= 1.4", "< 2"
|
|
32
32
|
spec.add_development_dependency "appraisal"
|
|
33
|
+
# SuperAirbrake 只在測試時需要;使用的專案自己的 Gemfile 本來就有 airbrake,這裡不加成硬相依
|
|
34
|
+
spec.add_development_dependency "airbrake", ">= 13.0.1", "< 14"
|
|
35
|
+
spec.add_development_dependency "sidekiq", ">= 5", "< 8"
|
|
33
36
|
|
|
34
37
|
spec.add_dependency "rails", ">= 6.1", "< 8.2"
|
|
35
38
|
spec.add_dependency "nokogiri", ">= 1.6"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: super_tools
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.0
|
|
4
|
+
version: 4.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- eddie
|
|
@@ -119,6 +119,46 @@ dependencies:
|
|
|
119
119
|
- - ">="
|
|
120
120
|
- !ruby/object:Gem::Version
|
|
121
121
|
version: '0'
|
|
122
|
+
- !ruby/object:Gem::Dependency
|
|
123
|
+
name: airbrake
|
|
124
|
+
requirement: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - ">="
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: 13.0.1
|
|
129
|
+
- - "<"
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '14'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: 13.0.1
|
|
139
|
+
- - "<"
|
|
140
|
+
- !ruby/object:Gem::Version
|
|
141
|
+
version: '14'
|
|
142
|
+
- !ruby/object:Gem::Dependency
|
|
143
|
+
name: sidekiq
|
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
|
145
|
+
requirements:
|
|
146
|
+
- - ">="
|
|
147
|
+
- !ruby/object:Gem::Version
|
|
148
|
+
version: '5'
|
|
149
|
+
- - "<"
|
|
150
|
+
- !ruby/object:Gem::Version
|
|
151
|
+
version: '8'
|
|
152
|
+
type: :development
|
|
153
|
+
prerelease: false
|
|
154
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
155
|
+
requirements:
|
|
156
|
+
- - ">="
|
|
157
|
+
- !ruby/object:Gem::Version
|
|
158
|
+
version: '5'
|
|
159
|
+
- - "<"
|
|
160
|
+
- !ruby/object:Gem::Version
|
|
161
|
+
version: '8'
|
|
122
162
|
- !ruby/object:Gem::Dependency
|
|
123
163
|
name: rails
|
|
124
164
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -348,6 +388,8 @@ files:
|
|
|
348
388
|
- ".ruby-version"
|
|
349
389
|
- ".travis.yml"
|
|
350
390
|
- Appraisals
|
|
391
|
+
- CHANGELOG.md
|
|
392
|
+
- CLAUDE.md
|
|
351
393
|
- Gemfile
|
|
352
394
|
- Gemfile.lock
|
|
353
395
|
- LICENSE.txt
|
|
@@ -358,6 +400,7 @@ files:
|
|
|
358
400
|
- bin/setup
|
|
359
401
|
- bin/test_all
|
|
360
402
|
- dependabot.yml
|
|
403
|
+
- docs/super_airbrake.md
|
|
361
404
|
- gemfiles/.bundle/config
|
|
362
405
|
- gemfiles/ruby_2.7_rails_6.gemfile
|
|
363
406
|
- gemfiles/ruby_2.7_rails_7.gemfile
|
|
@@ -369,6 +412,25 @@ files:
|
|
|
369
412
|
- gemfiles/ruby_3.3_rails_8.1.gemfile
|
|
370
413
|
- gemfiles/ruby_3.4_rails_8.0.gemfile
|
|
371
414
|
- gemfiles/ruby_3.4_rails_8.1.gemfile
|
|
415
|
+
- gemfiles/super_airbrake_legacy.gemfile
|
|
416
|
+
- lib/super_airbrake.rb
|
|
417
|
+
- lib/super_airbrake/configuration.rb
|
|
418
|
+
- lib/super_airbrake/installer.rb
|
|
419
|
+
- lib/super_airbrake/matcher.rb
|
|
420
|
+
- lib/super_airbrake/rule.rb
|
|
421
|
+
- lib/super_airbrake/rules/blocklist.rb
|
|
422
|
+
- lib/super_airbrake/rules/current_session.rb
|
|
423
|
+
- lib/super_airbrake/rules/ignore.rb
|
|
424
|
+
- lib/super_airbrake/rules/invalid_mime.rb
|
|
425
|
+
- lib/super_airbrake/rules/performance_threshold.rb
|
|
426
|
+
- lib/super_airbrake/rules/proxy_timeout.rb
|
|
427
|
+
- lib/super_airbrake/rules/referer_blocklist.rb
|
|
428
|
+
- lib/super_airbrake/rules/retryable_jobs.rb
|
|
429
|
+
- lib/super_airbrake/rules/signal_exception.rb
|
|
430
|
+
- lib/super_airbrake/rules/web_not_found.rb
|
|
431
|
+
- lib/super_airbrake/safe_filter.rb
|
|
432
|
+
- lib/super_airbrake/skip_fast_query.rb
|
|
433
|
+
- lib/super_airbrake/testing.rb
|
|
372
434
|
- lib/super_form/basic.rb
|
|
373
435
|
- lib/super_form/concerns/atomic_save.rb
|
|
374
436
|
- lib/super_form/reform.rb
|