代码监控

为什么我们需要加前端线上代码监控

当我们的C端项目上线后,除了用户反馈和流量统计外是否还能做更多事情,类似于sentry的平台提供给我们的是捕捉线上前端的控制台报错信息,按浏览器、设备、受影响的用户或错误是否未处理等属性进行过滤。可以检查问题详细信息以更好地了解问题并有效地进行分类

image.png

image.png

创建项目


进入控制台创建项目

image.png

安装sentry

  1. # Using yarn
  2. yarn add @sentry/vue @sentry/tracing
  3. # Using npm
  4. npm install --save @sentry/vue @sentry/tracing

配置sentry

vue2

  1. import Vue from "vue";
  2. import Router from "vue-router";
  3. import * as Sentry from "@sentry/vue";
  4. import { BrowserTracing } from "@sentry/tracing";
  5. Vue.use(Router);
  6. const router = new Router({
  7. // ...
  8. });
  9. Sentry.init({
  10. Vue,
  11. dsn: "https://3bcbfa2b771443978f3843f7eeebb1d1@o317998.ingest.sentry.io/6707654",
  12. integrations: [
  13. new BrowserTracing({
  14. routingInstrumentation: Sentry.vueRouterInstrumentation(router),
  15. tracingOrigins: ["localhost", "my-site-url.com", /^\//],
  16. }),
  17. ],
  18. // Set tracesSampleRate to 1.0 to capture 100%
  19. // of transactions for performance monitoring.
  20. // We recommend adjusting this value in production
  21. tracesSampleRate: 1.0,
  22. });
  23. // ...
  24. new Vue({
  25. router,
  26. render: h => h(App),
  27. }).$mount("#app");

vue3

  1. import { createApp } from "vue";
  2. import { createRouter } from "vue-router";
  3. import * as Sentry from "@sentry/vue";
  4. import { BrowserTracing } from "@sentry/tracing";
  5. const app = createApp({
  6. // ...
  7. });
  8. const router = createRouter({
  9. // ...
  10. });
  11. Sentry.init({
  12. app,
  13. dsn: "https://3bcbfa2b771443978f3843f7eeebb1d1@o317998.ingest.sentry.io/6707654",
  14. integrations: [
  15. new BrowserTracing({
  16. routingInstrumentation: Sentry.vueRouterInstrumentation(router),
  17. tracingOrigins: ["localhost", "my-site-url.com", /^\//],
  18. }),
  19. ],
  20. tracesSampleRate: 1.0,
  21. });
  22. app.use(router);
  23. app.mount("#app");

个性化配置

  1. Sentry.init({
  2. app,
  3. release: "defipay-payment@" + import.meta.env.VITE_BASE_WEB_VERSION,
  4. environment: import.meta.env.MODE,
  5. dsn: "https://3bcbfa2b771443978f3843f7eeebb1d1@o317998.ingest.sentry.io/6707654",
  6. integrations: [
  7. new BrowserTracing({
  8. routingInstrumentation: Sentry.vueRouterInstrumentation(router),
  9. tracingOrigins: ["localhost","https://www.defipay.biz", "https://test.defipay.biz", /^\//],
  10. }),
  11. ],
  12. //请将tracesSampleRate您的选项设置Sentry.init()为 0 到 1 之间的数字。设置此选项后,创建的每笔交易都将有一定百分比的机会发送到 Sentry。
  13. tracesSampleRate: 1.0,
  14. });