Repository navigation
feat(sidekiq): migrate to ActiveJob-first integration #3134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
solnic
wants to merge
2
commits into
chore/3119-switch-activejob-integration-to-the-backend-extension-registry
from
chore/2937-migrate-sentry-sidekiq-to-aj-first-integration
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "bundler/gem_tasks" | ||
| require_relative "../lib/sentry/test/rake_tasks" | ||
|
|
||
| Sentry::Test::RakeTasks.define_spec_tasks( | ||
| spec_pattern: "spec/sentry/**/*_spec.rb", | ||
| spec_rspec_opts: "--order rand --format progress", | ||
| isolated_specs_pattern: "spec/isolated/**/*_spec.rb", | ||
| isolated_rspec_opts: "--format progress" | ||
| ) | ||
| require "rspec/core/rake_task" | ||
|
|
||
| RSpec::Core::RakeTask.new(:spec) do |task| | ||
| task.pattern = "spec/sentry/**/*_spec.rb" | ||
| task.rspec_opts = "--order rand --format progress" | ||
| end | ||
|
|
||
| namespace :spec do | ||
| isolated_tasks = Dir["spec/isolated/**/*_spec.rb"].sort.map do |file| | ||
| task_name = "isolated:#{file.delete_prefix("spec/isolated/").delete_suffix("_spec.rb")}" | ||
|
|
||
| RSpec::Core::RakeTask.new(task_name) do |task| | ||
| task.pattern = file | ||
| task.rspec_opts = "--format progress" | ||
| end | ||
|
|
||
| task_name | ||
| end | ||
|
|
||
| task isolated: isolated_tasks | ||
| end | ||
|
|
||
| task default: [:spec, :"spec:isolated"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
sentry-sidekiq/lib/sentry/sidekiq/active_job_support/job_extension.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| module Sidekiq | ||
| module ActiveJobSupport | ||
| module JobExtension | ||
| ACTIVE_JOB_ADAPTER = "ActiveJob::QueueAdapters::SidekiqAdapter" | ||
| SIDEKIQ_JOB_KEY = "_sentry_sidekiq_job" | ||
|
|
||
| def handles?(job) | ||
| super || handles_active_job?(job) | ||
| end | ||
|
|
||
| def scope_data(job:) | ||
| return super unless handles_active_job?(job) | ||
|
|
||
| sidekiq_job = handed_over_sidekiq_job(job) | ||
| sidekiq_job ? super(job: sidekiq_job) : {} | ||
| end | ||
|
|
||
| def messaging_data(job:) | ||
| return super unless handles_active_job?(job) | ||
|
|
||
| sidekiq_job = handed_over_sidekiq_job(job) | ||
| return {} unless sidekiq_job | ||
|
|
||
| super(job: sidekiq_job).merge(body_size: body_size(sidekiq_job)).compact | ||
| end | ||
|
|
||
| def retry_info(job:) | ||
| return super unless handles_active_job?(job) | ||
|
|
||
| sidekiq_job = handed_over_sidekiq_job(job) | ||
| super(job: sidekiq_job) if sidekiq_job | ||
| end | ||
|
|
||
| def extract_trace_headers(job:, payload:) | ||
| return super if sidekiq_job?(payload) | ||
| return unless handles_active_job?(job) | ||
|
|
||
| sidekiq_job = sidekiq_job_in(payload) | ||
| super(job: job, payload: sidekiq_job) if sidekiq_job | ||
| end | ||
|
|
||
| def transaction_name(job:) | ||
| handles_active_job?(job) ? "#{self.class::SIDEKIQ_NAME}/#{job.class.name}" : super | ||
| end | ||
|
|
||
| def span_origin(job:) | ||
| handles_active_job?(job) ? self.class::SPAN_ORIGIN : super | ||
| end | ||
|
|
||
| def integration(job:) | ||
| handles_active_job?(job) ? Sentry::Sidekiq : super | ||
| end | ||
|
|
||
| def report_retried_exception?(job:, exception:) | ||
| handles_active_job?(job) ? false : super | ||
| end | ||
|
|
||
| def performed_by_active_job?(sidekiq_job) | ||
| !!sidekiq_job["wrapped"] && active_job_handoff? | ||
| end | ||
|
|
||
| def active_job_args(sidekiq_job, scope:) | ||
| payload, *arguments = sidekiq_job["args"] | ||
| return sidekiq_job["args"] unless payload.is_a?(Hash) | ||
|
|
||
| sentry_data = (payload["_sentry"] || {}).merge("delivery_queue" => sidekiq_job["queue"]) | ||
| sentry_data["execution_scope_id"] = scope.object_id | ||
| [payload.merge( | ||
| SIDEKIQ_JOB_KEY => sidekiq_job, | ||
| "_sentry" => sentry_data | ||
| ), *arguments] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def handles_active_job?(job) | ||
| return false unless defined?(::ActiveJob::Base) && job.is_a?(::ActiveJob::Base) | ||
|
|
||
| job.class.queue_adapter.class.name == ACTIVE_JOB_ADAPTER && active_job_handoff? | ||
| end | ||
|
|
||
| def active_job_handoff? | ||
| active_job_owns_adapter? && Sentry.configuration.sidekiq.active_job_integration == :rails | ||
| end | ||
|
|
||
| def active_job_owns_adapter? | ||
| return false unless defined?(::Sentry::Rails::ActiveJobExtensions) | ||
| return false unless ::Sentry::Rails.respond_to?(:active_job_owns_adapter?) | ||
|
|
||
| ::Sentry::Rails.active_job_owns_adapter?(ACTIVE_JOB_ADAPTER) | ||
| end | ||
|
|
||
| def handed_over_sidekiq_job(job) | ||
| return unless handles_active_job?(job) && job.respond_to?(:_sentry_job_data) | ||
|
|
||
| sidekiq_job_in(job._sentry_job_data) | ||
| end | ||
|
|
||
| def sidekiq_job_in(active_job_payload) | ||
| return unless active_job_payload.is_a?(Hash) | ||
|
|
||
| sidekiq_job = active_job_payload[SIDEKIQ_JOB_KEY] | ||
| sidekiq_job if sidekiq_job?(sidekiq_job) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
32 changes: 32 additions & 0 deletions
32
sentry-sidekiq/lib/sentry/sidekiq/active_job_support/server_middleware.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| module Sidekiq | ||
| module ActiveJobSupport | ||
| module ServerMiddleware | ||
| private | ||
|
|
||
| def execution_trace_headers(job, sidekiq_job) | ||
| if job_extension.performed_by_active_job?(job) && job["args"].first.is_a?(Hash) | ||
| headers = job["args"].first.dig("_sentry", "trace_propagation_headers") | ||
| return headers if headers && !headers.empty? | ||
| end | ||
|
|
||
| super | ||
| end | ||
|
|
||
| def perform_job(job, sidekiq_job, scope:) | ||
| return super unless job_extension.performed_by_active_job?(job) | ||
|
|
||
| args = job["args"] | ||
| begin | ||
| job["args"] = job_extension.active_job_args(sidekiq_job, scope: scope) | ||
| super | ||
| ensure | ||
| job["args"] = args | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.