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
1
commit into
chore/3119-switch-activejob-integration-to-the-backend-extension-registry
from
chore/2937-migrate-sentry-sidekiq-to-aj-first-integration
+1,483
−217
Open
Changes from all commits
Commits
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
| 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
106 changes: 106 additions & 0 deletions
106
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,106 @@ | ||
| # 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 || active_job?(job) | ||
| end | ||
|
|
||
| def scope_data(job:) | ||
| return super unless active_job?(job) | ||
|
|
||
| sidekiq_job = handed_over_sidekiq_job(job) | ||
| sidekiq_job ? super(job: sidekiq_job) : {} | ||
| end | ||
|
|
||
| def messaging_data(job:) | ||
| return super unless 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 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) | ||
|
|
||
| sidekiq_job = sidekiq_job_in(payload) | ||
| super(job: job, payload: sidekiq_job) if sidekiq_job | ||
| end | ||
|
|
||
| def transaction_name(job:) | ||
| active_job?(job) ? "#{self.class::SIDEKIQ_NAME}/#{job.class.name}" : super | ||
| end | ||
|
|
||
| def span_origin(job:) | ||
| active_job?(job) ? self.class::SPAN_ORIGIN : super | ||
| end | ||
|
|
||
| def integration(job:) | ||
| active_job?(job) ? Sentry::Sidekiq : super | ||
| end | ||
|
|
||
| def report_retried_exception?(job:, exception:) | ||
| active_job?(job) ? false : super | ||
| end | ||
|
|
||
| def performed_by_active_job?(sidekiq_job) | ||
| !!sidekiq_job["wrapped"] && active_job_owns_adapter? | ||
| 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 active_job?(job) | ||
| return false unless defined?(::ActiveJob::Base) && job.is_a?(::ActiveJob::Base) | ||
|
|
||
| job.class.queue_adapter.class.name == ACTIVE_JOB_ADAPTER | ||
| 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "sentry/sidekiq/retry_policy" | ||
|
|
||
| module Sentry | ||
| module Sidekiq | ||
| class FailureReporting | ||
| def initialize(retry_policy: RetryPolicy.build) | ||
| @retry_policy = retry_policy | ||
| end | ||
|
|
||
| def report?(job, sidekiq_config = nil) | ||
| config = Sentry.configuration.sidekiq | ||
| retry_limit = @retry_policy.limit(job, sidekiq_config) | ||
|
|
||
| return false if config.report_after_job_retries && retries_pending?(job, retry_limit) | ||
| return false if config.report_only_dead_jobs && job_value(job, "dead") == false | ||
| return false if below_attempt_threshold?(job, retry_limit) | ||
|
|
||
| true | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def retries_pending?(job, retry_limit) | ||
| return false unless @retry_policy.retryable?(job) | ||
|
|
||
| retry_count = job_value(job, "retry_count") | ||
| retry_count.nil? || retry_count < retry_limit - 1 | ||
| end | ||
|
|
||
| def below_attempt_threshold?(job, retry_limit) | ||
| attempt_threshold = job_value(job, "attempt_threshold") | ||
| return false unless attempt_threshold && @retry_policy.retryable?(job) | ||
|
|
||
| [attempt_threshold.to_i, retry_limit + 1].min > attempt(job) | ||
| end | ||
|
|
||
| def attempt(job) | ||
| retry_count = job_value(job, "retry_count") | ||
| retry_count.nil? ? 1 : retry_count.to_i + 2 | ||
| end | ||
|
|
||
| def job_value(job, key) | ||
| job && job[key] | ||
| end | ||
| end | ||
| end | ||
| end |
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.