设置指标
了解如何通过在 Python 应用程序中配置指标来测量您关心的数据点。
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.
Python 的指标支持需要 Sentry Python SDK 版本 1.40.0
及以上。
Sentry 指标通过测量对您重要的数据点来帮助您定位和解决影响用户体验和应用程序性能的问题。您可以跟踪处理时间、事件大小、用户注册和转化率等,并将其与跟踪数据相关联,以获得更深入的见解并更快地解决问题。
指标开箱即用,因此您无需执行任何操作即可启用它。有一些实验性的选项可以用来影响指标行为。
计数器是一种更基本的指标类型,可以用来计算某些事件的发生次数。
要发送计数器,请执行以下操作:
# Increment a counter by one for each button click.
sentry_sdk.metrics.incr(
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_sdk.metrics.distribution(
key="page_load",
value=15.0,
unit="millisecond",
tags={
"page": "/home"
}
)
Sentry 支持 unit
的任意自定义值,但我们建议使用以下支持的单位之一。
集合用于查看唯一事件的发生情况并计算您添加的唯一元素数量。
要发送集合,请执行以下操作:
# Add 'jane' to a set used for tracking the number of users that viewed a page.
sentry_sdk.metrics.set(
key="user_view",
value="jane",
unit="username",
tags={
"page": "/home"
}
)
Sentry 支持 unit
的任意自定义值,但我们建议使用以下支持的单位之一。
仪表(Gauges)允许您获取聚合数据,如 min
、max
、avg
、sum
和 count
。它们比分布更节省空间,但无法用于获取百分位数。如果百分位数对您不重要,我们建议使用仪表。
要发送仪表,请执行以下操作:
# Add '15.0' to a gauge used for tracking the loading times for a page.
sentry_sdk.metrics.gauge(
key="page_load",
value=15.0,
unit="millisecond",
tags={
"page": "/home"
}
)
Sentry 支持 unit
的任意自定义值,但我们建议使用以下支持的单位之一。
计时器可以用于测量特定代码块的执行时间。它们的实现类似于分布,但测量单位为秒。
要发送计时器,请执行以下操作:
# Measure the time of execution of the `process()` function.
with sentry_sdk.metrics.timing(key="event_processing_time"):
process()
您可以使用以下选项来设置或更改 Metrics 的行为。由于 Metrics 仍在测试阶段,这些选项位于 _experiments
选项下。
before_emit_metric
一个回调函数,在发送指标之前被调用。如果回调函数返回 True
,则发送指标;如果返回 False
,则不发送指标。回调函数中还可以更新给定的 tags
。
def before_emit(key, value, unit, tags):
if key == "removed-metric":
return False
tags["extra"] = "foo"
del tags["release"]
return True
sentry_sdk.init(
...
_experiments={
"before_emit_metric": before_emit,
}
)
metric_code_locations
True
,则会将发送指标的代码行添加到指标中。如果为 False
,则省略代码位置。
默认值: True
sentry_sdk.init(
...
_experiments={
"metric_code_locations": False,
}
)
单位通过赋予含义来增强指标值,否则这些值可能是抽象的数字。添加单位还允许 Sentry 提供基于这些单位的控制——单位转换、过滤器等。对于无单位的值,你可以提供空字符串或 none
。
nanosecond
microsecond
millisecond
second
minute
hour
day
week
bit
byte
kilobyte
kibibyte
megabyte
mebibyte
gigabyte
gibibyte
terabyte
tebibyte
petabyte
pebibyte
exabyte
exbibyte
ratio
percent
有关支持的单位的更多详细信息,请参阅我们的event ingestion 文档。