sentry-ruby-sdk
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSentry Ruby SDK
Sentry Ruby SDK
Opinionated wizard that scans the project and guides through complete Sentry setup.
这是一个指导性工具,会扫描项目并引导你完成完整的Sentry配置流程。
Invoke This Skill When
触发该指南的场景
- User asks to "add Sentry to Ruby" or "set up Sentry" in a Ruby app
- User wants error monitoring, tracing, logging, metrics, profiling, or crons in Ruby
- User mentions ,
sentry-ruby, or the Ruby Sentry SDKsentry-rails - User is migrating from AppSignal or Honeybadger to Sentry
- User wants to monitor exceptions, HTTP requests, or background jobs in Rails/Sinatra
Note: SDK APIs below reflect sentry-ruby v6.4.0. Always verify against docs.sentry.io/platforms/ruby/ before implementing.
- 用户要求“在Ruby中添加Sentry”或在Ruby应用中“配置Sentry”
- 用户需要为Ruby应用实现错误监控、链路追踪、日志、指标、性能分析或定时任务监控
- 用户提及、
sentry-ruby或Ruby版Sentry SDKsentry-rails - 用户需要从AppSignal或Honeybadger迁移至Sentry
- 用户希望监控Rails/Sinatra中的异常、HTTP请求或后台任务
注意: 以下SDK API基于sentry-ruby v6.4.0版本。 实施前请务必对照docs.sentry.io/platforms/ruby/确认最新内容。
Phase 1: Detect
阶段1:项目检测
bash
undefinedbash
undefinedExisting Sentry gems
Existing Sentry gems
grep -i sentry Gemfile 2>/dev/null
grep -i sentry Gemfile 2>/dev/null
Framework
Framework
grep -E '"rails"|"sinatra"' Gemfile 2>/dev/null
grep -E '"rails"|"sinatra"' Gemfile 2>/dev/null
Background jobs
Background jobs
grep -E '"sidekiq"|"resque"|"delayed_job"' Gemfile 2>/dev/null
grep -E '"sidekiq"|"resque"|"delayed_job"' Gemfile 2>/dev/null
Competitor monitoring tools — triggers migration path if found
Competitor monitoring tools — triggers migration path if found
grep -E '"appsignal"|"honeybadger"' Gemfile 2>/dev/null
grep -E '"appsignal"|"honeybadger"' Gemfile 2>/dev/null
Existing metric patterns (StatsD, Datadog, Prometheus)
Existing metric patterns (StatsD, Datadog, Prometheus)
grep -rE "(statsd|dogstatsd|prometheus|.gauge|.histogram|.increment|.timing)"
app/ lib/ --include="*.rb" 2>/dev/null | grep -v "_spec|_test" | head -20
app/ lib/ --include="*.rb" 2>/dev/null | grep -v "_spec|_test" | head -20
grep -rE "(statsd|dogstatsd|prometheus|.gauge|.histogram|.increment|.timing)"
app/ lib/ --include="*.rb" 2>/dev/null | grep -v "_spec|_test" | head -20
app/ lib/ --include="*.rb" 2>/dev/null | grep -v "_spec|_test" | head -20
Companion frontend
Companion frontend
cat package.json frontend/package.json web/package.json 2>/dev/null | grep -E '"@sentry|"sentry-'
**Route from what you find:**
- **Competitor detected** (`appsignal`, `honeybadger`) → load `references/migration.md` first; **delete the competitor initializer** (`config/initializers/honeybadger.rb` or `config/initializers/appsignal.rb`) as part of migration
- **Sentry already present** → skip to Phase 2 to configure features
- **Rails** → use `sentry-rails` + `config/initializers/sentry.rb`
- **Rack/Sinatra** → `sentry-ruby` + `Sentry::Rack::CaptureExceptions` middleware
- **Sidekiq** → add `sentry-sidekiq`; recommend Metrics if existing metric patterns found
---cat package.json frontend/package.json web/package.json 2>/dev/null | grep -E '"@sentry|"sentry-'
**根据检测结果选择对应流程:**
- **检测到竞品工具**(`appsignal`、`honeybadger`)→ 先加载`references/migration.md`;迁移过程中需**删除竞品的初始化文件**(`config/initializers/honeybadger.rb`或`config/initializers/appsignal.rb`)
- **已存在Sentry** → 直接跳至阶段2配置功能
- **Rails项目** → 使用`sentry-rails` + `config/initializers/sentry.rb`配置
- **Rack/Sinatra项目** → 使用`sentry-ruby` + `Sentry::Rack::CaptureExceptions`中间件
- **Sidekiq项目** → 添加`sentry-sidekiq`;若检测到现有指标工具,推荐配置Metrics功能
---Phase 2: Recommend
阶段2:功能推荐
Lead with a concrete proposal — don't ask open-ended questions:
| Feature | Recommend when... |
|---|---|
| Error Monitoring | Always |
| Tracing | Rails / Sinatra / Rack / any HTTP framework |
| Logging | Always — |
| Metrics | Sidekiq present; existing metric lib (StatsD, Prometheus) detected |
| Profiling | ⚠️ Beta — performance profiling requested; requires |
| Crons | Scheduled jobs detected (ActiveJob, Sidekiq-Cron, Clockwork, Whenever) |
Propose: "I recommend Error Monitoring + Tracing + Logging [+ Metrics if applicable]. Shall I proceed?"
直接给出具体方案,避免开放式问题:
| 功能 | 推荐场景 |
|---|---|
| 错误监控 | 所有场景必选 |
| 链路追踪 | Rails / Sinatra / Rack 或任何HTTP框架项目 |
| 日志采集 | 所有场景必选 — 开启 |
| 指标监控 | 存在Sidekiq的项目;检测到现有指标库(StatsD、Prometheus)的项目 |
| 性能分析 | ⚠️ 测试功能 — 用户明确要求性能分析;需依赖 |
| 定时任务监控 | 检测到定时任务(ActiveJob、Sidekiq-Cron、Clockwork、Whenever)的项目 |
推荐话术:“我建议配置错误监控 + 链路追踪 + 日志采集 [+ 指标监控(若适用)]。是否继续?”
Phase 3: Guide
阶段3:配置引导
Install
安装依赖
Rails:
ruby
undefinedRails项目:
ruby
undefinedGemfile
Gemfile
gem "sentry-ruby"
gem "sentry-rails"
gem "sentry-sidekiq" # if using Sidekiq
gem "sentry-resque" # if using Resque
gem "sentry-delayed_job" # if using DelayedJob
**Rack / Sinatra / plain Ruby:**
```ruby
gem "sentry-ruby"Run .
bundle installgem "sentry-ruby"
gem "sentry-rails"
gem "sentry-sidekiq" # 若使用Sidekiq
gem "sentry-resque" # 若使用Resque
gem "sentry-delayed_job" # 若使用DelayedJob
**Rack / Sinatra / 纯Ruby项目:**
```ruby
gem "sentry-ruby"执行完成安装。
bundle installInit — Rails (config/initializers/sentry.rb
)
config/initializers/sentry.rb初始化 — Rails项目(config/initializers/sentry.rb
)
config/initializers/sentry.rbruby
Sentry.init do |config|
config.dsn = ENV["SENTRY_DSN"]
config.spotlight = Rails.env.development? # local Spotlight UI; no DSN needed in dev
config.breadcrumbs_logger = [:active_support_logger, :http_logger]
config.send_default_pii = true
config.traces_sample_rate = 1.0 # lower to 0.05–0.2 in production
config.enable_logs = true
# Metrics on by default; disable with: config.enable_metrics = false
endsentry-railsruby
Sentry.init do |config|
config.dsn = ENV["SENTRY_DSN"]
config.spotlight = Rails.env.development? # 本地Spotlight UI;开发环境无需DSN
config.breadcrumbs_logger = [:active_support_logger, :http_logger]
config.send_default_pii = true
config.traces_sample_rate = 1.0 # 生产环境建议调低至0.05–0.2
config.enable_logs = true
# 指标监控默认开启;可通过以下配置关闭:config.enable_metrics = false
endsentry-railsInit — Rack / Sinatra
初始化 — Rack / Sinatra项目
ruby
require "sentry-ruby"
Sentry.init do |config|
config.dsn = ENV["SENTRY_DSN"]
config.spotlight = ENV["RACK_ENV"] == "development"
config.breadcrumbs_logger = [:sentry_logger, :http_logger]
config.send_default_pii = true
config.traces_sample_rate = 1.0
config.enable_logs = true
end
use Sentry::Rack::CaptureExceptions # in config.ru, before app middlewareruby
require "sentry-ruby"
Sentry.init do |config|
config.dsn = ENV["SENTRY_DSN"]
config.spotlight = ENV["RACK_ENV"] == "development"
config.breadcrumbs_logger = [:sentry_logger, :http_logger]
config.send_default_pii = true
config.traces_sample_rate = 1.0
config.enable_logs = true
end
use Sentry::Rack::CaptureExceptions # 在config.ru中,置于应用中间件之前Init — Sidekiq standalone
初始化 — 独立Sidekiq项目
ruby
require "sentry-ruby"
require "sentry-sidekiq"
Sentry.init do |config|
config.dsn = ENV["SENTRY_DSN"]
config.spotlight = ENV.fetch("RAILS_ENV", "development") == "development"
config.breadcrumbs_logger = [:sentry_logger]
config.traces_sample_rate = 1.0
config.enable_logs = true
endruby
require "sentry-ruby"
require "sentry-sidekiq"
Sentry.init do |config|
config.dsn = ENV["SENTRY_DSN"]
config.spotlight = ENV.fetch("RAILS_ENV", "development") == "development"
config.breadcrumbs_logger = [:sentry_logger]
config.traces_sample_rate = 1.0
config.enable_logs = true
endEnvironment variables
环境变量配置
bash
SENTRY_DSN=https://xxx@oYYY.ingest.sentry.io/ZZZ
SENTRY_ENVIRONMENT=production # overrides RAILS_ENV / RACK_ENV
SENTRY_RELEASE=my-app@1.0.0bash
SENTRY_DSN=https://xxx@oYYY.ingest.sentry.io/ZZZ
SENTRY_ENVIRONMENT=production # 覆盖RAILS_ENV / RACK_ENV
SENTRY_RELEASE=my-app@1.0.0Feature reference files
功能参考文件
Load each reference when implementing the corresponding feature:
| Feature | Reference | Load when... |
|---|---|---|
| Migration | | Competitor gem found — load before installing Sentry |
| Error Monitoring | | Always |
| Tracing | | HTTP handlers / distributed tracing |
| Logging | | Structured log capture |
| Metrics | | Sidekiq present; existing metric patterns |
| Profiling | | Performance profiling requested (beta) |
| Crons | | Scheduled jobs detected or requested |
实现对应功能时加载对应的参考文件:
| 功能 | 参考文件 | 加载场景 |
|---|---|---|
| 迁移指南 | | 检测到竞品gem — 安装Sentry前加载 |
| 错误监控 | | 所有场景 |
| 链路追踪 | | HTTP处理 / 分布式追踪场景 |
| 日志采集 | | 结构化日志采集场景 |
| 指标监控 | | 存在Sidekiq的项目;检测到现有指标工具的项目 |
| 性能分析 | | 用户明确要求性能分析(测试功能) |
| 定时任务监控 | | 检测到或用户要求定时任务监控的场景 |
Configuration Reference
配置参考
Key Sentry.init
Options
Sentry.initSentry.init
核心配置项
Sentry.init| Option | Type | Default | Purpose |
|---|---|---|---|
| String | | SDK disabled if empty; env: |
| String | | e.g., |
| String | | e.g., |
| Boolean | | Send events to Spotlight sidecar (local dev, no DSN needed) |
| Boolean | | Include IP addresses and request headers |
| Float | | Error event sample rate (0.0–1.0) |
| Float | | Transaction sample rate; |
| Float | | Profiling rate relative to |
| Boolean | | Enable Sentry structured Logs |
| Boolean | | Enable custom metrics (on by default) |
| Array | | Loggers for automatic breadcrumbs (see logging reference) |
| Integer | | Max breadcrumbs per event |
| Boolean | | Verbose SDK output to stdout |
| Lambda | | Mutate or drop error events before sending |
| Lambda | | Mutate or drop transaction events before sending |
| Lambda | | Mutate or drop log events before sending |
| 配置项 | 类型 | 默认值 | 用途 |
|---|---|---|---|
| 字符串 | | 为空时SDK禁用;可通过环境变量 |
| 字符串 | | 例如 |
| 字符串 | | 例如 |
| 布尔值 | | 将事件发送至Spotlight本地服务(开发环境无需DSN) |
| 布尔值 | | 包含IP地址和请求头信息 |
| 浮点数 | | 错误事件采样率(0.0–1.0) |
| 浮点数 | | 事务采样率; |
| 浮点数 | | 性能分析采样率(基于链路追踪采样率);需依赖 |
| 布尔值 | | 开启Sentry结构化日志采集 |
| 布尔值 | | 开启自定义指标监控(默认开启) |
| 数组 | | 自动采集面包屑的日志器(参考日志采集文档) |
| 整数 | | 每个事件最多保留的面包屑数量 |
| 布尔值 | | 开启SDK详细日志输出至标准输出 |
| Lambda | | 发送前修改或丢弃错误事件 |
| Lambda | | 发送前修改或丢弃事务事件 |
| Lambda | | 发送前修改或丢弃日志事件 |
Environment Variables
环境变量映射
| Variable | Maps to | Purpose |
|---|---|---|
| | Data Source Name |
| | App version (e.g., |
| | Deployment environment |
Options set in override environment variables.
Sentry.init| 环境变量 | 对应配置项 | 用途 |
|---|---|---|
| | 数据源名称 |
| | 应用版本(例如 |
| | 部署环境 |
Sentry.initVerification
验证配置
Local dev (no DSN needed) — Spotlight:
bash
npx @spotlightjs/spotlight # browser UI at http://localhost:8969本地开发环境(无需DSN)—— 使用Spotlight:
bash
npx @spotlightjs/spotlight # 浏览器UI地址:http://localhost:8969or stream events to terminal:
或在终端查看事件流:
npx @spotlightjs/spotlight tail traces --format json
`config.spotlight = Rails.env.development?` (already in the init block above) routes events to the local sidecar automatically.
**With a real DSN:**
```ruby
Sentry.capture_message("Sentry Ruby SDK test")Nothing appears? Set and check stdout. Verify DSN format: .
config.debug = truehttps://<key>@o<org>.ingest.sentry.io/<project>npx @spotlightjs/spotlight tail traces --format json
`config.spotlight = Rails.env.development?`(已包含在上述初始化代码中)会自动将事件路由至本地服务。
**使用真实DSN验证:**
```ruby
Sentry.capture_message("Sentry Ruby SDK test")无事件显示?设置并查看标准输出。验证DSN格式是否正确:。
config.debug = truehttps://<key>@o<org>.ingest.sentry.io/<project>Phase 4: Cross-Link
阶段4:跨端关联
bash
cat package.json frontend/package.json web/package.json 2>/dev/null | grep -E '"@sentry|"sentry-'| Frontend detected | Suggest |
|---|---|
| React / Next.js | |
| Svelte / SvelteKit | |
| Vue | |
For trace stitching between Ruby backend and JS frontend, see → "Frontend trace stitching".
references/tracing.mdbash
cat package.json frontend/package.json web/package.json 2>/dev/null | grep -E '"@sentry|"sentry-'| 检测到前端框架 | 推荐配置 |
|---|---|
| React / Next.js | |
| Svelte / SvelteKit | |
| Vue | |
如需实现Ruby后端与JS前端的链路追踪关联,请参考 → “Frontend trace stitching”章节。
references/tracing.mdTroubleshooting
故障排查
| Issue | Solution |
|---|---|
| Events not appearing | |
| Rails exceptions missing | Must use |
| No traces | Set |
| Sidekiq jobs not traced | Add |
| Missing request context | Set |
| Logs not appearing | Set |
| Metrics not appearing | Check |
| 问题 | 解决方案 |
|---|---|
| 事件未显示 | 设置 |
| Rails异常未捕获 | 必须使用 |
| 无链路追踪数据 | 设置 |
| Sidekiq任务无追踪数据 | 添加 |
| 请求上下文缺失 | 设置 |
| 日志未显示 | 设置 |
| 指标数据未显示 | 确认 |