super_tools 4.0.2 → 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,463 @@
|
|
|
1
|
+
# SuperAirbrake 使用教學
|
|
2
|
+
|
|
3
|
+
把 Airbrake 的設定與 filter(遮罩敏感資料、忽略雜訊、效能門檻、proxy timeout、登入者資訊…)收成一份 DSL,讓一頁商店(landing)、火箭快遞(xdelivery)、EIP 共用同一套規則,不再各自複製 `airbrake_helper.rb`。
|
|
4
|
+
|
|
5
|
+
- 每條規則都有穩定的名稱,可以單獨 `rule`(開+調參數)、`enable`、`disable`、`replace`。
|
|
6
|
+
- 專案自訂的規則用 `ignore`/`filter`/`performance_filter`/`use`/`register` 加,不用改 gem。
|
|
7
|
+
- 所有規則都包一層 rescue(`safe = true`):規則自己出錯只寫 log,照常通報。
|
|
8
|
+
- `SuperAirbrake.explain` 一次印出目前掛了哪些規則與參數。
|
|
9
|
+
|
|
10
|
+
## 目錄
|
|
11
|
+
|
|
12
|
+
1. [安裝與載入](#1-安裝與載入)
|
|
13
|
+
2. [最小設定](#2-最小設定)
|
|
14
|
+
3. [DSL 參考](#3-dsl-參考)
|
|
15
|
+
4. [內建規則](#4-內建規則)
|
|
16
|
+
5. [`c.ignore`:宣告式忽略](#5-cignore宣告式忽略)
|
|
17
|
+
6. [自訂規則:filter/performance_filter/use/register/replace](#6-自訂規則)
|
|
18
|
+
7. [三個專案的 initializer 範例](#7-三個專案的-initializer-範例)
|
|
19
|
+
8. [除錯:rules/explain](#8-除錯rulesexplain)
|
|
20
|
+
9. [測試](#9-測試)
|
|
21
|
+
10. [相容性與 landing(2.x 線)](#10-相容性與-landing2x-線)
|
|
22
|
+
11. [Airbrake filter 執行順序(進階)](#11-airbrake-filter-執行順序進階)
|
|
23
|
+
|
|
24
|
+
## 1. 安裝與載入
|
|
25
|
+
|
|
26
|
+
`airbrake` **不是** super_tools 的硬相依,`require "super_tools"` 也**不會**載入 SuperAirbrake。專案自己的 Gemfile 本來就有 airbrake:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
# Gemfile
|
|
30
|
+
gem "airbrake" # 支援 13.0.1 ~ 13.x(airbrake-ruby 6.1 ~ 6.x)
|
|
31
|
+
gem "super_tools"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
在 initializer 明確載入:
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
# config/initializers/airbrake.rb
|
|
38
|
+
require "super_airbrake"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
沒裝 airbrake 時 `require "super_airbrake"` 會丟出說明清楚的 `LoadError`。
|
|
42
|
+
|
|
43
|
+
## 2. 最小設定
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
require "super_airbrake"
|
|
47
|
+
|
|
48
|
+
SuperAirbrake.configure do |c|
|
|
49
|
+
c.project_id = ENV["AIRBRAKE_PROJECT_ID"]
|
|
50
|
+
c.project_key = ENV["AIRBRAKE_PROJECT_KEY"]
|
|
51
|
+
end
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
這樣就會得到「預設開」的規則:敏感 key 遮罩(Rails `filter_parameters`+`Authorization`)、Referer query 遮罩、`SignalException` 不報、無效 MIME 不報、Sidekiq 重試期間不報、效能事件低於 1500ms 不報。
|
|
55
|
+
|
|
56
|
+
`configure` 結束時依序做:
|
|
57
|
+
|
|
58
|
+
1. `project_id` 與 `project_key` **都有值**才呼叫 `Airbrake.configure`(含 `c.airbrake` 區塊),並設 `ENV["REVISION"] ||= app_version`(給 `rake airbrake:deploy`;可用 `sync_revision_env = false` 關掉)。沒有憑證時只掛 filter,Airbrake 本來就不會送。
|
|
59
|
+
2. 套用 `query_stats`(不論有沒有憑證)。
|
|
60
|
+
3. 依宣告順序把開著的規則掛到 `Airbrake.add_filter`/`add_performance_filter`。
|
|
61
|
+
4. 執行 `after_install` hooks。
|
|
62
|
+
5. 凍結設定:之後再改會丟 `SuperAirbrake::ConfigurationFrozen`。
|
|
63
|
+
|
|
64
|
+
同一個 process 再呼叫一次 `SuperAirbrake.configure` 會丟 `SuperAirbrake::AlreadyConfigured`(避免重複掛 filter)。
|
|
65
|
+
|
|
66
|
+
## 3. DSL 參考
|
|
67
|
+
|
|
68
|
+
### 3.1 連線與環境
|
|
69
|
+
|
|
70
|
+
| 設定 | 預設值 | 說明 |
|
|
71
|
+
| --- | --- | --- |
|
|
72
|
+
| `project_id` | 無 | 從 ENV/Figaro/Setting 帶入,不要寫死 |
|
|
73
|
+
| `project_key` | 無 | 同上;與 `project_id` 任一為空就不呼叫 `Airbrake.configure` |
|
|
74
|
+
| `environment` | `Rails.env` | |
|
|
75
|
+
| `ignore_environments` | `%w[development test]` | |
|
|
76
|
+
| `root_directory` | `Rails.root` | |
|
|
77
|
+
| `logger` | `Airbrake::Rails.logger`(沒有 `Rails.root` 時用 `Rails.logger`) | |
|
|
78
|
+
| `timeout` | 不設(用 Airbrake 預設) | |
|
|
79
|
+
| `app_version` | `ENV["APP_REVISION"].presence` | |
|
|
80
|
+
| `sync_revision_env` | `true` | 有憑證時 `ENV["REVISION"] \|\|= app_version` |
|
|
81
|
+
| `performance_stats` | 不設(用 Airbrake 預設) | `true`/`false` |
|
|
82
|
+
| `query_stats` | 不設 | `nil`(不碰)、`true`、`false`(關掉 APM queries)、`:skip_fast`(低於門檻的 SQL 不記錄、不抓堆疊) |
|
|
83
|
+
| `performance_threshold_ms` | `1500` | 效能事件(含 `:skip_fast` 的 SQL)低於此毫秒數不上報 |
|
|
84
|
+
| `safe` | `true` | `false` 時規則出錯直接拋出(測試用) |
|
|
85
|
+
|
|
86
|
+
規則:**沒設 → 用上表的預設;明確設成 `nil` → 不碰 Airbrake 自己的預設**(例如 `c.app_version = nil`)。
|
|
87
|
+
|
|
88
|
+
逃生門:gem 沒包到的 Airbrake 選項,用 `c.airbrake` 直接拿 `Airbrake::Config`(只在有憑證、呼叫 `Airbrake.configure` 時執行):
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
c.airbrake do |a|
|
|
92
|
+
a.code_hunks = false
|
|
93
|
+
a.blocklist_keys << /card_number/ # 一般請用 c.rule :blocklist, keys: [...]
|
|
94
|
+
end
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 3.2 規則開關
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
c.rule :web_not_found # 開啟(預設關的規則)
|
|
101
|
+
c.rule :blocklist, keys: [/card_number/] # 開啟並調參數;沒給的參數沿用預設
|
|
102
|
+
c.enable :invalid_mime
|
|
103
|
+
c.disable :referer_blocklist
|
|
104
|
+
c.replace :signal_exception do |notice| # 整條換掉,名稱保留(explain 會顯示 replaced)
|
|
105
|
+
notice.ignore! if notice.stash[:exception].is_a?(SignalException) && !Rails.env.production?
|
|
106
|
+
end
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
不認得的規則名稱會丟 `SuperAirbrake::UnknownRule`;缺必填參數(例如 `:current_session` 沒給 `with:`)在 `c.rule` 當下就丟 `ArgumentError`。
|
|
110
|
+
|
|
111
|
+
### 3.3 專案自訂
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
c.ignore(*exceptions, **conditions, &block) # 見第 5 節
|
|
115
|
+
c.filter(name = nil, weight: 0) { |notice| ... } # notice.ignore! 或修改 notice
|
|
116
|
+
c.performance_filter(name = nil, weight: 0) { |metric| ... }
|
|
117
|
+
c.use(filter_or_class, name: nil, weight: nil, kind: nil, **options)
|
|
118
|
+
c.after_install { |notifier| ... } # notifier 預設是 ::Airbrake
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
沒給 `name` 時自動命名為 `ignore_1`、`filter_1`、`use_1`…;名稱重複會丟 `ArgumentError`。
|
|
122
|
+
|
|
123
|
+
## 4. 內建規則
|
|
124
|
+
|
|
125
|
+
| 規則名 | 種類 | 預設 | 參數(預設值) | 作用 |
|
|
126
|
+
| --- | --- | --- | --- | --- |
|
|
127
|
+
| `:blocklist` | notice | **開** | `source: :rails_filter_parameters`、`keys: []`、`authorization: true`、`filter_parameters: nil` | 遮掉 params/session/headers/context 裡的敏感 key(Airbrake `KeysBlocklist`) |
|
|
128
|
+
| `:referer_blocklist` | notice | **開** | 無(沿用 `:blocklist` 的 keys) | 遮掉 `context.referer` query string 裡的敏感 key(例如 `reset_password_token`) |
|
|
129
|
+
| `:signal_exception` | notice | **開** | `exceptions: ["SignalException"]` | 部署/重啟時的 `SIGTERM` 不報 |
|
|
130
|
+
| `:invalid_mime` | notice | **開** | `exceptions: ["ActionDispatch::Http::MimeNegotiation::InvalidType", "Mime::Type::InvalidMimeType"]` | 爬蟲亂送 `Accept`/`Content-Type` 不報 |
|
|
131
|
+
| `:retryable_jobs` | notice | **開**(有載入 `Airbrake::Sidekiq` 才掛) | `max_retries: nil`(nil = 用 job 自己的 `retry`) | Sidekiq 重試期間不報,次數用完才報 |
|
|
132
|
+
| `:performance_threshold` | performance | **開** | `threshold_ms: nil`(nil = 用 `c.performance_threshold_ms`) | 效能事件低於門檻不上報 |
|
|
133
|
+
| `:web_not_found` | notice | 關 | `statuses: [404]`、`web_only: true`、`except: nil` | 網頁請求裡 Rails 回 404 的例外不報(依 `rescue_responses`,含專案自己加的對應) |
|
|
134
|
+
| `:current_session` | notice | 關 | `with:`(**必填**,`-> { Hash or String }`)、`key: :current_session` | 把登入者資訊放到 `notice[:context][key]`;lambda 出錯就不附、照常通報 |
|
|
135
|
+
| `:proxy_timeout` | performance | 關 | `threshold_ms:`(**必填**)、`error_class:`(**必填**)、`via: :performance_filter` | request 用時超過 proxy timeout 時,另發一筆 `error_class` 錯誤通報(需 `performance_stats` 開著) |
|
|
136
|
+
|
|
137
|
+
### `:blocklist` 細節
|
|
138
|
+
|
|
139
|
+
- `source: :rails_filter_parameters`:讀 `Rails.application.config.filter_parameters`,**延後到第一次通報才讀**(initializer 比 `filter_parameter_logging.rb` 早載入)。
|
|
140
|
+
- `source: :static`:只用 `keys`。
|
|
141
|
+
- `keys`:Regexp 原樣;String/Symbol 轉成**不分大小寫的部分比對**(Airbrake 原生對 String/Symbol 是完全相等,`:passw` 擋不到 `password`)。
|
|
142
|
+
- `authorization: true`:補上 `/authorization/i`(`filter_parameters` 通常沒有涵蓋 `Authorization` header)。
|
|
143
|
+
- `filter_parameters:`:取代 Rails 的 `filter_parameters` 來源(陣列或 lambda),通常只有測試會用到。
|
|
144
|
+
|
|
145
|
+
```ruby
|
|
146
|
+
c.rule :blocklist, keys: [:card_number, /\Acvv\z/i]
|
|
147
|
+
c.rule :blocklist, source: :static, authorization: false, keys: [/password/i, /token/i]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### `:web_not_found` 細節
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
c.rule :web_not_found # 網頁 404 不報;job 裡的 RecordNotFound 照報
|
|
154
|
+
c.rule :web_not_found, statuses: [404, 422]
|
|
155
|
+
c.rule :web_not_found, except: ->(notice) { notice.stash[:rack_request].path.start_with?("/api/") }
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`ActionDispatch::MissingController` 這類 Rails 預設沒有對應 404 的例外,專案在 `config.action_dispatch.rescue_responses` 加上 `:not_found` 之後就會一起被忽略。
|
|
159
|
+
|
|
160
|
+
### `:proxy_timeout` 細節
|
|
161
|
+
|
|
162
|
+
```ruby
|
|
163
|
+
class RequestOverProxyTimeout < StandardError; end
|
|
164
|
+
|
|
165
|
+
c.performance_stats = true
|
|
166
|
+
c.rule :proxy_timeout, threshold_ms: 180_000, error_class: RequestOverProxyTimeout
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
通報內容:訊息 `用時 181,234ms (超過 kamal proxy timeout 180s)`;`context` 帶 `httpMethod`、`route`、`url`(含網域)、`responseType`;`params` 帶 `timing_ms`、`db_ms`、`view_ms`、`request_time`。只看 `Airbrake::PerformanceBreakdown`(沒碰 DB 也沒 render 的 request 不會產生它)。
|
|
170
|
+
|
|
171
|
+
## 5. `c.ignore`:宣告式忽略
|
|
172
|
+
|
|
173
|
+
```ruby
|
|
174
|
+
c.ignore(*exceptions, # 例外類別或名稱字串,可多個;不給 = 任何例外
|
|
175
|
+
status: 404, # Integer/Array:Rails 對這個例外回的 HTTP status
|
|
176
|
+
web: true, # true = 只管網頁請求;false = 只管 job/背景
|
|
177
|
+
controller: "Admin::BaseController", # controller 實例的類別或祖先(字串/類別/陣列)
|
|
178
|
+
controller_path: %r{\Aadmin/}, # String = 前綴;Regexp;Array
|
|
179
|
+
path: %r{\A/api/}, # request path:String = 前綴;Regexp;Array
|
|
180
|
+
host: /\Aapi\./, # request host:String = 完全相等;Regexp;Array
|
|
181
|
+
message: /Pattern/, # 例外訊息:String = 包含;Regexp;Array
|
|
182
|
+
job: "SyncXxxJob", # Sidekiq/ActiveJob 類別名稱:String = 完全相等;Regexp;類別
|
|
183
|
+
if: ->(notice) { ... }, # 回 true 才忽略(也可以直接給 block)
|
|
184
|
+
unless: ->(notice) { ... }, # 回 true 就不忽略(白名單)
|
|
185
|
+
name: :admin_not_found, # 顯示在 explain/log 的名稱
|
|
186
|
+
weight: 0) # Airbrake filter 的 weight
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
- 條件之間是 **AND**;同一個條件給陣列是 **OR**。
|
|
190
|
+
- 例外與 controller **建議給字串**:比對的是類別與祖先的名稱,不會觸發 autoload(initializer 跑的時候 `Admin::BaseController` 可能還沒載入)。
|
|
191
|
+
- 不認得的條件名稱會在設定當下丟 `ArgumentError`。
|
|
192
|
+
|
|
193
|
+
範例:
|
|
194
|
+
|
|
195
|
+
```ruby
|
|
196
|
+
# landing PR #2344:後台的 RecordNotFound 不報;金流回呼(admin/payment 不繼承 Admin::BaseController)、
|
|
197
|
+
# system/(繼承 Admin::BaseController 但 path 不是 admin/)、前台、job 照報
|
|
198
|
+
c.ignore "ActiveRecord::RecordNotFound", name: :admin_not_found,
|
|
199
|
+
controller: "Admin::BaseController", controller_path: %r{\Aadmin/}
|
|
200
|
+
|
|
201
|
+
# 某個 job 的 Timeout 不報
|
|
202
|
+
c.ignore "Timeout::Error", job: "SyncInventoryJob"
|
|
203
|
+
|
|
204
|
+
# 多個例外、訊息比對
|
|
205
|
+
c.ignore "Net::ReadTimeout", "Net::OpenTimeout", message: ["api.example.com", /sandbox/]
|
|
206
|
+
|
|
207
|
+
# API 以外的 404
|
|
208
|
+
c.ignore status: 404, web: true, unless: ->(notice) { notice.stash[:rack_request].path.start_with?("/api/") }
|
|
209
|
+
|
|
210
|
+
# 用 block 寫其他條件
|
|
211
|
+
c.ignore("ActionController::InvalidAuthenticityToken", name: :bot_csrf) do |notice|
|
|
212
|
+
notice.stash[:rack_request].user_agent.to_s =~ /bot/i
|
|
213
|
+
end
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## 6. 自訂規則
|
|
217
|
+
|
|
218
|
+
### `c.filter`/`c.performance_filter`
|
|
219
|
+
|
|
220
|
+
```ruby
|
|
221
|
+
c.filter(:tag_tenant) do |notice|
|
|
222
|
+
notice[:context][:tenant] = Current.tenant&.slug
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
c.performance_filter(:skip_health) do |metric|
|
|
226
|
+
metric.ignore! if metric.respond_to?(:route) && metric.route == "/up(.:format)"
|
|
227
|
+
end
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### `c.use`:掛一個物件或類別
|
|
231
|
+
|
|
232
|
+
```ruby
|
|
233
|
+
class NoisyHostFilter
|
|
234
|
+
def initialize(hosts:)
|
|
235
|
+
@hosts = hosts
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def call(notice)
|
|
239
|
+
request = notice.stash[:rack_request]
|
|
240
|
+
notice.ignore! if request && @hosts.include?(request.host)
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
c.use NoisyHostFilter, name: :noisy_host, hosts: %w[staging.example.com] # 類別:用 options new 出來
|
|
245
|
+
c.use NoisyHostFilter.new(hosts: %w[x.com]), name: :noisy_host_2 # 物件:直接掛
|
|
246
|
+
c.use ->(metric) { metric.ignore! if metric.timing < 10 }, kind: :performance, name: :tiny
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### `SuperAirbrake::Rule` + `register`:做成可開關的具名規則
|
|
250
|
+
|
|
251
|
+
```ruby
|
|
252
|
+
# app/lib/airbrake_rules/noisy_host.rb
|
|
253
|
+
class NoisyHost < SuperAirbrake::Rule
|
|
254
|
+
rule_name :noisy_host
|
|
255
|
+
defaults hosts: []
|
|
256
|
+
# kind :performance # 效能事件規則
|
|
257
|
+
# enabled_by_default true # register 後不用 c.rule 就開
|
|
258
|
+
# required_options :hosts # 必填參數
|
|
259
|
+
|
|
260
|
+
def call(notice)
|
|
261
|
+
request = notice.stash[:rack_request]
|
|
262
|
+
notice.ignore! if request && options[:hosts].include?(request.host)
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
SuperAirbrake.register(NoisyHost)
|
|
267
|
+
|
|
268
|
+
SuperAirbrake.configure do |c|
|
|
269
|
+
c.rule :noisy_host, hosts: %w[x.com]
|
|
270
|
+
end
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
需要在掛上去之前拿 notifier 或其他規則的資料時,覆寫 `build(installer)`(回傳要掛的 filter;回 `nil` 表示不掛)。
|
|
274
|
+
|
|
275
|
+
### `c.replace`:換掉內建規則
|
|
276
|
+
|
|
277
|
+
```ruby
|
|
278
|
+
c.replace :signal_exception, MySignalRule # Rule 子類別:沿用原本的 options
|
|
279
|
+
c.replace :web_not_found, ->(notice) { ... } # 任何 respond_to?(:call) 的物件
|
|
280
|
+
c.replace(:current_session) { |notice| notice[:context][:user] = "system" }
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### `c.after_install`
|
|
284
|
+
|
|
285
|
+
規則都掛好之後執行,例如訂閱 `ActiveSupport::Notifications`:
|
|
286
|
+
|
|
287
|
+
```ruby
|
|
288
|
+
c.after_install do |notifier|
|
|
289
|
+
ActiveSupport::Notifications.subscribe("process_action.action_controller") { |*args| ... }
|
|
290
|
+
end
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## 7. 三個專案的 initializer 範例
|
|
294
|
+
|
|
295
|
+
以下是「行為零變更」的遷移寫法(遷移 PR 另外開,這個 gem PR 不改任何專案)。
|
|
296
|
+
|
|
297
|
+
### landing(一頁商店)
|
|
298
|
+
|
|
299
|
+
```ruby
|
|
300
|
+
require "super_airbrake"
|
|
301
|
+
|
|
302
|
+
SuperAirbrake.configure do |c|
|
|
303
|
+
c.project_id = Figaro.env.AIRBRAKE_PROJECT_ID
|
|
304
|
+
c.project_key = Figaro.env.AIRBRAKE_PROJECT_KEY
|
|
305
|
+
c.ignore_environments = %i[development test staging]
|
|
306
|
+
c.performance_stats = (Figaro.env.domain == "app.super-landing.com")
|
|
307
|
+
c.performance_threshold_ms = 1500
|
|
308
|
+
c.query_stats = :skip_fast # 取代 patch_airbrake_skip_fast_query.rb
|
|
309
|
+
|
|
310
|
+
# 靜態清單(今天的 AIRBRAKE_BLOCKLIST_KEYS);不補 authorization 以維持現狀
|
|
311
|
+
c.rule :blocklist, source: :static, authorization: false, keys: [
|
|
312
|
+
/password/i, /token/i, /secret/i, /access_?key/i, /key\z/i, /hash_?(iv|[ab]\d)/i, /check_?code/i,
|
|
313
|
+
/\AHTTP_COOKIE\z/, /\Awarden\./, /\Asession_id\z/, /\Aemail\z/, /\Alogin\z/
|
|
314
|
+
]
|
|
315
|
+
|
|
316
|
+
c.disable :invalid_mime # 今天 landing 沒有,之後評估開啟
|
|
317
|
+
c.disable :retryable_jobs # individual_partial 有 retry: 5,開了會吞掉重試期間的通報
|
|
318
|
+
|
|
319
|
+
# 嚴格零變更:今天 landing 沒送 app_version
|
|
320
|
+
c.app_version = nil
|
|
321
|
+
c.sync_revision_env = false
|
|
322
|
+
|
|
323
|
+
# PR #2344:後台 RecordNotFound 不通報
|
|
324
|
+
c.ignore "ActiveRecord::RecordNotFound", name: :admin_not_found,
|
|
325
|
+
controller: "Admin::BaseController", controller_path: %r{\Aadmin/}
|
|
326
|
+
|
|
327
|
+
# 留在 landing:Notifications 版 proxy timeout(50 秒抓 backtrace)
|
|
328
|
+
c.after_install do
|
|
329
|
+
ActiveSupport::Notifications.subscribe("start_processing.action_controller") do |*args|
|
|
330
|
+
RequestOverProxyTimeout.watch(ActiveSupport::Notifications::Event.new(*args).payload)
|
|
331
|
+
end
|
|
332
|
+
ActiveSupport::Notifications.subscribe("process_action.action_controller") do |*args|
|
|
333
|
+
RequestOverProxyTimeout.check(ActiveSupport::Notifications::Event.new(*args))
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### xdelivery(火箭快遞)
|
|
340
|
+
|
|
341
|
+
```ruby
|
|
342
|
+
require "super_airbrake"
|
|
343
|
+
|
|
344
|
+
# 與 config/deploy.production.yml 的 proxy response_timeout 同步
|
|
345
|
+
class RequestOverProxyTimeout < StandardError
|
|
346
|
+
THRESHOLD_MS = 180_000
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
SuperAirbrake.configure do |c|
|
|
350
|
+
c.project_id = Figaro.env.AIRBRAKE_PROJECT_ID
|
|
351
|
+
c.project_key = Figaro.env.AIRBRAKE_PROJECT_KEY
|
|
352
|
+
c.ignore_environments = %i[development test staging]
|
|
353
|
+
c.timeout = 10
|
|
354
|
+
c.performance_stats = true
|
|
355
|
+
c.performance_threshold_ms = 4000
|
|
356
|
+
|
|
357
|
+
c.rule :web_not_found
|
|
358
|
+
c.rule :proxy_timeout, threshold_ms: RequestOverProxyTimeout::THRESHOLD_MS, error_class: RequestOverProxyTimeout
|
|
359
|
+
c.rule :current_session, with: -> {
|
|
360
|
+
user = Current.user
|
|
361
|
+
user ? { user_id: user.id, email: user.email, username: user.username, nickname: user.nickname } : "Unknown User"
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
c.disable :referer_blocklist # 今天沒有;開了是資安加分,另案處理
|
|
365
|
+
end
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### EIP
|
|
369
|
+
|
|
370
|
+
```ruby
|
|
371
|
+
require "super_airbrake"
|
|
372
|
+
|
|
373
|
+
SuperAirbrake.configure do |c|
|
|
374
|
+
c.project_id = Setting.airbrake_project_id if Setting.airbrake_project_id?
|
|
375
|
+
c.project_key = Setting.airbrake_project_key if Setting.airbrake_project_key?
|
|
376
|
+
c.performance_stats = true
|
|
377
|
+
c.performance_threshold_ms = 1500
|
|
378
|
+
c.query_stats = false # ActiveRecordSubscriber 對每句 SQL 呼叫 Kernel.caller 太貴
|
|
379
|
+
|
|
380
|
+
c.rule :web_not_found
|
|
381
|
+
c.rule :current_session, with: -> {
|
|
382
|
+
if (user = Current.user)
|
|
383
|
+
employee = user.employee
|
|
384
|
+
{ user_id: user.id, email: user.email, no: employee.employee_no, fullname: employee.fullname,
|
|
385
|
+
dept_name: employee.dept_name, title: employee.dept_title }
|
|
386
|
+
else
|
|
387
|
+
"Unknow User"
|
|
388
|
+
end
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
c.disable :referer_blocklist # 同 xdelivery
|
|
392
|
+
end
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
## 8. 除錯:rules/explain
|
|
396
|
+
|
|
397
|
+
```ruby
|
|
398
|
+
SuperAirbrake.configured? # => true
|
|
399
|
+
SuperAirbrake.rules
|
|
400
|
+
# => [{ name: :blocklist, kind: :notice, enabled: true, source: :builtin, options: {...} }, ...]
|
|
401
|
+
puts SuperAirbrake.explain
|
|
402
|
+
# SuperAirbrake 規則(依註冊順序):
|
|
403
|
+
# [開] blocklist notice builtin source=:static, keys=[...], authorization=false
|
|
404
|
+
# [關] invalid_mime notice builtin exceptions=[...]
|
|
405
|
+
# [開] admin_not_found notice custom matcher=ActiveRecord::RecordNotFound controller="Admin::BaseController" ...
|
|
406
|
+
# 設定:performance_threshold_ms=1500, query_stats=:skip_fast, ...
|
|
407
|
+
puts SuperAirbrake.config.performance_threshold_ms
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
`source` 的值:`builtin`(內建)、`registered`(專案 `register` 的)、`replaced`(被 `replace` 換掉)、`custom`(`ignore`/`filter`/`use`)。
|
|
411
|
+
|
|
412
|
+
## 9. 測試
|
|
413
|
+
|
|
414
|
+
`super_airbrake/testing` 提供不碰全域 `Airbrake` 的 notifier,用 airbrake-ruby 真的 `FilterChain` 跑規則(含 weight 排序):
|
|
415
|
+
|
|
416
|
+
```ruby
|
|
417
|
+
require "super_airbrake/testing"
|
|
418
|
+
|
|
419
|
+
notifier = SuperAirbrake::Testing.install do |c|
|
|
420
|
+
c.rule :web_not_found
|
|
421
|
+
c.ignore "ActiveRecord::RecordNotFound", controller: "Admin::BaseController"
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
notice = notifier.build_notice(ActiveRecord::RecordNotFound.new, controller: Admin::OrdersController.new)
|
|
425
|
+
assert notifier.refine(notice).ignored?
|
|
426
|
+
|
|
427
|
+
notice = notifier.build_notice(RuntimeError.new("boom"), params: { job: { "class" => "SyncJob" } })
|
|
428
|
+
refute notifier.refine(notice).ignored?
|
|
429
|
+
|
|
430
|
+
notifier.filter_names # 掛上去的 filter 名稱
|
|
431
|
+
notifier.super_airbrake_config.explain # 這份設定的 explain
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
- 想讓規則錯誤直接拋出:`c.safe = false`。
|
|
435
|
+
- 要測真正的 `SuperAirbrake.configure`:`SuperAirbrake.configure(notifier: fake, airbrake_config: Airbrake::Config.new) { ... }`,測完呼叫 `SuperAirbrake.reset!`(只清設定,已掛到 notifier 的 filter 不會拿掉)。
|
|
436
|
+
|
|
437
|
+
在 super_tools 裡跑 SuperAirbrake 自己的測試:
|
|
438
|
+
|
|
439
|
+
```bash
|
|
440
|
+
# 現代環境(與其他工具同一組 gemfile)
|
|
441
|
+
BUNDLE_GEMFILE=gemfiles/ruby_3.4_rails_8.1.gemfile bundle exec rake super_airbrake
|
|
442
|
+
|
|
443
|
+
# 舊環境(對齊 landing:Ruby 2.6.7 / Rails 5.2.8 / airbrake 13.0.1 / airbrake-ruby 6.1.0 / Sidekiq 5.2.8)
|
|
444
|
+
BUNDLE_GEMFILE=gemfiles/super_airbrake_legacy.gemfile bundle exec rake super_airbrake
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## 10. 相容性與 landing(2.x 線)
|
|
448
|
+
|
|
449
|
+
| 環境 | Ruby | Rails | airbrake/airbrake-ruby | 狀態 |
|
|
450
|
+
| --- | --- | --- | --- | --- |
|
|
451
|
+
| landing | 2.6.7 | 5.2.8 | 13.0.1/6.1.0 | CI `super_airbrake_legacy` job |
|
|
452
|
+
| super_tools CI 矩陣 | 2.7 ~ 3.4 | 6.1 ~ 8.1 | 13.x/6.x | CI `test` job(`rake t super_airbrake`) |
|
|
453
|
+
| xdelivery/EIP | 4.0.10 | 8.1 | 13.0.5/6.2.x | 本機驗證 |
|
|
454
|
+
|
|
455
|
+
- `lib/super_airbrake/` 用 Ruby 2.6 相容語法,只依賴 `airbrake`、`active_support`(`actionpack` 有載入時才用 `ExceptionWrapper` 判斷 status),不依賴 super_tools 的其他模組。
|
|
456
|
+
- super_tools 4.x 要求 Ruby >= 2.7、Rails >= 6.1,**landing 不能直接升到 4.x**。landing 的用法是 2.x 回植線:從 landing 目前鎖的 `feat-beyond-modal`(2.0.0)開 `release-2.x`,把 `lib/super_airbrake.rb`、`lib/super_airbrake/`、測試與 `gemfiles/super_airbrake_legacy.gemfile` **原封不動**搬過去,版號 2.1.0 並打 tag `v2.1.0`,landing 的 Gemfile 改成 `tag: "v2.1.0"`。兩條線的 `lib/super_airbrake/` 保持 byte-for-byte 相同。
|
|
457
|
+
|
|
458
|
+
## 11. Airbrake filter 執行順序(進階)
|
|
459
|
+
|
|
460
|
+
- airbrake-ruby 的 `FilterChain` 依 `weight` 由大到小執行;遇到第一個 `ignore!` 就停。
|
|
461
|
+
- Airbrake 自己的 filter(gem root、git 資訊、context、exception attributes…)weight 約 95 ~ 130,先跑。
|
|
462
|
+
- SuperAirbrake 的規則預設 `weight: 0`(`c.ignore`/`c.filter`/`c.use` 都可以給 `weight:`)。airbrake-ruby 對**同 weight** 的 filter 不保證先後,規則之間有先後需求時請用不同的 weight(大的先跑)。
|
|
463
|
+
- `:blocklist` 用的是 Airbrake `KeysBlocklist`(weight -110),**永遠最後跑**,所以其他規則看得到原值,送出前才遮罩。
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# SuperAirbrake 的舊環境測試:對齊一頁商店(landing)的 Ruby 2.6.7/Rails 5.2.8/airbrake 13.0.1
|
|
2
|
+
# 不使用 gemspec:super_tools 4.x 要求 Ruby >= 2.7,這裡只測 lib/super_airbrake(不載入其他工具)
|
|
3
|
+
# 執行:BUNDLE_GEMFILE=gemfiles/super_airbrake_legacy.gemfile bundle exec rake super_airbrake
|
|
4
|
+
|
|
5
|
+
source "https://rubygems.org"
|
|
6
|
+
|
|
7
|
+
gem "rake", "~> 13.0"
|
|
8
|
+
gem "minitest", "~> 5.15.0"
|
|
9
|
+
gem "railties", "5.2.8"
|
|
10
|
+
gem "actionpack", "5.2.8"
|
|
11
|
+
gem "activesupport", "5.2.8"
|
|
12
|
+
gem "airbrake", "13.0.1"
|
|
13
|
+
gem "airbrake-ruby", "6.1.0"
|
|
14
|
+
gem "sidekiq", "5.2.8"
|
|
15
|
+
gem "rbtree3", "0.7.0" # 與 landing 的 Gemfile.lock 相同;0.7.1 在 Ruby 2.6 編譯失敗
|