安装
安装跟踪软件包:
ESM
# Using yarn yarn add @sentry/tracing # Using npm npm install @sentry/tracing
CDN
<script <!-- Note that `bundle.tracing.min.js` contains both `@sentry/browser` AND `@sentry/tracing`, and should therefore be used in place of `@sentry/browser`'s bundle rather than in addition to it. --> src="https://browser.sentry-cdn.com/5.29.2/bundle.tracing.min.js" integrity="sha384-4zxA5Bnxor/VkZae20EqPP3A/6vDlw1ZhqF7EvpmeTfWYFjPIDdaUSOk/q7G/bYw" crossorigin="anonymous" ></script>
配置
通过以下两种方式在您的应用中启用性能监控:
- 使用 SDK 配置中的
tracesSampleRate
选项将所有transactions
的统一采样率设置为0
到1
之间的数字。(例如,要发送20%
的transactions
,请将tracesSampleRate
设置为0.2
。) - 通过为
tracesSampler
配置选项提供功能,基于 transaction 本身及其捕获的上下文动态控制采样率。
ESM
// If you're using one of our integration packages, like `@sentry/react` or // `@sentry/angular`, substitute its name for `@sentry/browser` here import * as Sentry from "@sentry/browser"; // If taking advantage of automatic instrumentation (highly recommended) import { Integrations as TracingIntegrations } from "@sentry/tracing"; // Or, if only manually tracing // import * as _ from "@sentry/tracing" // Note: You MUST import the package in some way for tracing to work Sentry.init({ dsn: "https://examplePublicKey@o0.ingest.sentry.io/0", // This enables automatic instrumentation (highly recommended), but is not // necessary for purely manual usage integrations: [new TracingIntegrations.BrowserTracing()], // To set a uniform sample rate tracesSampleRate: 0.2 // Alternatively, to control sampling dynamically tracesSampler: samplingContext => { ... } });
CDN
Sentry.init({ dsn: "https://examplePublicKey@o0.ingest.sentry.io/0", // This enables automatic instrumentation (highly recommended), but is not // necessary for purely manual usage integrations: [new Sentry.Integrations.BrowserTracing()], // To set a uniform sample rate tracesSampleRate: 0.2 // Alternatively, to control sampling dynamically tracesSampler: samplingContext => { ... } });
如果设置了这些选项之一,则将在您的应用程序中启用跟踪。虽然这些选项是互斥的,但是如果您同时设置了这两个选项,tracesSampler
将具有优先权。您可以在 Sampling Transactions 中了解有关它们如何工作的更多信息。
验证
首次启用跟踪时,通过将 tracesSampleRate
设置为 1.0
来验证其是否正常运行,因为这可以确保将每个事务发送到 Sentry。
一旦测试完成,我们建议在生产中降低这个值,方法是降低您的 tracesSampleRate
值,或者切换到使用 tracesSampler
来动态取样和过滤您的 transaction。
在没有采样的情况下,我们的自动检测将在任何用户加载任何页面或在应用程序中的任何位置导航时发送 transaction。那是很多 transactions!采样可以实现代表性数据,而无需占用系统或 Sentry transaction 配额。