前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Sentry-JS-SDK-Browser 官方示例最佳实践

Sentry-JS-SDK-Browser 官方示例最佳实践

作者头像
为少
发布2021-05-27 18:52:09
1.5K0
发布2021-05-27 18:52:09
举报
文章被收录于专栏:黑客下午茶

SDK 开发

  1. 顶级开源项目 Sentry 20.x JS-SDK 设计艺术(理念与设计原则篇)
  2. 顶级开源项目 Sentry 20.x JS-SDK 设计艺术(开发基础篇)
  3. 顶级开源项目 Sentry 20.x JS-SDK 设计艺术(概述篇)

系列

  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(问题分组篇)
  14. 顶级开源项目 Sentry 20.x JS-SDK 设计艺术(Unified API篇)

@sentry/browser

Sentry-Javascript

  • Official Sentry SDKs for JavaScript
    • https://github.com/getsentry/sentry-javascript
  • 示例代码
    • https://github.com/getsentry/sentry-javascript/tree/master/packages/browser/examples

相关示例

  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

官方示例

快速运行示例

代码语言:javascript
复制
# 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/

示例配置详解

代码语言:javascript
复制
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:针对每个捕获的面包屑调用的方法
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-03-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 黑客下午茶 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SDK 开发
  • 系列
  • @sentry/browser
    • Sentry-Javascript
      • 相关示例
      • 官方示例
        • 快速运行示例
          • 示例配置详解
          相关产品与服务
          应用性能监控
          应用性能监控(Application Performance Management,APM)是一款应用性能管理平台,基于实时多语言应用探针全量采集技术,为您提供分布式性能分析和故障自检能力。APM 协助您在复杂的业务系统里快速定位性能问题,降低 MTTR(平均故障恢复时间),实时了解并追踪应用性能,提升用户体验。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档