设置 Metrics
了解如何通过在 React Native 应用中配置 Metrics 来衡量你关心的数据点。
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.
Metrics for React Native are supported in Sentry React Native SDK version 5.19.0
and above.
Sentry Metrics 帮助你通过衡量对你重要的数据点来定位和解决影响用户体验和应用性能的问题。你可以跟踪诸如处理时间、事件大小、用户注册和转化率等指标,并将它们与追踪数据关联,以获得更深入的见解并更快地解决问题。
Sentry.init({
dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0',
integrations: [
Sentry.metrics.metricsAggregatorIntegration(),
],
});
计数器是最基本的 Metrics 类型之一,可用于统计某些事件的发生次数。
要发送一个计数器,请执行以下操作:
// Increment a counter by one for each button click.
Sentry.metrics.increment("button_click", 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 for component.
Sentry.metrics.distribution("component_load_time", 15.0, {
tags: { type: "important" },
unit: "millisecond",
});
集合适用于查看唯一发生情况并统计你添加的唯一元素。
要发送一个集合,请执行以下操作:
// Add 'jane' to a set used for tracking the number of users that viewed a page.
Sentry.metrics.set("user_view", "jane");
仪表盘让你可以获得如 min
、max
、avg
、sum
和 count
等聚合值。它们比分布占用更少的空间,但不能用于获取百分位数。如果你不需要百分位数,我们建议使用仪表盘。
要发送一个仪表盘,请执行以下操作:
// Add 2 to a gauge tracking CPU usage.
Sentry.metrics.gauge("cpu_usage", 34, {
tags: { os: "MacOS" },
unit: "percent",
});
将单位作为度量参数可以赋予那些可能看起来抽象的数字意义。它还允许 Sentry 提供基于你选择的单位的控制——单位转换、过滤等。你可以将单位作为 increment
、distribution
、set
和 gauge
方法的第三个参数的可选参数传递。如果值没有单位,你可以提供一个空字符串或 none
。
以下单位被 Sentry 后端理解,但你可以添加任何自定义单位。
nanosecond
microsecond
millisecond
second
minute
hour
day
week
bit
byte
kilobyte
kibibyte
megabyte
mebibyte
gigabyte
gibibyte
terabyte
tebibyte
petabyte
pebibyte
exabyte
exbibyte
ratio
percent
你可以在我们的 事件摄取文档 中找到支持单位的更多详细信息。