sentry-setup-tracing by getsentry/sentry-agent-skills
npx skills add https://github.com/getsentry/sentry-agent-skills --skill sentry-setup-tracing配置 Sentry 的性能监控以追踪事务和跨度。
tracesSampleRate 或自定义跨度重要提示: 下面的 SDK 版本、API 名称和代码示例仅供参考。在实施前,请务必对照 docs.sentry.io 进行验证,因为 API 和最低版本可能已发生变化。
| 平台 | 最低 SDK | 启用方式 | 自定义跨度 |
|---|---|---|---|
| JS/Browser | 9.0.0+ | tracesSampleRate + browserTracingIntegration() | Sentry.startSpan() |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| Next.js | 9.0.0+ | 在每个运行时配置文件中设置 tracesSampleRate | Sentry.startSpan() |
| Node.js | 9.0.0+ | tracesSampleRate | Sentry.startSpan() |
| Python | 0.11.2+ | traces_sample_rate | @sentry_sdk.trace 或 start_span() |
| Ruby | 5.0.0+ | traces_sample_rate | Sentry.with_child_span() |
Sentry.init({
dsn: "YOUR_DSN",
tracesSampleRate: 1.0, // 1.0 = 100%,生产环境建议降低
integrations: [Sentry.browserTracingIntegration()], // 仅限 Browser/React
tracePropagationTargets: ["localhost", /^https:\/\/api\./],
});
// 异步操作
const result = await Sentry.startSpan(
{ name: "fetch-user", op: "http.client" },
async () => {
return await fetch("/api/user").then(r => r.json());
}
);
// 嵌套跨度
await Sentry.startSpan({ name: "checkout", op: "transaction" }, async () => {
await Sentry.startSpan({ name: "validate", op: "validation" }, validateCart);
await Sentry.startSpan({ name: "payment", op: "payment" }, processPayment);
});
tracesSampler: ({ name, inheritOrSampleWith }) => {
if (name.includes("healthcheck")) return 0;
if (name.includes("checkout")) return 1.0;
return inheritOrSampleWith(0.1); // 尊重父级采样决策,否则回退到 0.1
},
sentry_sdk.init(
dsn="YOUR_DSN",
traces_sample_rate=1.0,
)
# 装饰器
@sentry_sdk.trace
def expensive_function():
return do_work()
# 上下文管理器
with sentry_sdk.start_span(name="process-order", op="task") as span:
span.set_data("order.id", order_id)
process(order_id)
from sentry_sdk.types import SamplingContext
def traces_sampler(sampling_context: SamplingContext) -> float:
name = sampling_context.get("transaction_context", {}).get("name", "")
parent_sampled = sampling_context.get("parent_sampled")
if "healthcheck" in name: return 0
if "checkout" in name: return 1.0
if parent_sampled is not None: return float(parent_sampled) # 尊重父级决策
return 0.1
sentry_sdk.init(dsn="YOUR_DSN", traces_sampler=traces_sampler)
Sentry.init do |config|
config.dsn = "YOUR_DSN"
config.traces_sample_rate = 1.0
end
op 值 | 使用场景 |
|---|---|
http.client | 发起的 HTTP 请求 |
http.server | 接收的 HTTP 请求 |
db / db.query | 数据库 |
cache | 缓存操作 |
queue.task | 后台任务 |
function | 函数调用 |
| 流量 | 采样率 |
|---|---|
| 开发环境 | 1.0 |
| 低流量 (<1K 请求/分钟) | 0.5 - 1.0 |
| 中等流量 (1K-10K) | 0.1 - 0.5 |
| 高流量 (>10K) | 0.01 - 0.1 |
配置 tracePropagationTargets 以将追踪头发送到您的 API:
tracePropagationTargets: ["localhost", "https://api.yourapp.com"],
对于 Next.js 14 App Router,请添加到根布局中(Next.js 15+ 不需要):
export function generateMetadata(): Metadata {
return { other: { ...Sentry.getTraceData() } };
}
启用追踪后,触发一个被追踪的操作(例如,一个 HTTP 请求),并在 Sentry 性能仪表板中检查事务。自定义跨度应出现在父事务的嵌套结构中。
| 问题 | 解决方案 |
|---|---|
| 事务未出现 | 检查 tracesSampleRate > 0,验证 DSN |
| 浏览器追踪缺失 | 添加 browserTracingIntegration() |
| 分布式追踪断开 | 检查 tracePropagationTargets,CORS 头 |
| 事务过多 | 降低采样率,使用 tracesSampler 进行过滤 |
每周安装数
224
代码仓库
GitHub 星标数
19
首次出现
2026年1月20日
安全审计
安装于
opencode195
codex190
gemini-cli182
github-copilot177
claude-code177
cursor168
Configure Sentry's performance monitoring to track transactions and spans.
tracesSampleRate or custom spansImportant: 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.
| Platform | Min SDK | Enable | Custom Span |
|---|---|---|---|
| JS/Browser | 9.0.0+ | tracesSampleRate + browserTracingIntegration() | Sentry.startSpan() |
| Next.js | 9.0.0+ | tracesSampleRate in each runtime config file | Sentry.startSpan() |
| Node.js | 9.0.0+ | tracesSampleRate | Sentry.startSpan() |
| Python | 0.11.2+ | traces_sample_rate | @sentry_sdk.trace or start_span() |
| Ruby | 5.0.0+ | traces_sample_rate | Sentry.with_child_span() |
Sentry.init({
dsn: "YOUR_DSN",
tracesSampleRate: 1.0, // 1.0 = 100%, lower for production
integrations: [Sentry.browserTracingIntegration()], // Browser/React only
tracePropagationTargets: ["localhost", /^https:\/\/api\./],
});
// Async operation
const result = await Sentry.startSpan(
{ name: "fetch-user", op: "http.client" },
async () => {
return await fetch("/api/user").then(r => r.json());
}
);
// Nested spans
await Sentry.startSpan({ name: "checkout", op: "transaction" }, async () => {
await Sentry.startSpan({ name: "validate", op: "validation" }, validateCart);
await Sentry.startSpan({ name: "payment", op: "payment" }, processPayment);
});
tracesSampler: ({ name, inheritOrSampleWith }) => {
if (name.includes("healthcheck")) return 0;
if (name.includes("checkout")) return 1.0;
return inheritOrSampleWith(0.1); // Respects parent sampling decision, falls back to 0.1
},
sentry_sdk.init(
dsn="YOUR_DSN",
traces_sample_rate=1.0,
)
# Decorator
@sentry_sdk.trace
def expensive_function():
return do_work()
# Context manager
with sentry_sdk.start_span(name="process-order", op="task") as span:
span.set_data("order.id", order_id)
process(order_id)
from sentry_sdk.types import SamplingContext
def traces_sampler(sampling_context: SamplingContext) -> float:
name = sampling_context.get("transaction_context", {}).get("name", "")
parent_sampled = sampling_context.get("parent_sampled")
if "healthcheck" in name: return 0
if "checkout" in name: return 1.0
if parent_sampled is not None: return float(parent_sampled) # Respect parent decision
return 0.1
sentry_sdk.init(dsn="YOUR_DSN", traces_sampler=traces_sampler)
Sentry.init do |config|
config.dsn = "YOUR_DSN"
config.traces_sample_rate = 1.0
end
op Value | Use Case |
|---|---|
http.client | Outgoing HTTP |
http.server | Incoming HTTP |
db / db.query | Database |
cache | Cache operations |
queue.task | Background jobs |
| Traffic | Rate |
|---|---|
| Development | 1.0 |
| Low (<1K req/min) | 0.5 - 1.0 |
| Medium (1K-10K) | 0.1 - 0.5 |
| High (>10K) | 0.01 - 0.1 |
Configure tracePropagationTargets to send trace headers to your APIs:
tracePropagationTargets: ["localhost", "https://api.yourapp.com"],
For Next.js 14 App Router, add to root layout (not needed in Next.js 15+):
export function generateMetadata(): Metadata {
return { other: { ...Sentry.getTraceData() } };
}
After enabling tracing, trigger a traced operation (e.g., an HTTP request) and check the Sentry Performance dashboard for transactions. Custom spans should appear nested under the parent transaction.
| Issue | Solution |
|---|---|
| Transactions not appearing | Check tracesSampleRate > 0, verify DSN |
| Browser traces missing | Add browserTracingIntegration() |
| Distributed traces disconnected | Check tracePropagationTargets, CORS headers |
| Too many transactions | Lower sample rate, use tracesSampler to filter |
Weekly Installs
224
Repository
GitHub Stars
19
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode195
codex190
gemini-cli182
github-copilot177
claude-code177
cursor168
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
79,900 周安装
function |
| Function calls |