sentry-instrumentation-guide

作者: sentry

决定在代码中添加监控时,应选择哪种Sentry信号——错误、跨度、跨度属性、日志或指标。在添加监控且不确定时使用…

npx skills add https://github.com/getsentry/sentry-for-ai --skill sentry-instrumentation-guide

All Skills > Feature Setup > Instrumentation Guide

Sentry Instrumentation Guide: When to Reach for What

Errors, traces, logs, and metrics are the four kinds of telemetry most apps run on, and they overlap enough that the choice is rarely obvious. You can stuff context into a span attribute instead of logging it. You can count log lines instead of emitting a metric. You can add a duration to a log and call it a span.

But each signal exists because it answers a different question and feeds a different workflow once it lands. Reaching for the wrong one means the data is technically there but useless for the job you actually have later. This skill is the decision framework: given a value or an event in front of you, which signal should carry it, and why.

It decides what to emit. For how to turn each pillar on for a given stack, hand off to the sentry-*-sdk skills and sentry-setup-ai-monitoring.

Invoke This Skill When

  • You're instrumenting a piece of code and unsure whether something should be a log, a span, a span attribute, or a metric
  • You're deciding "what to instrument where" across a service or request handler
  • You're reviewing existing instrumentation for gaps (e.g. an error feed that's empty while users report problems)
  • A coding agent needs a consistent rule for choosing between errors, traces, logs, and metrics

Important: The SDK APIs and code samples here are illustrative. Verify exact signatures and minimum versions against docs.sentry.io and the relevant sentry-*-sdk skill before implementing.

The Four Signals, One Question Each

SignalThe question it answersDocs
Errors"What just broke?" — a stack trace and exception type, grouped into a deduplicated Issue that gets assigned and tracked to resolution. If your code threw, it's an error.Issues
Traces"Did the request flow the way it was supposed to?" — a waterfall of timed spans. Mostly auto-instrumented.Trace Explorer
Logs"What was true at this point in the code, and why?" — the system's state at one moment as a structured event: config, flags, inputs/outputs, the decision that was made.Logs
Metrics"How's this trending over time?" — counters, gauges, distributions you can slice by attribute and chart, alert on, or compare across a deploy.Metrics

A useful mental split: a log is one request's story (the needle), a metric is the aggregate (whether the haystack is normal), a trace is where the time went, and an error is the thing that needs a stack trace and an owner.

The Decision Table

Use this as a gut check:

What you want to knowReach for
Something crashed, show the stack traceError
How long did this take? Which step is slow?Traces / Spans
Did the request flow through the steps I expected?Traces / Spans
What was the state when the code made this decision?Log
What did this function receive and return?Log
How often does X happen? Is the rate normal?Metric
Did something change after the deploy?Metric

Resolving the Overlaps

The same value can legitimately appear in more than one signal. These four tiebreakers cover almost every real case. (Full reasoning, gotchas, and the "why not just log everything / emit one wide event?" arguments live in references/choosing-signals.md.)

  • Span attribute or metric? Context about one request's flow that you want while reading that trace → span attribute (it rides on the span in the waterfall). A standalone value you want to chart, alert on, or slice over time across all requests → metric. The same number can warrant both: candidate_count on the span to read one request, recommendations.served as a metric to watch the rate.
  • Log or span? The span is the timed node in the flow (mostly auto-instrumented, you rarely write it). The log is the decision-point state inside that node (you always write it on purpose). Span answers where and how long; log answers what was true and why.
  • Log or metric? A log finds the one specific request that went wrong (the needle). A metric tells you how many requests went wrong (the haystack). Don't derive a rate by counting log lines — emit the metric directly.
  • Error or log? Needs a stack trace and should be tracked as an Issue → error. An unexpected-but-handled condition worth recording → log. Truly non-critical with a traceback → logger.warning(exc_info=True) keeps the trace in logs without creating noise in the error feed.

Sampling vs Filtering — Match Retention to the Question

Each signal's retention falls out of the question it answers:

  • Traces are sampled. You don't need every request to understand where time goes, so keep a representative slice via traces_sample_rate (higher in dev, lower in production).
  • Errors are captured by default. No sampling to think about for the baseline.
  • Logs and metrics are NOT sampled. You keep every one and filter instead, with before_send_log and before_send_metric. This is the point: the whole reason for a log is to find the one rare request that went sideways, and you can't find what you sampled away.

(For the exact sampling and filtering config in your language, see the matching SDK skill's references/tracing.md and references/metrics.md.)

Because all four signals come from one SDK, they share a trace_id and correlate on their own — every log and metric is tied to its trace, so you can drill from a metric spike straight into the samples behind it.

What Deliberate Instrumentation Looks Like

Roughly 80% of spans are auto-instrumented by your framework and database integrations — you write almost none of them. The deliberate work is the other 20%: a span attribute or two to enrich the flow, a decision-point log, and a metric, placed at the spots where your code makes a choice worth questioning later.

references/instrumentation-examples.md walks through a single request handler instrumented end to end, in both Python and JavaScript/TypeScript, showing the span attribute, the log, and the metric side by side on the same decision.

Handing Off to Setup

This skill tells you what to emit. To actually wire a pillar up:

  • Install the SDK and turn on tracing, logs, and metrics → the matching sentry-<platform>-sdk skill (e.g. sentry-python-sdk, sentry-nextjs-sdk, sentry-node-sdk). Each has per-feature reference files for tracing, logging, metrics, and more.
  • Instrument LLM / agent callssentry-setup-ai-monitoring.

Logs and metrics are the two pillars most projects haven't turned on yet, and both are included on every plan. If they aren't enabled, route to the SDK skill first, then come back here to decide what to put where.

来自 sentry 的更多技能

generate-frontend-forms
sentry
使用Sentry新表单系统创建表单的指南。在实现表单、表单字段、验证或自动保存功能时使用。
official
sentry-snapshots-cocoa
sentry
完整的 Sentry Snapshots 配置,适用于 Apple/Cocoa 项目。当被要求“设置 SnapshotPreviews”、“设置 Apple 快照测试”、“上传 Apple 快照到…”时使用。
official
architecture-review
sentry
员工级代码库健康审查。发现单体模块、静默失败、类型安全漏洞、测试覆盖缺口以及LLM友好性问题。
official
linear-type-labeler
sentry
根据每个问题的标题和描述内容,对Linear问题进行分类,并从Sentry工作区的标签分类体系中应用一个类型标签。
official
sentry-flutter-sdk
sentry
完整的Sentry SDK配置,适用于Flutter和Dart。当被要求“为Flutter添加Sentry”、“安装sentry_flutter”、“在Dart中配置Sentry”或配置错误…时使用。
official
sentry-svelte-sdk
sentry
为Svelte和SvelteKit提供完整的Sentry SDK设置。当被要求“为Svelte添加Sentry”、“为SvelteKit添加Sentry”、“安装@sentry/sveltekit”或配置……时使用。
official
vercel-react-best-practices
sentry
来自 Vercel 工程团队的 React 和 Next.js 性能优化指南。在编写、审查或重构 React/Next.js… 时应使用此技能。
official
sentry-tanstack-start-sdk
sentry
为TanStack Start React提供完整的Sentry SDK设置。当被要求“向TanStack Start添加Sentry”、“安装@sentry/tanstackstart-react”或配置错误…时使用。
official