设置追踪

了解如何在您的应用程序中启用追踪,并发现有价值的应用程序性能洞察。

通过 追踪,Sentry 跟踪您的软件性能,测量吞吐量和延迟等指标,并显示错误对多个系统的影响。Sentry 捕获由事务和跨度组成的分布式跟踪,这些事务和跨度分别测量各个服务及其内部的各个操作。了解更多关于我们的模型,请参阅 分布式追踪

如果您在一个高吞吐量环境中采用追踪,我们建议在部署前进行测试,以确保您的服务性能特征符合预期。

首先,启用追踪并配置事务的采样率。通过以下两种方式之一设置事务的采样率:

  • 使用 traces_sample_rate 选项在 SDK 配置中为所有事务设置一个统一的采样率,值为 01 之间的数字。(例如,要发送 20% 的事务,将 traces_sample_rate 设置为 0.2。)
  • 通过提供一个函数给 traces_sampler 配置选项,根据事务本身及其捕获上下文来控制采样率。

这两个选项是互斥的。如果同时设置了两者,traces_sampler 将优先生效。

Copied
\Sentry\init([
    'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0',
    // Specify a fixed sample rate:
    'traces_sample_rate' => 0.2,
    // Or provide a custom sampler:
    'traces_sampler' => function (\Sentry\Tracing\SamplingContext $context): float {
        // return a number between 0 and 1
    },
]);

了解更多关于追踪 选项,如何使用 traces_sampler 函数,或如何 采样事务

在测试时,将 traces_sample_rate 设置为 1.0,以确保每个事务都会发送到 Sentry。测试完成后,您可能希望设置一个较低的 traces_sample_rate 值,或者切换到使用 traces_sampler,根据上下文数据有选择地采样和过滤您的事务。

当您在 PHP 应用程序中使用性能功能时,响应时间会受到一定影响(具体取决于您配置的 traces_sample_rate)。

This is due to the nature of PHP, where requests are usually sent as part of the process in which you serve your users' response. This affects the time it takes to send a request to Sentry because it's added to your servers' response time.

To offset this and make the addition close to zero, run Relay locally on the same machine or a local network that can act as a proxy/agent.

这样做会使 PHP 进程将请求发送到本地 Relay,然后由 Relay 将其转发到 Sentry,而不是直接将请求发送到 Sentry。

要开始使用 Relay,请参阅我们的 入门文档。 我们建议使用 Relay 的 managed 模式(这是默认模式)。

按照 Relay 文档中的说明,通过 Relay 发送一个测试事件到 Sentry。 不要忘记更新您的 DSN 以指向正在运行的 Relay 实例。 设置好 Relay 后,您应该会看到对服务器影响的显著改善。