|
| 1 | +# Sentry and Ruby LLM Rules |
| 2 | + |
| 3 | +You are an expert in application architecture and the configuration of observability tools like Sentry. Ensure you are following these instructions, while taking into account the user's request. |
| 4 | + |
| 5 | +Always ensure you are installing Sentry using `gem 'sentry-ruby', '~> 5.x'`. The latest package should always be used unless explicitly told otherwise. |
| 6 | + |
| 7 | +## Configuration examples |
| 8 | + |
| 9 | +These examples should be used to configure the different Sentry functionality in Ruby applications. Use these as the default configuration, factoring in the user's requests. |
| 10 | + |
| 11 | +### Sentry Setup (applies to all configurations) |
| 12 | + |
| 13 | +Configure Sentry using the following parameters unless explicitly told otherwise. |
| 14 | + |
| 15 | +```ruby |
| 16 | +require 'sentry-ruby' |
| 17 | + |
| 18 | +Sentry.init do |config| |
| 19 | + config.dsn = '<sentry dsn>' |
| 20 | + |
| 21 | + # Get breadcrumbs from logs |
| 22 | + config.breadcrumbs_logger = [:sentry_logger, :http_logger] |
| 23 | + |
| 24 | + # Add data like request headers and IP for users |
| 25 | + config.send_default_pii = true |
| 26 | + |
| 27 | + # Enable tracing - adjust in production |
| 28 | + config.traces_sample_rate = 1.0 |
| 29 | + |
| 30 | + # Enable profiling - relative to traces_sample_rate |
| 31 | + config.profiles_sample_rate = 1.0 |
| 32 | +end |
| 33 | +``` |
| 34 | + |
| 35 | +### Rails Setup |
| 36 | + |
| 37 | +For Rails applications, create a file in `config/initializers/sentry.rb` with the following: |
| 38 | + |
| 39 | +```ruby |
| 40 | +Sentry.init do |config| |
| 41 | + config.dsn = '<sentry dsn>' |
| 42 | + |
| 43 | + # Get breadcrumbs from logs |
| 44 | + config.breadcrumbs_logger = [:active_support_logger, :http_logger] |
| 45 | + |
| 46 | + # Add data like request headers and IP for users |
| 47 | + config.send_default_pii = true |
| 48 | + |
| 49 | + # Enable tracing - adjust in production |
| 50 | + config.traces_sample_rate = 1.0 |
| 51 | + |
| 52 | + # Enable profiling - relative to traces_sample_rate |
| 53 | + config.profiles_sample_rate = 1.0 |
| 54 | + |
| 55 | + # To set a uniform sample rate |
| 56 | + # config.sample_rate = 0.5 |
| 57 | +end |
| 58 | +``` |
| 59 | + |
| 60 | +### Rack Setup |
| 61 | + |
| 62 | +For Rack applications, set up Sentry in your `config.ru` or rackup file: |
| 63 | + |
| 64 | +```ruby |
| 65 | +require 'sentry-ruby' |
| 66 | + |
| 67 | +Sentry.init do |config| |
| 68 | + config.dsn = '<sentry dsn>' |
| 69 | + config.breadcrumbs_logger = [:sentry_logger, :http_logger] |
| 70 | + config.send_default_pii = true |
| 71 | + config.traces_sample_rate = 1.0 |
| 72 | + config.profiles_sample_rate = 1.0 |
| 73 | +end |
| 74 | + |
| 75 | +use Sentry::Rack::CaptureExceptions |
| 76 | +``` |
| 77 | + |
| 78 | +### Error Tracking and Exception Catching |
| 79 | + |
| 80 | +Instrument errors throughout the application using the following approaches: |
| 81 | + |
| 82 | +```ruby |
| 83 | +# Explicitly capture an exception |
| 84 | +begin |
| 85 | + 1 / 0 |
| 86 | +rescue ZeroDivisionError => exception |
| 87 | + Sentry.capture_exception(exception) |
| 88 | +end |
| 89 | + |
| 90 | +# Capture a custom message |
| 91 | +Sentry.capture_message('Something went wrong') |
| 92 | + |
| 93 | +# Add context to events |
| 94 | +Sentry.configure_scope do |scope| |
| 95 | + scope.set_user(id: 1, email: " [email protected]") |
| 96 | + scope.set_tag(:page_locale, "de-at") |
| 97 | + scope.set_extra(:character_name, "Mighty Fighter") |
| 98 | +end |
| 99 | +``` |
| 100 | + |
| 101 | +### Tracing and Performance Monitoring |
| 102 | + |
| 103 | +Utilize the following examples for tracing scenarios: |
| 104 | + |
| 105 | +```ruby |
| 106 | +# Create a transaction |
| 107 | +Sentry.start_transaction(name: "task_name", op: "task") do |transaction| |
| 108 | + # Create a child span |
| 109 | + Sentry.start_span(op: "subtask", description: "Subtask description") do |span| |
| 110 | + begin |
| 111 | + # Your code here |
| 112 | + span.set_data(:key, "value") |
| 113 | + rescue => e |
| 114 | + # Record failure information |
| 115 | + span.set_status(:internal_error) |
| 116 | + raise e |
| 117 | + end |
| 118 | + end |
| 119 | +end |
| 120 | +``` |
| 121 | + |
| 122 | +### Background Job Integration |
| 123 | + |
| 124 | +For Sidekiq, use the following setup: |
| 125 | + |
| 126 | +```ruby |
| 127 | +require 'sentry-ruby' |
| 128 | +require 'sentry-sidekiq' |
| 129 | + |
| 130 | +Sentry.init do |config| |
| 131 | + config.dsn = '<sentry dsn>' |
| 132 | + config.traces_sample_rate = 1.0 |
| 133 | +end |
| 134 | +``` |
0 commit comments