设置追踪
了解如何在您的应用程序中启用追踪。
通过 追踪,Sentry 跟踪您的软件性能,测量吞吐量和延迟等指标,并显示错误对多个系统的影響。Sentry 捕获由事务和跨度组成的分布式跟踪,这些事务和跨度分别测量各个服务及其内部的各个操作。更多关于我们的模型信息,请参阅 分布式追踪。
首先,启用追踪并配置事务的采样率。通过以下方式之一设置事务的采样率:
- 使用 SDK 配置中的
tracesSampleRate
选项为所有事务设置统一的采样率,该值应介于0
和1
之间。(例如,要发送 20% 的事务,将tracesSampleRate
设置为0.2
。) - 通过提供一个函数给
tracesSampler
配置选项,根据事务本身及其捕获上下文来控制采样率。
这两个选项是互斥的。如果同时设置了两者,则以 tracesSampler
为准。
Copied
// If you're using one of our framework SDK packages, like `@sentry/react`,
// substitute its name for `@sentry/browser` here
import * as Sentry from "@sentry/browser";
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
// This enables automatic instrumentation (highly recommended),
// but is not necessary for purely manual usage
// If you only want to use custom instrumentation:
// * Remove the `BrowserTracing` integration
// * add `Sentry.addTracingExtensions()` above your Sentry.init() call
integrations: [Sentry.browserTracingIntegration()],
// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
// Set `tracePropagationTargets` to control for which URLs trace propagation should be enabled
tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/],
});
了解更多关于追踪的 选项,如何使用 tracesSampler 函数,或如何 采样事务。
通过使用我们的 自动 instrumentation 或通过使用 自定义 instrumentation 启动和结束事务,来验证追踪是否正常工作。
在测试期间,将 tracesSampleRate
设置为 1.0
,以确保每个事务都会发送到 Sentry。测试完成后,您可能希望设置一个较低的 tracesSampleRate
值,或切换到使用 tracesSampler
,根据上下文数据有选择地采样和过滤事务。
如果您将采样率保持在 1.0
,每次用户加载页面或在应用程序内导航时都会发送一个事务。根据您的应用程序流量,这可能会导致大量的事务。如果您有一个高负载的后端应用程序,您可能需要考虑设置一个较低的 tracesSampleRate
值,或切换到使用 tracesSampler
,根据上下文数据有选择地采样和过滤事务。