设置指标
了解如何通过在 PHP 应用中配置指标来衡量您关心的数据点。
The Metrics beta has ended on October 7th
Thank you for participating in our Metrics beta program. After careful consideration, we have ended the beta program and retired the current Metrics solution. We're actively developing a new solution that will make tracking and debugging any issues in your application easier. Learn more.
PHP 的指标支持需要 Sentry PHP SDK 版本 4.5.0
及以上。
Sentry 指标通过衡量对您重要的数据点,帮助您定位和解决影响用户体验和应用程序性能的问题。您可以跟踪诸如处理时间、事件大小、用户注册和转化率等数据点,并将它们与追踪数据关联,以获得更深入的见解并更快解决问题。
计数器是较为基本的指标类型之一,可用于统计某些事件的发生次数。
要发送一个计数器,请按照以下步骤操作:
// Increment a counter by one for each button click.
\Sentry\metrics()->increment(
key: 'button_click',
value: 1,
tags: [
'browser' => 'Firefox',
'app_version' => '1.0.0',
],
);
分布帮助您通过允许获取诸如 p90
、min
、max
和 avg
等聚合信息,从而从数据中获得最大洞察力。
要发送一个分布,请按照以下步骤操作:
// Add '15.0' to a distribution used for tracking the loading times per page.
\Sentry\metrics()->distribution(
key: 'page_load',
value: 15.0,
unit: \Sentry\Metrics\MetricsUnit::millisecond(),
tags: [
'page' => '/home',
],
);
集合适用于查看唯一发生情况并统计您添加的唯一元素。
要发送一个集合,请按照以下步骤操作:
// Add 'jane' to a set used for tracking the number of users that viewed a page.
\Sentry\metrics()->set(
key: 'user_view',
value: 'jane',
unit: \Sentry\Metrics\MetricsUnit::custom('username'),
tags: [
'page' => '/home',
],
);
仪表让您能够获取如 min
、max
、avg
、sum
和 count
等聚合值。与分布相比,仪表可以更高效地利用空间进行表示,但不能用于获取百分位数。如果百分位数对您不重要,我们建议使用仪表。
要发送一个仪表,请按照以下步骤操作:
// Add '15.0' to a gauge used for tracking the loading times for a page.
\Sentry\metrics()->gauge(
key: 'page_load',
value: 15.0,
unit: \Sentry\Metrics\MetricsUnit::millisecond(),
tags: [
'page' => '/home',
],
);
计时器可以用于测量特定代码块的执行时间。它们的实现方式类似于分布,但以秒为单位进行测量。
要发送一个计时器,请按照以下步骤操作:
\Sentry\metrics()->timing(
key: 'event_processing_time',
callback: static fn() => process(),
);
所有发送的指标都必须手动刷新到 Sentry。
要刷新您的指标,请按照以下步骤操作:
use function Sentry\metrics;
metrics()->flush();
为了减少开销,我们建议在关闭函数中注册刷新操作。
use function Sentry\metrics;
register_shutdown_function(static fn () => metrics()->flush());
要附加指标的代码位置,将 attach_metric_code_locations
设置为 true
(默认值为 false
)。
\Sentry\init([
'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0',
'attach_metric_code_locations' => true,
]);