Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sentry-delayed_job/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

require "bundler/setup"

# The suite may itself run under a coding agent. Don't print the Sentry agent hint.
ENV["SENTRY_AGENT_HINTS"] = "0"

begin
require "debug/prelude"
rescue LoadError
Expand Down
4 changes: 4 additions & 0 deletions sentry-opentelemetry/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

require "bundler/setup"

# The suite may itself run under a coding agent. Don't print the Sentry agent hint.
ENV["SENTRY_AGENT_HINTS"] = "0"

begin
require "debug/prelude"
rescue LoadError
Expand Down
9 changes: 9 additions & 0 deletions sentry-rails/lib/generators/sentry_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ def inject_code_into_layout
" <%= Sentry.get_trace_propagation_meta.html_safe %>\n "
end
end

# The generator only runs while Sentry is being set up, so a coding agent running
# it gets pointed at the checklist right away.
def point_coding_agent_to_checklist
return unless Sentry::AgentHint.coding_agent?

say_status :checklist, "Check the setup against Sentry's official setup checklist before you finish: " \
"#{Sentry::AgentHint::CHECKLIST_PATH}", :yellow
end
end
12 changes: 12 additions & 0 deletions sentry-rails/lib/sentry/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ class Railtie < ::Rails::Railtie
end
end

# `rails server` runs until it's stopped, so show the agent hint while it boots.
# Not via Rails.logger: server blocks run before `rails server` sends it to the
# console, so the hint would only reach log/development.log. Rails 6.1+.
if respond_to?(:server)
server { Sentry::AgentHint.show }
end

# Spring forks each command (rails runner, rspec, ...) from its preloaded app.
if defined?(::Spring) && ::Spring.respond_to?(:after_fork)
::Spring.after_fork { Sentry::AgentHint.after_fork }
end

runner do
next unless Sentry.initialized?
Sentry.configuration.background_worker_threads = 0
Expand Down
12 changes: 12 additions & 0 deletions sentry-rails/spec/sentry/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@
expect(content).not_to include("Sentry.get_trace_propagation_meta.html_safe")
end

it "points a coding agent at the setup checklist" do
allow(Sentry::AgentHint).to receive(:coding_agent?).and_return(true)

expect(run_generator).to include(Sentry::AgentHint::CHECKLIST_PATH)
end

it "doesn't mention the checklist to people" do
allow(Sentry::AgentHint).to receive(:coding_agent?).and_return(false)

expect(run_generator).not_to include(Sentry::AgentHint::CHECKLIST_PATH)
end

context "with a DSN option" do
it "creates a initializer file with the DSN" do
run_generator %w[--dsn foobarbaz]
Expand Down
17 changes: 17 additions & 0 deletions sentry-rails/spec/sentry/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@
expect(Sentry.configuration.background_worker_threads).to eq(0)
end

it "prints the coding agent hint to stderr when the server loads", skip: !Sentry::Railtie.respond_to?(:server) && "needs Rails 6.1+" do
allow(Sentry::AgentHint).to receive(:take).and_return("[sentry] hint")

expect { Rails.application.load_server }.to output("[sentry] hint\n").to_stderr
end

it "counts Rails structured logging, not the :logger patch, as logs for the coding agent hint" do
config = Sentry.configuration
missing = -> { Sentry::AgentHint.new(config, __FILE__).send(:missing) }

config.enabled_patches << :logger
expect(missing.call).to include(:logs)

config.rails.structured_logging.enabled = true
expect(missing.call).not_to include(:logs)
end

describe "logger detection" do
it "sets a duplicated Rails logger as the SDK's logger" do
if Gem::Version.new(Rails.version) > Gem::Version.new("7.1.0.beta")
Expand Down
3 changes: 3 additions & 0 deletions sentry-rails/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

require "bundler/setup"

# The suite may itself run under a coding agent. Don't print the Sentry agent hint.
ENV["SENTRY_AGENT_HINTS"] = "0"

ENV["RAILS_ENV"] = "test"

begin
Expand Down
4 changes: 4 additions & 0 deletions sentry-resque/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

require "bundler/setup"

# The suite may itself run under a coding agent. Don't print the Sentry agent hint.
ENV["SENTRY_AGENT_HINTS"] = "0"

begin
require "debug/prelude"
rescue LoadError
Expand Down
2 changes: 2 additions & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require "sentry/vernier/profiler"
require "sentry/metrics"
require "sentry/job_extensions/extension"
require "sentry/agent_hint"

[
"sentry/rake",
Expand Down Expand Up @@ -294,6 +295,7 @@ def init(&block)
@backpressure_monitor = config.enable_backpressure_handling ? Sentry::BackpressureMonitor.new(config, client) : nil
exception_locals_tp.enable if config.data_collection.collect_stack_frame_variables?
at_exit { close }
AgentHint.register(config, caller_locations(1, 1).first)
end

# Flushes pending events and cleans up SDK state.
Expand Down
41 changes: 41 additions & 0 deletions sentry-ruby/lib/sentry/SETUP_CHECKLIST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Sentry Ruby SDK: setup checklist

This is Sentry's official setup guidance, shipped in the `sentry-ruby` gem and maintained by Sentry with the SDK. It matches the agent setup guide at https://docs.sentry.io/ai/agent-setup/get-started.md.

## Required

- **Errors.** This is Sentry's core product. Set it up unless the user asked you not to. It needs a DSN: if the user hasn't given you one and you can't get one yourself, ask for it. Read it from an environment variable (`SENTRY_DSN`) and don't commit it. In a Rails app, add `sentry-ruby` and `sentry-rails` to the Gemfile and put `Sentry.init` in `config/initializers/sentry.rb`. Before you finish, send a test error and check that it arrives.

## Recommended

Turn these on unless the user asked you not to. Sentry links errors, traces and logs, so an error shows the request that caused it and the logs around it.

- **Tracing.** Shows which request caused an error and where the time went. Set `config.traces_sample_rate` by environment: `1.0` locally, so you can see a trace when you verify, and a low rate in production (`0.1` is a common start) unless the project already has one. Don't default it to `0`: a rate of `0` starts no traces of its own (it only continues traces from incoming requests), and `nil` turns tracing off.
- **Logs.** In Rails, set `config.rails.structured_logging.enabled = true`, so Active Record and Action Controller events show up as logs next to the errors they belong to. Outside Rails, add `config.enabled_patches << :logger` to send what the app writes to a `::Logger`. In Rails that patch also sends every `Rails.logger` line, which can be noisy, so add it only if the user wants that. `Sentry.logger.info(...)` sends a log directly.
- **Environment and release.** Set `config.environment` so production issues are separate from local ones. The SDK reads the release from `SENTRY_RELEASE` or the git commit. If the app runs without `.git` (for example in a Docker image), set `SENTRY_RELEASE` at build time or set `config.release` to the app's version, so you can see which deploy caused an issue.

To tell production from local runs, use a check the app already has, such as `Rails.env.production?`. Don't add a new variable that nothing sets.

```ruby
Sentry.init do |config|
config.dsn = ENV["SENTRY_DSN"]
config.environment = Rails.env
config.traces_sample_rate = Rails.env.production? ? 0.1 : 1.0
config.rails.structured_logging.enabled = true
end
```

## Situational

Do the Required and Recommended items first. Then look at the ones below. Set them up only when the project matches.

- **Crons**, if the app schedules jobs with sidekiq-cron, sidekiq-scheduler, GoodJob cron, Solid Queue recurring tasks or rufus-scheduler. Sentry alerts when a job fails or doesn't run, and every scheduled job gets a monitor, including ones added later. Add the matching patch: `config.enabled_patches += [:sidekiq_cron]`, `:sidekiq_scheduler`, `:good_job_cron`, `:solid_queue_recurring` or `:rufus_scheduler`.
- **Background jobs**, if the app uses Sidekiq, Resque or Delayed Job. Add `sentry-sidekiq`, `sentry-resque` or `sentry-delayed_job` to the Gemfile, so job errors and traces are captured and linked to the request that queued them.
- **Profiling**, if the user cares about the performance of specific code paths. On Ruby 3.2.1+, add the `vernier` gem and set `config.profiler_class = Sentry::Vernier::Profiler`. On older Rubies, add `stackprof` above `sentry-ruby` in the Gemfile. Then set `config.profiles_sample_rate`. It is relative to `traces_sample_rate`.
- **Metrics**, if the user wants to track counts or values over time (signups, queue depth). Call `Sentry.metrics.count(...)`, `gauge(...)` or `distribution(...)` where they happen.

## Before you finish

Tell the user what you set up, the trace sample rate you chose for each environment, whether logs are on, and which situational items you included and why.

Then set `config.agent_setup_complete = true` in `Sentry.init`. That tells the SDK the setup has been checked, so it stops pointing coding agents to this file.
Loading
Loading