使用者體驗監控相容 Sentry SDK 支援上報以下類型的資料:
自動採集 — 頁面載入(包含 Webvitals 指標)、資源請求(Resource)、卡頓(LongTask)、行為事件(Action)。
Custom Event(自訂事件) — 追蹤商務程序與關鍵操作。
Custom Log(自訂日誌) — 記錄業務日誌與關鍵節點資訊。
Exception(自訂異常/錯誤) — 捕獲並上報 JS 異常。
User(使用者身份) — 關聯使用者資訊,按使用者維度檢索分析。
一、SDK 初始化推薦配置
要獲得儘可能完整的 RUM 資料,需要在 Sentry.init 時開啟以下配置。
原生 JavaScript (Browser) 專案樣本
import * as Sentry from "@sentry/browser";
Sentry.init({
dsn: "https://key@<YOUR-ENDPOINT>/0",
// ─── 基礎配置 ───
release: "my-web-app@1.2.0",
environment: "production",
// ─── Performance Tracing(效能追蹤) ───
tracesSampleRate: 1.0, // 生產環境建議按需調整,例如 0.2
integrations:[
// 自動採集 pageload / navigation / resource / Web Vitals 等
Sentry.browserTracingIntegration(),
],
});Android 專案完整樣本 (Kotlin)
import android.app.Application
import io.sentry.android.core.SentryAndroid
import io.sentry.android.core.SentryAndroidOptions
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
SentryAndroid.init(this) { options ->
options.dsn = "https://key@<YOUR-ENDPOINT>/0"
// ─── 基礎配置 ───
options.release = "my-android-app@1.2.0"
options.environment = "production"
// ─── Performance Tracing(效能追蹤) ───
options.tracesSampleRate = 1.0
}
}
}Android 補充提示: 對於網路請求(Resource 事件),如果您使用的是 OkHttp 或 Retrofit,必須在構建 OkHttpClient 時添加 SentryOkHttpInterceptor(),否則無法自動採集介面請求耗時。
iOS (Swift) 專案完整樣本
import UIKit
import Sentry
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
SentrySDK.start { options in
options.dsn = "https://key@<YOUR-ENDPOINT>/0"
// ─── 基礎配置 ───
options.environment = "production"
options.releaseName = "my-ios-app@1.2.0"
// ─── Performance Tracing(效能追蹤) ───
options.tracesSampleRate = 1.0
}
return true
}
}關鍵配置項說明
配置項 | 推薦值 | 對應 RUM 資料 | 說明 |
|
| View, Resource, LongTask, Action, Custom Event | 效能追蹤採樣率,必須設定 > 0 才能採集效能資料。 |
| 啟用 | View(pageload/navigation), Resource, LongTask, Web Vitals | 自動建立頁面載入和路由跳轉的 transaction,採集 FCP/LCP/FID/CLS/INP 等指標。 |
| 版本號碼字串 | 所有事件 | 應用版本號碼,用於版本維度分析和 SourceMap 關聯。 |
|
| 所有事件 | 環境標識,用於區分生產/測試環境。 |
自動採集的 RUM 資料總覽
開啟上述配置後,以下資料會被自動採集,無需額外代碼:
RUM 事件類型 | 資料來源 | 說明 |
View |
| 頁面載入和路由切換,包含 Web Vitals(FP, FCP, LCP, FID, CLS, INP, TTFB) |
Resource | transaction 內 | XHR/Fetch 請求、靜態資源(JS/CSS/圖片等)載入 |
LongTask |
或 Vue/React 組件渲染(≥ 50ms) | 主線程阻塞任務 |
Action |
| 使用者點擊、輸入、滾動等互動操作,含 INP |
Application |
| 應用生命週期事件(前台/後台切換等,移動端情境) |
不需要開啟的配置
配置項 | 說明 |
| Session Replay 資料當前不落盤,無需開啟 |
| Profiling 資料當前不落盤,無需開啟 |
| 僅影響 |
二、Custom Event — 自訂事件(手動上報)
適用於追蹤商務程序、關鍵操作的耗時和狀態,例如支付流程、表單提交、搜尋請求等。
使用方式
通過 Sentry.startSpan 上報,op 固定為 rum.custom:
import * as Sentry from "@sentry/vue"; // 或 @sentry/react、@sentry/browser 等
// 基礎用法
Sentry.startSpan(
{
op: "rum.custom",
name: "支付流程",
attributes: {
"custom.group": "payment",
"custom.value": "99.9",
},
},
() => {
// 商務邏輯...
}
);
// 攜帶自訂業務屬性
Sentry.startSpan(
{
op: "rum.custom",
name: "商品搜尋",
attributes: {
"custom.group": "search",
"custom.value": "42",
// 以下屬性會自動落入 properties,可在控制台查詢
"keyword": "藍芽耳機",
"result_count": 42,
"search_type": "full_text",
},
},
() => {
// 搜尋邏輯...
}
);非同步情境
如果商務邏輯是非同步,回呼函數返回 Promise 即可,SDK 會自動等待完成後計算耗時:
await Sentry.startSpan(
{
op: "rum.custom",
name: "上傳檔案",
attributes: {
"custom.group": "upload",
"file_type": "image",
"file_size": 1024000,
},
},
async () => {
await uploadFile(file);
}
);欄位說明
參數 | 是否必填 | 落盤欄位 | 說明 |
| 必填 | — | 固定為 |
| 必填 |
| 事件名稱,用於控制台展示和檢索 |
| 選填 |
| 事件分組,用於彙總統計(如 |
| 選填 |
| 事件值,用於數值分析(如金額、數量) |
| 選填 |
| 自訂業務屬性,支援 string / number / boolean |
自動計算 | — |
| 回呼函數執行耗時(毫秒),SDK 自動採集 |
三、Custom Log — 自訂日誌(手動上報)
適用於記錄業務日誌、關鍵節點資訊、非異常的警告訊息等。
使用方式
通過 Sentry.captureMessage 上報:
import * as Sentry from "@sentry/vue";
// 基礎用法(預設 level 為 "info")
Sentry.captureMessage("使用者完成了新手引導流程");
// 指定記錄層級
Sentry.captureMessage("支付重試超過最大次數", "warning");
欄位說明
參數 | 是否必填 | 落盤欄位 | 說明 |
| 必填 |
| 日誌內容 |
| 選填 |
| 記錄層級,支援 |
四、Exception — 異常/錯誤上報(手動上報)
適用於捕獲運行時異常、商務邏輯錯誤、Promise 拒絕等情境。落盤為 RUM 的 Exception 事件,包含錯誤類型、錯誤訊息和完整堆棧資訊,並自動進行錯誤彙總分組。
使用方式
通過 Sentry.captureException 上報:
import * as Sentry from "@sentry/vue";
// 基礎用法 — 捕獲一個 Error 對象
try {
riskyOperation();
} catch (err) {
Sentry.captureException(err);
}
// 手動構造 Error 上報
Sentry.captureException(new Error("支付介面返回異常狀態代碼"));
// 捕獲 Promise 異常
fetchOrder(orderId).catch((err) => {
Sentry.captureException(err);
});
落盤說明
captureException 上報的事件會產生以下 RUM 資料:
落盤模型 | 說明 |
ExceptionModel | 主體錯誤事件,包含錯誤類型( |
NodeStore | 儲存完整的原始 Sentry Event JSON,用於控制台詳情頁展示 |
GroupMessage | 錯誤彙總分組資訊,用於錯誤清單的彙總展示 |
注意事項
始終傳入 Error 對象:
captureException(new Error("msg"))比captureException("msg")更好,前者會包含完整堆棧資訊。與 captureMessage 的區別:
captureException走 Exception 鏈路,有堆棧和錯誤分組;captureMessage走 Custom Log 鏈路,是輕量日誌。不要用 captureMessage 上報錯誤:錯誤情境請使用
captureException,以便在控制台獲得完整的錯誤分析能力。
五、User — 使用者身份配置
通過 Sentry.setUser 設定目前使用者資訊後,後續上報的所有事件(Custom Event、Custom Log、Exception 等)都會自動攜帶使用者標識,用於在控制台按使用者維度檢索和分析。
使用方式
import * as Sentry from "@sentry/vue";
// 使用者登入後設定
Sentry.setUser({
id: "user-12345",
username: "張三",
data: {
vip_level: "gold",
department: "engineering",
},
});
// 使用者退出後清除
Sentry.setUser(null);
欄位說明
Sentry User 欄位 | 落盤欄位 | 說明 |
|
| 使用者唯一標識,推薦設定,用於控制台按使用者檢索 |
|
| 使用者名稱,用於控制台展示 |
|
| 自訂使用者屬性(如會員等級、部門等),用於多維分析 |
| — | 當前未落盤,不建議依賴此欄位 |
| — | 當前由服務端從 HTTP 要求頭自動採集,無需手動設定 |
注意事項
儘早調用
setUser:建議在使用者登入完成後立即設定,確保後續事件都能關聯到使用者。id是最重要的欄位:控制台按使用者維度檢索依賴此欄位,建議使用業務系統的使用者識別碼。data適合放業務屬性:如會員等級、使用者角色、所屬組織等,會以 JSON 格式儲存在userTags中。退出時清除使用者資訊:調用
Sentry.setUser(null)避免事件錯誤關聯到已退出的使用者。
六、選擇指引
情境 | 推薦方式 | 落盤類型 | 理由 |
追蹤某個操作的耗時 |
| Custom Event | 自動採集 duration,適合效能分析 |
記錄商務程序的關鍵節點 |
| Custom Event | 可攜帶 group/value/自訂屬性,適合彙總分析 |
記錄一條日誌訊息 |
| Custom Log | 輕量,帶記錄層級,適合日誌類情境 |
記錄非異常的警告或提示 |
| Custom Log | 語義清晰,區別於 captureException |
捕獲 JS 異常/執行階段錯誤 |
| Exception | 包含堆棧、錯誤分組、趨勢分析 |
捕獲 Promise 異常 |
| Exception | 同上,保留完整調用鏈 |
捕獲 商務邏輯錯誤(如介面 500) |
| Exception | 有堆棧定位,有彙總分組 |
七、注意事項
op必須為rum.custom:其他 op 值的獨立 Span 不會被轉換為自訂事件。name建議簡潔且可彙總:避免將變數(如使用者識別碼、訂單號)直接寫入 name,應放入 attributes。attributes 的 value 類型:支援
string、number、boolean,不支援對象或數組。captureMessage 不是 captureException:如果要上報錯誤,請使用
Sentry.captureException(new Error("...")),它會走 Exception 鏈路並包含堆棧資訊。captureException 始終傳入 Error 對象:
captureException(new Error("msg"))比captureException("msg")更好,前者包含完整堆棧。