sentry-setup-metrics by getsentry/sentry-agent-skills
npx skills add https://github.com/getsentry/sentry-agent-skills --skill sentry-setup-metrics配置 Sentry 的自定义指标以跟踪计数器、仪表盘和分布。
Sentry.metrics 或 sentry_sdk.metrics重要提示: 下面的 SDK 版本、API 名称和代码示例仅供参考。在实施前,请务必对照 docs.sentry.io 进行验证,因为 API 和最低版本可能已发生变化。
查看 Sentry 指标入门指南 以获取支持的 SDK 完整列表和最低版本要求。以下示例使用 JavaScript 和 Python:
| 平台 | 最低 SDK | API | 状态 |
|---|---|---|---|
| JavaScript | 10.25.0+ | Sentry.metrics.* | 公开测试版 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| Python | 2.44.0+ | sentry_sdk.metrics.* | 公开测试版 |
| Ruby | 6.3.0+ | Sentry.metrics.* | 公开测试版 |
| 类型 | 用途 | 示例用例 |
|---|---|---|
| 计数器 | 累计计数 | API 调用、点击、错误 |
| 仪表盘 | 时间点数值 | 队列深度、内存、连接数 |
| 分布 | 统计数值 | 响应时间、购物车金额 |
在 SDK 10.25.0+ 中,指标默认启用。
Sentry.metrics.count("api_call", 1, {
attributes: { endpoint: "/api/users", status_code: 200 },
});
Sentry.metrics.gauge("queue_depth", 42, {
unit: "none",
attributes: { queue: "jobs" },
});
Sentry.metrics.distribution("response_time", 187.5, {
unit: "millisecond",
attributes: { endpoint: "/api/products" },
});
Sentry.init({
beforeSendMetric: (metric) => {
if (metric.attributes?.sensitive) return null;
return metric;
},
});
在 SDK 2.44.0+ 中,指标默认启用。
sentry_sdk.metrics.count("api_call", 1, attributes={"endpoint": "/api/users"})
sentry_sdk.metrics.gauge("queue_depth", 42, attributes={"queue": "jobs"})
sentry_sdk.metrics.distribution(
"response_time", 187.5,
unit="millisecond",
attributes={"endpoint": "/api/products"}
)
def before_send_metric(metric, hint):
if metric.get("attributes", {}).get("sensitive"):
return None
return metric
sentry_sdk.init(dsn="YOUR_DSN", before_send_metric=before_send_metric)
| 类别 | 值 |
|---|---|
| 时间 | millisecond, second, minute, hour |
| 大小 | byte, kilobyte, megabyte |
| 货币 | usd, eur, gbp |
| 其他 | none, percent, ratio |
async function withTiming(name, fn, attrs = {}) {
const start = performance.now();
try { return await fn(); }
finally {
Sentry.metrics.distribution(name, performance.now() - start, {
unit: "millisecond", attributes: attrs,
});
}
}
import time, sentry_sdk
def track_duration(name, **attrs):
def decorator(fn):
def wrapper(*args, **kwargs):
start = time.time()
try: return fn(*args, **kwargs)
finally:
sentry_sdk.metrics.distribution(
name, (time.time() - start) * 1000,
unit="millisecond", attributes=attrs
)
return wrapper
return decorator
在 SDK 6.3.0+ 中,指标默认启用。
Sentry.metrics.count("api_call", 1, attributes: { endpoint: "/api/users" })
Sentry.metrics.gauge("queue_depth", 42, attributes: { queue: "jobs" })
Sentry.metrics.distribution("response_time", 187.5, unit: "millisecond", attributes: { endpoint: "/api/products" })
api.request.duration,而不是 durationSentry.flush()添加指标后,触发发出该指标的代码路径,并检查 Sentry 指标仪表盘(探索 > 指标)。由于缓冲区刷新,指标可能需要几分钟才会出现。
| 问题 | 解决方案 |
|---|---|
| 指标未出现 | 验证 SDK 版本,检查 DSN,等待缓冲区刷新 |
| 指标被静默丢弃 | 检查指标事件是否在 2KB 大小限制内 —— 减少属性 |
| 指标过多 | 使用 beforeSendMetric 进行过滤 |
每周安装数
182
代码仓库
GitHub 星标数
19
首次出现
2026年1月20日
安全审计
安装于
opencode155
codex152
gemini-cli144
claude-code140
github-copilot139
cursor132
Configure Sentry's custom metrics for tracking counters, gauges, and distributions.
Sentry.metrics or sentry_sdk.metricsImportant: The SDK versions, API names, and code samples below are examples. Always verify against docs.sentry.io before implementing, as APIs and minimum versions may have changed.
Check Sentry Metrics Getting Started for the full list of supported SDKs and minimum versions. Examples below use JavaScript and Python:
| Platform | Min SDK | API | Status |
|---|---|---|---|
| JavaScript | 10.25.0+ | Sentry.metrics.* | Open Beta |
| Python | 2.44.0+ | sentry_sdk.metrics.* | Open Beta |
| Ruby | 6.3.0+ | Sentry.metrics.* | Open Beta |
| Type | Purpose | Example Use Cases |
|---|---|---|
| Counter | Cumulative counts | API calls, clicks, errors |
| Gauge | Point-in-time values | Queue depth, memory, connections |
| Distribution | Statistical values | Response times, cart amounts |
Metrics are enabled by default in SDK 10.25.0+.
Sentry.metrics.count("api_call", 1, {
attributes: { endpoint: "/api/users", status_code: 200 },
});
Sentry.metrics.gauge("queue_depth", 42, {
unit: "none",
attributes: { queue: "jobs" },
});
Sentry.metrics.distribution("response_time", 187.5, {
unit: "millisecond",
attributes: { endpoint: "/api/products" },
});
Sentry.init({
beforeSendMetric: (metric) => {
if (metric.attributes?.sensitive) return null;
return metric;
},
});
Metrics are enabled by default in SDK 2.44.0+.
sentry_sdk.metrics.count("api_call", 1, attributes={"endpoint": "/api/users"})
sentry_sdk.metrics.gauge("queue_depth", 42, attributes={"queue": "jobs"})
sentry_sdk.metrics.distribution(
"response_time", 187.5,
unit="millisecond",
attributes={"endpoint": "/api/products"}
)
def before_send_metric(metric, hint):
if metric.get("attributes", {}).get("sensitive"):
return None
return metric
sentry_sdk.init(dsn="YOUR_DSN", before_send_metric=before_send_metric)
| Category | Values |
|---|---|
| Time | millisecond, second, minute, hour |
| Size | byte, kilobyte, megabyte |
| Currency | usd, eur, |
async function withTiming(name, fn, attrs = {}) {
const start = performance.now();
try { return await fn(); }
finally {
Sentry.metrics.distribution(name, performance.now() - start, {
unit: "millisecond", attributes: attrs,
});
}
}
import time, sentry_sdk
def track_duration(name, **attrs):
def decorator(fn):
def wrapper(*args, **kwargs):
start = time.time()
try: return fn(*args, **kwargs)
finally:
sentry_sdk.metrics.distribution(
name, (time.time() - start) * 1000,
unit="millisecond", attributes=attrs
)
return wrapper
return decorator
Metrics are enabled by default in SDK 6.3.0+.
Sentry.metrics.count("api_call", 1, attributes: { endpoint: "/api/users" })
Sentry.metrics.gauge("queue_depth", 42, attributes: { queue: "jobs" })
Sentry.metrics.distribution("response_time", 187.5, unit: "millisecond", attributes: { endpoint: "/api/products" })
api.request.duration, not durationSentry.flush() before process exitAfter adding a metric, trigger the code path that emits it and check the Sentry Metrics dashboard (Explore > Metrics). Metrics may take a few minutes to appear due to buffer flushing.
| Issue | Solution |
|---|---|
| Metrics not appearing | Verify SDK version, check DSN, wait for buffer flush |
| Metric dropped silently | Check that metric event is under 2KB size limit — reduce attributes |
| Too many metrics | Use beforeSendMetric to filter |
Weekly Installs
182
Repository
GitHub Stars
19
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode155
codex152
gemini-cli144
claude-code140
github-copilot139
cursor132
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
104,900 周安装
gbp| Other | none, percent, ratio |