Skip to content

Commit

Permalink
add delayed job context into the sampling context (#2148)
Browse files Browse the repository at this point in the history
* copy delayed job context into the transaction via custom_sampling_context

* add changelog entry

* add spec
  • Loading branch information
rlineweaver authored Oct 28, 2023
1 parent d315a15 commit dbbc7e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- Make additional job context available to traces_sampler for determining sample rate (sentry-delayed_job) [#2148](https://github.com/getsentry/sentry-ruby/pull/2148)
- Add new `config.rails.active_support_logger_subscription_items` to allow customization breadcrumb data of active support logger [#2139](https://github.com/getsentry/sentry-ruby/pull/2139)

```rb
Expand Down
7 changes: 6 additions & 1 deletion sentry-delayed_job/lib/sentry/delayed_job/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ class Plugin < ::Delayed::Plugin
scope.set_contexts(**contexts)
scope.set_tags("delayed_job.queue" => job.queue, "delayed_job.id" => job.id.to_s)

transaction = Sentry.start_transaction(name: scope.transaction_name, source: scope.transaction_source, op: OP_NAME)
transaction = Sentry.start_transaction(
name: scope.transaction_name,
source: scope.transaction_source,
op: OP_NAME,
custom_sampling_context: contexts
)
scope.set_span(transaction) if transaction

begin
Expand Down
11 changes: 11 additions & 0 deletions sentry-delayed_job/spec/sentry/delayed_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ def perform
expect(transaction.contexts.dig(:trace, :status)).to eq("ok")
end

it "passes job context into the sampling context" do
expect_any_instance_of(Sentry::Transaction).to receive(:set_initial_sample_decision) do |**args|
expect(args.dig(:sampling_context, Sentry::DelayedJob::Plugin::DELAYED_JOB_CONTEXT_KEY, :priority)).to eq(7)
expect(args.dig(:sampling_context, Sentry::DelayedJob::Plugin::ACTIVE_JOB_CONTEXT_KEY, :job_class)).to eq('ReportingJob')
end
ReportingJob.set(priority: 7).perform_later

enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
end

it "records transaction with exception" do
FailedJob.perform_later
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
Expand Down

0 comments on commit dbbc7e4

Please sign in to comment.