|
| 1 | +--- |
| 2 | +title: GoodJob |
| 3 | +description: "Learn about using Sentry with GoodJob, an ActiveJob adapter for Postgres-based job queuing." |
| 4 | +--- |
| 5 | + |
| 6 | +The GoodJob integration adds support for [GoodJob](https://github.com/bensheldon/good_job), a multithreaded, Postgres-based ActiveJob backend for Ruby on Rails. This integration provides automatic error capture with enriched context, performance monitoring with execution time and queue latency tracking, and cron monitoring for scheduled jobs. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +Install `sentry-good_job`: |
| 11 | + |
| 12 | +```bash |
| 13 | +gem install sentry-good_job |
| 14 | +``` |
| 15 | + |
| 16 | +Or add it to your `Gemfile`: |
| 17 | + |
| 18 | +```ruby |
| 19 | +gem "sentry-ruby" |
| 20 | +gem "sentry-good_job" |
| 21 | +``` |
| 22 | + |
| 23 | +## Configure |
| 24 | + |
| 25 | +### Automatic Setup with Rails |
| 26 | + |
| 27 | +If you're using Rails and have GoodJob in your dependencies, the integration will be enabled automatically when you initialize the Sentry SDK. |
| 28 | + |
| 29 | +```ruby {filename:config/initializers/sentry.rb} |
| 30 | +Sentry.init do |config| |
| 31 | + config.dsn = "___PUBLIC_DSN___" |
| 32 | + config.breadcrumbs_logger = [:active_support_logger, :http_logger] |
| 33 | + |
| 34 | + # Set traces_sample_rate to 1.0 to capture 100% |
| 35 | + # of transactions for tracing. |
| 36 | + config.traces_sample_rate = 1.0 |
| 37 | +end |
| 38 | +``` |
| 39 | + |
| 40 | +### Manual Setup |
| 41 | + |
| 42 | +For non-Rails applications or when you need more control, you can configure the integration explicitly: |
| 43 | + |
| 44 | +```ruby |
| 45 | +require "sentry-ruby" |
| 46 | +require "sentry-good_job" |
| 47 | + |
| 48 | +Sentry.init do |config| |
| 49 | + config.dsn = "___PUBLIC_DSN___" |
| 50 | + config.traces_sample_rate = 1.0 |
| 51 | + |
| 52 | + # Configure GoodJob-specific options |
| 53 | + config.good_job.report_after_job_retries = false |
| 54 | + config.good_job.include_job_arguments = false |
| 55 | + config.good_job.auto_setup_cron_monitoring = true |
| 56 | +end |
| 57 | +``` |
| 58 | + |
| 59 | +<Alert> |
| 60 | + Make sure that `Sentry.init` is called before GoodJob workers start processing |
| 61 | + jobs. For Rails applications, placing the initialization in |
| 62 | + `config/initializers/sentry.rb` ensures proper setup. |
| 63 | +</Alert> |
| 64 | + |
| 65 | +## Verify |
| 66 | + |
| 67 | +To verify that the integration is working, create a job that raises an error: |
| 68 | + |
| 69 | +```ruby {filename:app/jobs/debug_job.rb} |
| 70 | +class DebugJob < ApplicationJob |
| 71 | + queue_as :default |
| 72 | + |
| 73 | + def perform |
| 74 | + 1 / 0 # Intentional error |
| 75 | + end |
| 76 | +end |
| 77 | +``` |
| 78 | + |
| 79 | +Enqueue the job: |
| 80 | + |
| 81 | +```ruby |
| 82 | +DebugJob.perform_later |
| 83 | +``` |
| 84 | + |
| 85 | +When the job is processed by GoodJob, the error will be captured and sent to Sentry. You'll see: |
| 86 | + |
| 87 | +- An error event with the exception details |
| 88 | +- Enriched context including job name, queue name, and job ID |
| 89 | +- A performance transaction showing job execution time and queue latency |
| 90 | + |
| 91 | +View the error in the **Issues** section and the performance data in the **Performance** section of [sentry.io](https://sentry.io). |
| 92 | + |
| 93 | +## Features |
| 94 | + |
| 95 | +### Error Capture |
| 96 | + |
| 97 | +The integration automatically captures exceptions raised during job execution: |
| 98 | + |
| 99 | +- Exceptions are captured with full context (job name, queue, arguments if enabled, job ID) |
| 100 | +- Trace propagation across job executions |
| 101 | +- Configurable error reporting (after retries, only dead jobs, etc.) |
| 102 | + |
| 103 | +### Performance Monitoring |
| 104 | + |
| 105 | +Job execution is automatically instrumented with performance monitoring: |
| 106 | + |
| 107 | +- **Execution time**: Time spent executing the job |
| 108 | +- **Queue latency**: Time job spent waiting in the queue before execution |
| 109 | +- **Trace propagation**: Jobs maintain trace context from the code that enqueued them |
| 110 | + |
| 111 | +Transactions are created with the name `queue.active_job/<JobClassName>` and include: |
| 112 | + |
| 113 | +- A span for the job execution |
| 114 | +- Queue latency measurement |
| 115 | +- Breadcrumbs for job lifecycle events |
| 116 | + |
| 117 | +### Cron Monitoring |
| 118 | + |
| 119 | +The integration provides two ways to monitor scheduled jobs: |
| 120 | + |
| 121 | +#### Automatic Setup |
| 122 | + |
| 123 | +GoodJob cron configurations are automatically detected and monitored: |
| 124 | + |
| 125 | +```ruby {filename:config/initializers/good_job.rb} |
| 126 | +Rails.application.configure do |
| 127 | + config.good_job.cron = { |
| 128 | + example_job: { |
| 129 | + cron: "0 0 * * *", # Daily at midnight |
| 130 | + class: "ExampleJob" |
| 131 | + } |
| 132 | + } |
| 133 | +end |
| 134 | +``` |
| 135 | + |
| 136 | +With `auto_setup_cron_monitoring` enabled (default), Sentry will automatically create cron monitors for all jobs in your GoodJob cron configuration. Monitor slugs are generated from the cron key. |
| 137 | + |
| 138 | +<Alert> |
| 139 | + Cron monitors are created when your application starts and the GoodJob |
| 140 | + configuration is loaded. You don't need to create monitors manually in Sentry. |
| 141 | +</Alert> |
| 142 | + |
| 143 | +#### Manual Setup |
| 144 | + |
| 145 | +For more control over cron monitoring, use the `sentry_cron_monitor` method in your job: |
| 146 | + |
| 147 | +```ruby {filename:app/jobs/scheduled_cleanup_job.rb} |
| 148 | +class ScheduledCleanupJob < ApplicationJob |
| 149 | + include GoodJob::ActiveJobExtensions::Crons |
| 150 | + |
| 151 | + sentry_cron_monitor( |
| 152 | + schedule: { cron: "0 2 * * *" }, # 2 AM daily |
| 153 | + timezone: "America/New_York" |
| 154 | + ) |
| 155 | + |
| 156 | + def perform |
| 157 | + # Cleanup logic |
| 158 | + end |
| 159 | +end |
| 160 | +``` |
| 161 | + |
| 162 | +The `sentry_cron_monitor` method accepts: |
| 163 | + |
| 164 | +- `schedule`: Cron schedule hash (e.g., `{ cron: "0 * * * *" }`) |
| 165 | +- `timezone`: Timezone for the schedule (optional, defaults to UTC) |
| 166 | + |
| 167 | +<Alert level="info"> |
| 168 | + If you use manual cron monitoring with `sentry_cron_monitor`, set |
| 169 | + `auto_setup_cron_monitoring` to `false` to avoid duplicate monitors. |
| 170 | +</Alert> |
| 171 | + |
| 172 | +View your monitored jobs at [sentry.io/insights/crons](https://sentry.io/insights/crons/). |
| 173 | + |
| 174 | +## Options |
| 175 | + |
| 176 | +Configure the GoodJob integration with these options: |
| 177 | + |
| 178 | +### `report_after_job_retries` |
| 179 | + |
| 180 | +<SdkOption name="report_after_job_retries" type="Boolean" defaultValue="false"> |
| 181 | + |
| 182 | +Only report errors to Sentry after all retry attempts have been exhausted. |
| 183 | + |
| 184 | +When `true`, errors are only sent to Sentry after the job has failed its final retry attempt. When `false`, errors are reported on every failure, including during retries. |
| 185 | + |
| 186 | +```ruby |
| 187 | +Sentry.init do |config| |
| 188 | + config.dsn = "___PUBLIC_DSN___" |
| 189 | + config.good_job.report_after_job_retries = true |
| 190 | +end |
| 191 | +``` |
| 192 | + |
| 193 | +</SdkOption> |
| 194 | + |
| 195 | +### `report_only_dead_jobs` |
| 196 | + |
| 197 | +<SdkOption name="report_only_dead_jobs" type="Boolean" defaultValue="false"> |
| 198 | + |
| 199 | +Only report errors for jobs that cannot be retried (dead jobs). |
| 200 | + |
| 201 | +When `true`, errors are only sent to Sentry for jobs that have permanently failed and won't be retried. This is stricter than `report_after_job_retries`. |
| 202 | + |
| 203 | +```ruby |
| 204 | +Sentry.init do |config| |
| 205 | + config.dsn = "___PUBLIC_DSN___" |
| 206 | + config.good_job.report_only_dead_jobs = true |
| 207 | +end |
| 208 | +``` |
| 209 | + |
| 210 | +</SdkOption> |
| 211 | + |
| 212 | +### `include_job_arguments` |
| 213 | + |
| 214 | +<SdkOption name="include_job_arguments" type="Boolean" defaultValue="false"> |
| 215 | + |
| 216 | +Include job arguments in error context sent to Sentry. |
| 217 | + |
| 218 | +When `true`, job arguments are included in the event's extra context. **Warning**: This may expose sensitive data. Only enable this if you're certain your job arguments don't contain PII or sensitive information. |
| 219 | + |
| 220 | +```ruby |
| 221 | +Sentry.init do |config| |
| 222 | + config.dsn = "___PUBLIC_DSN___" |
| 223 | + config.good_job.include_job_arguments = true |
| 224 | +end |
| 225 | +``` |
| 226 | + |
| 227 | +<Alert level="warning" title="Sensitive Data"> |
| 228 | + Job arguments may contain personally identifiable information (PII) or other |
| 229 | + sensitive data. Only enable this option if you've reviewed your job arguments |
| 230 | + and are certain they don't contain sensitive information, or if you've |
| 231 | + configured [data scrubbing](/platforms/ruby/data-management/sensitive-data/) |
| 232 | + appropriately. |
| 233 | +</Alert> |
| 234 | + |
| 235 | +</SdkOption> |
| 236 | + |
| 237 | +### `auto_setup_cron_monitoring` |
| 238 | + |
| 239 | +<SdkOption name="auto_setup_cron_monitoring" type="Boolean" defaultValue="true"> |
| 240 | + |
| 241 | +Automatically set up cron monitoring by reading GoodJob's cron configuration. |
| 242 | + |
| 243 | +When `true`, the integration scans your GoodJob cron configuration and automatically creates Sentry cron monitors for scheduled jobs. |
| 244 | + |
| 245 | +```ruby |
| 246 | +Sentry.init do |config| |
| 247 | + config.dsn = "___PUBLIC_DSN___" |
| 248 | + config.good_job.auto_setup_cron_monitoring = false |
| 249 | +end |
| 250 | +``` |
| 251 | + |
| 252 | +Disable this if you prefer to use manual cron monitoring with the `sentry_cron_monitor` method. |
| 253 | + |
| 254 | +</SdkOption> |
| 255 | + |
| 256 | +### `logging_enabled` |
| 257 | + |
| 258 | +<SdkOption name="logging_enabled" type="Boolean" defaultValue="false"> |
| 259 | + |
| 260 | +Enable detailed logging for debugging the integration. |
| 261 | + |
| 262 | +When `true`, the integration logs detailed information about job monitoring, cron setup, and error capture. Useful for troubleshooting but should be disabled in production. |
| 263 | + |
| 264 | +```ruby |
| 265 | +Sentry.init do |config| |
| 266 | + config.dsn = "___PUBLIC_DSN___" |
| 267 | + config.good_job.logging_enabled = true # Only for debugging |
| 268 | +end |
| 269 | +``` |
| 270 | + |
| 271 | +</SdkOption> |
| 272 | + |
| 273 | +## Supported Versions |
| 274 | + |
| 275 | +- Ruby: 2.4+ |
| 276 | +- Rails: 5.2+ |
| 277 | +- GoodJob: 3.0+ |
| 278 | +- Sentry Ruby SDK: 5.28.0+ |
0 commit comments