Sentry-JS-SDK-Browser 官方示例最佳實踐

為少發表於2021-03-16

系列

SDK 開發

  1. 頂級開源專案 Sentry 20.x JS-SDK 設計藝術(理念與設計原則篇)
  2. 頂級開源專案 Sentry 20.x JS-SDK 設計藝術(開發基礎篇)
  3. 頂級開源專案 Sentry 20.x JS-SDK 設計藝術(概述篇)
  4. 頂級開源專案 Sentry 20.x JS-SDK 設計藝術(Unified API篇)

系列

  1. Snuba:Sentry 新的搜尋基礎設施(基於 ClickHouse 之上)
  2. Sentry 10 K8S 雲原生架構探索,Vue App 1 分鐘快速接入
  3. Sentry(v20.x)玩轉前/後端監控與事件日誌大資料分析,使用 Helm 部署到 K8S 叢集
  4. Sentry(v20.x) JavaScript SDK 三種安裝載入方式
  5. Sentry(v20.x) JavaScript SDK 配置詳解
  6. Sentry(v20.x) JavaScript SDK 手動捕獲事件基本用法
  7. Sentry(v20.x) JavaScript SDK Source Maps詳解
  8. Sentry(v20.x) JavaScript SDK 故障排除
  9. Sentry(v20.x) JavaScript SDK 1分鐘上手效能監控
  10. Sentry(v20.x) JavaScript SDK 效能監控之管理 Transactions
  11. Sentry(v20.x) JavaScript SDK 效能監控之取樣 Transactions
  12. Sentry(v20.x) JavaScript SDK Enriching Events(豐富事件資訊)
  13. Sentry(v20.x) JavaScript SDK Data Management(問題分組篇)

Sentry-Javascript

@sentry/browser

相關示例

  1. denyUrls
  2. allowUrls
  3. ignoreError message
  4. ignoreError type
  5. regularException
  6. captureException
  7. captureMessage
  8. duplicated exception
  9. duplicated message
  10. integration
  11. event hints
  12. breadcrumb hints

實踐

快速執行示例

# sentry-javascript 專案
yarn
yarn build

cd packages/browser/examples/
python -m SimpleHTTPServer # Mac 自帶的,也可以用你自己喜歡的 http server
# Serving HTTP on 0.0.0.0 port 8000 ...

訪問:http://localhost:8000/

示例配置詳解

Sentry.init({
  // Client's DSN.
  dsn: 'https://363a337c11a64611be4845ad6e24f3ac@sentry.io/297378',
  // An array of strings or regexps that'll be used to ignore specific errors based on their type/message
  ignoreErrors: [/PickleRick_\d\d/, 'RangeError'],
  // An array of strings or regexps that'll be used to ignore specific errors based on their origin url
  denyUrls: ['external-lib.js'],
  // An array of strings or regexps that'll be used to allow specific errors based on their origin url
  allowUrls: ['http://localhost:5000', 'https://browser.sentry-cdn'],
  // Debug mode with valuable initialization/lifecycle informations.
  debug: true,
  // Whether SDK should be enabled or not.
  enabled: true,
  // Custom integrations callback
  integrations(integrations) {
    return [new HappyIntegration(), ...integrations];
  },
  // A release identifier.
  release: '1537345109360',
  // An environment identifier.
  environment: 'staging',
  // Custom event transport that will be used to send things to Sentry
  transport: HappyTransport,
  // Method called for every captured event
  async beforeSend(event, hint) {
    // Because beforeSend and beforeBreadcrumb are async, user can fetch some data
    // return a promise, or whatever he wants
    // Our CustomError defined in errors.js has `someMethodAttachedToOurCustomError`
    // which can mimick something like a network request to grab more detailed error info or something.
    // hint is original exception that was triggered, so we check for our CustomError name
    if (hint.originalException.name === 'CustomError') {
      const serverData = await hint.originalException.someMethodAttachedToOurCustomError();
      event.extra = {
        ...event.extra,
        serverData,
      };
    }
    console.log(event);
    return event;
  },
  // Method called for every captured breadcrumb
  beforeBreadcrumb(breadcrumb, hint) {
    // We ignore our own logger and rest of the buttons just for presentation purposes
    if (breadcrumb.message.startsWith('Sentry Logger')) return null;
    if (breadcrumb.category !== 'ui.click' || hint.event.target.id !== 'breadcrumb-hint') return null;

    // If we have a `ui.click` type of breadcrumb, eg. clicking on a button we defined in index.html
    // We will extract a `data-label` attribute from it and use it as a part of the message
    if (breadcrumb.category === 'ui.click') {
      const label = hint.event.target.dataset.label;
      if (label) {
        breadcrumb.message = `User clicked on a button with label "${label}"`;
      }
    }
    console.log(breadcrumb);
    return breadcrumb;
  },
});
  • dsn:客戶端的DSN
  • ignoreErrors:字串或正規表示式陣列,用於根據其 type/message 忽略特定錯誤
  • denyUrls:字串或正規表示式陣列,將用於根據 origin url 忽略特定錯誤
  • allowUrls:字串或正規表示式陣列,將用於允許基於其 origin url 的特定錯誤
  • debug:使用有價值的初始化(initialization)/生命週期(lifecycle)資訊的除錯模式
  • enabled:是否應該啟用 SDK
  • integrations:自定義整合回撥
  • release:釋出版本識別符號
  • environment:釋出環境識別符號
  • transport:自定義事件傳輸,將用於將事物傳送到 Sentry
  • beforeSend:針對每個捕獲的事件呼叫的方法
  • beforeBreadcrumb:針對每個捕獲的麵包屑呼叫的方法
我是為少
微信:uuhells123
公眾號:黑客下午茶
加我微信(互相學習交流),關注公眾號(獲取更多學習資料~)

相關文章