sentry-sidekiq 7.2.0 → 7.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +17 -1
- data/Rakefile +21 -8
- data/gemfiles/ruby-2.7_sidekiq-5.0.gemfile.lock +13 -13
- data/gemfiles/ruby-2.7_sidekiq-6.5.gemfile.lock +13 -13
- data/gemfiles/ruby-2.7_sidekiq-7.0.gemfile.lock +13 -13
- data/gemfiles/ruby-3.0_sidekiq-5.0.gemfile.lock +12 -12
- data/gemfiles/ruby-3.0_sidekiq-6.5.gemfile.lock +12 -12
- data/gemfiles/ruby-3.0_sidekiq-7.0.gemfile.lock +12 -12
- data/gemfiles/ruby-3.1_sidekiq-5.0.gemfile.lock +22 -22
- data/gemfiles/ruby-3.1_sidekiq-6.5.gemfile.lock +22 -22
- data/gemfiles/ruby-3.1_sidekiq-7.0.gemfile.lock +22 -22
- data/gemfiles/ruby-3.2_sidekiq-5.0.gemfile.lock +116 -116
- data/gemfiles/ruby-3.2_sidekiq-6.5.gemfile.lock +116 -116
- data/gemfiles/ruby-3.2_sidekiq-7.0.gemfile.lock +116 -116
- data/gemfiles/ruby-3.2_sidekiq-8.0.0.gemfile.lock +116 -116
- data/gemfiles/ruby-3.3_sidekiq-5.0.gemfile.lock +116 -116
- data/gemfiles/ruby-3.3_sidekiq-6.5.gemfile.lock +116 -116
- data/gemfiles/ruby-3.3_sidekiq-7.0.gemfile.lock +116 -116
- data/gemfiles/ruby-3.3_sidekiq-8.0.0.gemfile.lock +116 -116
- data/gemfiles/ruby-3.4_sidekiq-5.0.gemfile.lock +116 -116
- data/gemfiles/ruby-3.4_sidekiq-6.5.gemfile.lock +116 -116
- data/gemfiles/ruby-3.4_sidekiq-7.0.gemfile.lock +116 -116
- data/gemfiles/ruby-3.4_sidekiq-8.0.0.gemfile.lock +116 -116
- data/gemfiles/ruby-4.0_sidekiq-5.0.gemfile.lock +116 -116
- data/gemfiles/ruby-4.0_sidekiq-6.5.gemfile.lock +116 -116
- data/gemfiles/ruby-4.0_sidekiq-7.0.gemfile.lock +116 -116
- data/gemfiles/ruby-jruby-9.4.14.0_sidekiq-5.0.gemfile.lock +26 -26
- data/gemfiles/ruby-jruby-9.4.14.0_sidekiq-6.5.gemfile.lock +26 -26
- data/gemfiles/ruby-jruby-9.4.14.0_sidekiq-6.gemfile.lock +26 -26
- data/gemfiles/ruby-jruby-9.4.14.0_sidekiq-7.0.gemfile.lock +26 -26
- data/lib/sentry/sidekiq/active_job_extension.rb +44 -0
- data/lib/sentry/sidekiq/active_job_support/job_extension.rb +75 -0
- data/lib/sentry/sidekiq/active_job_support/server_middleware.rb +25 -0
- data/lib/sentry/sidekiq/context_filter.rb +4 -45
- data/lib/sentry/sidekiq/error_handler.rb +10 -71
- data/lib/sentry/sidekiq/failure_reporting.rb +49 -0
- data/lib/sentry/sidekiq/job_extension.rb +168 -0
- data/lib/sentry/sidekiq/railtie.rb +28 -0
- data/lib/sentry/sidekiq/retry_policy.rb +100 -0
- data/lib/sentry/sidekiq/sentry_context_middleware.rb +30 -81
- data/lib/sentry/sidekiq/version.rb +1 -1
- data/lib/sentry-sidekiq.rb +26 -7
- data/sentry-sidekiq.gemspec +1 -1
- metadata +15 -8
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "sentry/sidekiq/context_filter"
|
|
4
|
+
require "sentry/sidekiq/sentry_context_middleware"
|
|
4
5
|
|
|
5
6
|
module Sentry
|
|
6
7
|
module Sidekiq
|
|
7
8
|
class ErrorHandler
|
|
8
9
|
WITH_SIDEKIQ_7 = ::Gem::Version.new(::Sidekiq::VERSION) >= ::Gem::Version.new("7.0")
|
|
9
10
|
|
|
11
|
+
def initialize(extension = Sentry::Sidekiq.job_extension)
|
|
12
|
+
@extension = extension
|
|
13
|
+
end
|
|
14
|
+
|
|
10
15
|
# @param ex [Exception] the exception / error that occured
|
|
11
16
|
# @param context [Hash or Array] Sidekiq error context
|
|
12
17
|
# @param sidekiq_config [Sidekiq::Config, Hash] Sidekiq configuration,
|
|
@@ -16,83 +21,17 @@ module Sentry
|
|
|
16
21
|
def call(ex, context, sidekiq_config = nil)
|
|
17
22
|
return unless Sentry.initialized?
|
|
18
23
|
|
|
19
|
-
context_filter = Sentry::Sidekiq::ContextFilter.new(context)
|
|
20
|
-
|
|
21
24
|
scope = Sentry.get_current_scope
|
|
22
|
-
scope.set_transaction_name(
|
|
23
|
-
|
|
24
|
-
retry_lim = retry_limit(context, sidekiq_config)
|
|
25
|
-
|
|
26
|
-
# If Sentry is configured to only report an error _after_ all retries have been exhausted,
|
|
27
|
-
# and if the job is retryable, and have not exceeded the retry_limit,
|
|
28
|
-
# return early.
|
|
29
|
-
if Sentry.configuration.sidekiq.report_after_job_retries && retryable?(context)
|
|
30
|
-
retry_count = context.dig(:job, "retry_count")
|
|
31
|
-
if retry_count.nil? || retry_count < retry_lim - 1
|
|
32
|
-
return
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# See if we want to ignore jobs that have dead: false
|
|
37
|
-
return if Sentry.configuration.sidekiq.report_only_dead_jobs && context.dig(:job, "dead") == false
|
|
38
|
-
|
|
39
|
-
# Check if the retry count is below the attempt_threshold
|
|
40
|
-
attempt_threshold = context.dig(:job, "attempt_threshold")
|
|
41
|
-
if attempt_threshold && retryable?(context)
|
|
42
|
-
attempt_threshold = attempt_threshold.to_i
|
|
43
|
-
retry_count = context.dig(:job, "retry_count")
|
|
44
|
-
# attempt 1 - retry_count is nil
|
|
45
|
-
# attempt 2 - this is your first retry so retry_count is 0
|
|
46
|
-
# attempt 3 - you have retried once, retry_count is 1
|
|
47
|
-
attempt = retry_count.nil? ? 1 : retry_count.to_i + 2
|
|
48
|
-
# Cap at the final attempt so jobs with fewer retries than the threshold still report.
|
|
49
|
-
effective_threshold = [attempt_threshold, retry_lim + 1].min
|
|
25
|
+
scope.set_transaction_name(ContextFilter.new(context).transaction_name, source: :task) unless scope.transaction_name
|
|
50
26
|
|
|
51
|
-
|
|
52
|
-
|
|
27
|
+
job = context[:job]
|
|
28
|
+
extension = Sentry::Sidekiq.job_extension_for(job, default: @extension)
|
|
29
|
+
return unless extension.report_exception?(job: job, exception: ex, sidekiq_config: sidekiq_config)
|
|
53
30
|
|
|
54
|
-
|
|
55
|
-
ex,
|
|
56
|
-
contexts: { sidekiq: context_filter.filtered },
|
|
57
|
-
hint: { background: false }
|
|
58
|
-
)
|
|
31
|
+
extension.capture_exception(job: job, exception: ex, contexts: { sidekiq: extension.error_context(context) })
|
|
59
32
|
ensure
|
|
60
33
|
scope&.clear
|
|
61
34
|
end
|
|
62
|
-
|
|
63
|
-
private
|
|
64
|
-
|
|
65
|
-
def retryable?(context)
|
|
66
|
-
retry_option = context.dig(:job, "retry")
|
|
67
|
-
# when `retry` is not specified, it's default is `true` and it means 25 retries.
|
|
68
|
-
retry_option.nil? || retry_option == true || (retry_option.is_a?(Integer) && retry_option.positive?)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# @return [Integer] the number of retries allowed for the job
|
|
72
|
-
# Tries to fetch the retry limit from the job config first,
|
|
73
|
-
# then falls back to Sidekiq's configuration.
|
|
74
|
-
def retry_limit(context, sidekiq_config)
|
|
75
|
-
limit = context.dig(:job, "retry")
|
|
76
|
-
|
|
77
|
-
case limit
|
|
78
|
-
when Integer
|
|
79
|
-
limit
|
|
80
|
-
when TrueClass, NilClass
|
|
81
|
-
max_retries =
|
|
82
|
-
if WITH_SIDEKIQ_7
|
|
83
|
-
# Sidekiq 7.1.5+ passes the config to the error handler, so we should use that.
|
|
84
|
-
# Sidekiq 7.0 -> 7.1.5 provides ::Sidekiq.default_configuration.
|
|
85
|
-
sidekiq_config.is_a?(::Sidekiq::Config) ?
|
|
86
|
-
sidekiq_config[:max_retries] :
|
|
87
|
-
::Sidekiq.default_configuration[:max_retries]
|
|
88
|
-
else
|
|
89
|
-
::Sidekiq.options[:max_retries]
|
|
90
|
-
end
|
|
91
|
-
max_retries || 25
|
|
92
|
-
else
|
|
93
|
-
0
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
35
|
end
|
|
97
36
|
end
|
|
98
37
|
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sentry/sidekiq/retry_policy"
|
|
4
|
+
|
|
5
|
+
module Sentry
|
|
6
|
+
module Sidekiq
|
|
7
|
+
class FailureReporting
|
|
8
|
+
def initialize(retry_policy: RetryPolicy.build)
|
|
9
|
+
@retry_policy = retry_policy
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def report?(job, sidekiq_config = nil)
|
|
13
|
+
config = Sentry.configuration.sidekiq
|
|
14
|
+
retry_limit = @retry_policy.limit(job, sidekiq_config)
|
|
15
|
+
|
|
16
|
+
return false if config.report_after_job_retries && retries_pending?(job, retry_limit)
|
|
17
|
+
return false if config.report_only_dead_jobs && job_value(job, "dead") == false
|
|
18
|
+
return false if below_attempt_threshold?(job, retry_limit)
|
|
19
|
+
|
|
20
|
+
true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def retries_pending?(job, retry_limit)
|
|
26
|
+
return false unless @retry_policy.retryable?(job)
|
|
27
|
+
|
|
28
|
+
retry_count = job_value(job, "retry_count")
|
|
29
|
+
retry_count.nil? || retry_count < retry_limit - 1
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def below_attempt_threshold?(job, retry_limit)
|
|
33
|
+
attempt_threshold = job_value(job, "attempt_threshold")
|
|
34
|
+
return false unless attempt_threshold && @retry_policy.retryable?(job)
|
|
35
|
+
|
|
36
|
+
[attempt_threshold.to_i, retry_limit + 1].min > attempt(job)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def attempt(job)
|
|
40
|
+
retry_count = job_value(job, "retry_count")
|
|
41
|
+
retry_count.nil? ? 1 : retry_count.to_i + 2
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def job_value(job, key)
|
|
45
|
+
job && job[key]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sentry/sidekiq/retry_policy"
|
|
4
|
+
require "sentry/sidekiq/failure_reporting"
|
|
5
|
+
require "sentry/sidekiq/context_filter"
|
|
6
|
+
|
|
7
|
+
module Sentry
|
|
8
|
+
module Sidekiq
|
|
9
|
+
# The job extension sentry-sidekiq uses for Sidekiq jobs, given as job Hashes.
|
|
10
|
+
# Its retry and failure reporting follow sentry-sidekiq's configuration.
|
|
11
|
+
#
|
|
12
|
+
# @see ActiveJobSupport for how ActiveJob jobs on Sidekiq are reported
|
|
13
|
+
class JobExtension < Sentry::JobExtensions::Extension
|
|
14
|
+
SPAN_ORIGIN = "auto.queue.sidekiq"
|
|
15
|
+
SIDEKIQ_NAME = "Sidekiq"
|
|
16
|
+
MESSAGING_SYSTEM = "sidekiq"
|
|
17
|
+
ENQUEUED_AT_IN_MILLISECONDS = ::Gem::Version.new(::Sidekiq::VERSION) >= ::Gem::Version.new("8.0.0")
|
|
18
|
+
ARGUMENT_KEYS = %w[args jobstr].freeze
|
|
19
|
+
|
|
20
|
+
# @param retry_policy [RetryPolicy]
|
|
21
|
+
# @param failure_reporting [FailureReporting]
|
|
22
|
+
# @param serializer [#dump_json] encodes jobs to measure their body size
|
|
23
|
+
# @see Sentry::JobExtensions::Extension#initialize
|
|
24
|
+
def initialize(
|
|
25
|
+
name: :sidekiq,
|
|
26
|
+
integration: Sentry::Sidekiq,
|
|
27
|
+
origin: SPAN_ORIGIN,
|
|
28
|
+
retry_policy: RetryPolicy.build,
|
|
29
|
+
failure_reporting: FailureReporting.new(retry_policy: retry_policy),
|
|
30
|
+
serializer: ::Sidekiq
|
|
31
|
+
)
|
|
32
|
+
super(name: name, integration: integration, origin: origin)
|
|
33
|
+
@retry_policy = retry_policy
|
|
34
|
+
@failure_reporting = failure_reporting
|
|
35
|
+
@serializer = serializer
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Returns the Sidekiq error context in the shape attached to error events.
|
|
39
|
+
#
|
|
40
|
+
# @param context [Hash]
|
|
41
|
+
# @return [Hash]
|
|
42
|
+
def error_context(context)
|
|
43
|
+
without_arguments(ContextFilter.new(context).filtered)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Adds the job as the `sidekiq` context, and its queue, jid, tags and user.
|
|
47
|
+
#
|
|
48
|
+
# @see Sentry::JobExtensions::Extension#scope_data
|
|
49
|
+
def scope_data(job:, **)
|
|
50
|
+
data = {
|
|
51
|
+
contexts: { sidekiq: without_arguments(job) },
|
|
52
|
+
tags: { queue: job["queue"], jid: job["jid"] }.merge(job_tags(job))
|
|
53
|
+
}
|
|
54
|
+
user = job["sentry_user"]
|
|
55
|
+
data[:user] = user if user.is_a?(Hash)
|
|
56
|
+
data
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Describes the Sidekiq message being processed.
|
|
60
|
+
#
|
|
61
|
+
# @see Sentry::JobExtensions::Extension#messaging_data
|
|
62
|
+
def messaging_data(job:, **)
|
|
63
|
+
data = {
|
|
64
|
+
system: MESSAGING_SYSTEM,
|
|
65
|
+
message_id: job["jid"],
|
|
66
|
+
destination: job["queue"],
|
|
67
|
+
receive_latency: receive_latency(job),
|
|
68
|
+
retry_count: @retry_policy.retry_count(job) || 0
|
|
69
|
+
}
|
|
70
|
+
data[:body_size] = body_size(job) if Sentry.get_current_scope.get_span&.sampled
|
|
71
|
+
data.compact
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Describes the Sidekiq message being enqueued.
|
|
75
|
+
#
|
|
76
|
+
# @see Sentry::JobExtensions::Extension#publish_messaging_data
|
|
77
|
+
def publish_messaging_data(job:, **)
|
|
78
|
+
{ system: MESSAGING_SYSTEM, message_id: job["jid"], destination: job["queue"] }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Follows the job's and Sidekiq's retry settings.
|
|
82
|
+
#
|
|
83
|
+
# @see Sentry::JobExtensions::Extension#will_retry?
|
|
84
|
+
def will_retry?(job:, **)
|
|
85
|
+
@retry_policy.will_retry?(job)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Follows `config.sidekiq.report_after_job_retries`, `report_only_dead_jobs` and
|
|
89
|
+
# the job's `attempt_threshold`.
|
|
90
|
+
#
|
|
91
|
+
# @see Sentry::JobExtensions::Extension#report_exception?
|
|
92
|
+
def report_exception?(job:, exception:, sidekiq_config: nil, **)
|
|
93
|
+
@failure_reporting.report?(job, sidekiq_config)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Stores the headers in the job's `trace_propagation_headers`, unless it already has them.
|
|
97
|
+
#
|
|
98
|
+
# @see Sentry::JobExtensions::Extension#inject_trace_headers
|
|
99
|
+
def inject_trace_headers(job:, payload:, headers:, **)
|
|
100
|
+
return payload if payload["trace_propagation_headers"]
|
|
101
|
+
|
|
102
|
+
payload.merge("trace_propagation_headers" => headers)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Reads the job's `trace_propagation_headers`.
|
|
106
|
+
#
|
|
107
|
+
# @see Sentry::JobExtensions::Extension#extract_trace_headers
|
|
108
|
+
def extract_trace_headers(job:, **)
|
|
109
|
+
job["trace_propagation_headers"] || {}
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Names the transaction `Sidekiq/<job class>`.
|
|
113
|
+
#
|
|
114
|
+
# @see Sentry::JobExtensions::Extension#transaction_name
|
|
115
|
+
def transaction_name(job:, **)
|
|
116
|
+
name = job_class(job)
|
|
117
|
+
name ? "#{SIDEKIQ_NAME}/#{name}" : SIDEKIQ_NAME
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Describes the `queue.publish` span with the job class.
|
|
121
|
+
#
|
|
122
|
+
# @see Sentry::JobExtensions::Extension#publish_description
|
|
123
|
+
def publish_description(job:, **)
|
|
124
|
+
job["class"].to_s
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Keeps the `queue.publish` span's default origin.
|
|
128
|
+
#
|
|
129
|
+
# @see Sentry::JobExtensions::Extension#publish_span_origin
|
|
130
|
+
def publish_span_origin(job:, **)
|
|
131
|
+
Span::DEFAULT_SPAN_ORIGIN
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
private
|
|
135
|
+
|
|
136
|
+
def without_arguments(context)
|
|
137
|
+
return context if Sentry.configuration.data_collection.queues
|
|
138
|
+
|
|
139
|
+
context.reject { |key, _value| ARGUMENT_KEYS.include?(key.to_s) }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def job_class(job)
|
|
143
|
+
job["class"] if job.is_a?(Hash)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def job_tags(job)
|
|
147
|
+
Array(job["tags"]).each_with_object({}) { |name, tags_hash| tags_hash[:"sidekiq.#{name}"] = true }
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def body_size(job)
|
|
151
|
+
@serializer.dump_json(job).bytesize
|
|
152
|
+
rescue StandardError
|
|
153
|
+
nil
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def enqueued_at(job)
|
|
157
|
+
enqueued_at = job["enqueued_at"]
|
|
158
|
+
return unless enqueued_at
|
|
159
|
+
|
|
160
|
+
ENQUEUED_AT_IN_MILLISECONDS ? Time.at(Rational(enqueued_at, 1000)) : Time.at(enqueued_at)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def receive_latency(job)
|
|
164
|
+
Sentry::Utils::Latency.milliseconds_since(enqueued_at(job))&.round
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sentry/sidekiq/active_job_support/server_middleware"
|
|
4
|
+
|
|
5
|
+
module Sentry
|
|
6
|
+
module Sidekiq
|
|
7
|
+
# Sets up how ActiveJob jobs on Sidekiq are reported in Rails apps.
|
|
8
|
+
#
|
|
9
|
+
# @see ActiveJobSupport
|
|
10
|
+
class Railtie < ::Rails::Railtie
|
|
11
|
+
initializer "sentry.sidekiq.skippable_job_adapter", before: :load_environment_config do
|
|
12
|
+
next unless defined?(::Sentry::Rails)
|
|
13
|
+
|
|
14
|
+
Sentry::Configuration.after(:initialize) { rails.skippable_job_adapters << ActiveJobSupport::ACTIVE_JOB_ADAPTER }
|
|
15
|
+
# The callback only covers configurations created from here on, so an app that
|
|
16
|
+
# initialized Sentry earlier gets the adapter added to its current configuration.
|
|
17
|
+
Sentry.configuration.rails.skippable_job_adapters << ActiveJobSupport::ACTIVE_JOB_ADAPTER if Sentry.initialized?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
config.after_initialize do
|
|
21
|
+
next unless defined?(::Sentry::Rails) && Sentry::Rails.active_job_owns_adapter?(ActiveJobSupport::ACTIVE_JOB_ADAPTER)
|
|
22
|
+
|
|
23
|
+
require "sentry/sidekiq/active_job_extension"
|
|
24
|
+
SentryContextServerMiddleware.prepend(ActiveJobSupport::ServerMiddleware)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sentry
|
|
4
|
+
module Sidekiq
|
|
5
|
+
class RetryPolicy
|
|
6
|
+
DEFAULT_MAX_RETRIES = 25
|
|
7
|
+
|
|
8
|
+
def self.build
|
|
9
|
+
if ::Gem::Version.new(::Sidekiq::VERSION) >= ::Gem::Version.new("7.0")
|
|
10
|
+
Sidekiq7.new
|
|
11
|
+
elsif ::Sidekiq.respond_to?(:[])
|
|
12
|
+
Sidekiq6.new
|
|
13
|
+
else
|
|
14
|
+
Legacy.new
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
attr_reader :default_config
|
|
19
|
+
|
|
20
|
+
def initialize(default_config)
|
|
21
|
+
@default_config = default_config
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def retryable?(job)
|
|
25
|
+
retry_option = retry_option(job)
|
|
26
|
+
# when `retry` is not specified, it's default is `true` and it means 25 retries.
|
|
27
|
+
retry_option.nil? || retry_option == true || (retry_option.is_a?(Integer) && retry_option.positive?)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @return [Integer] the number of retries allowed for the job
|
|
31
|
+
# Tries to fetch the retry limit from the job config first,
|
|
32
|
+
# then falls back to Sidekiq's configuration.
|
|
33
|
+
def limit(job, sidekiq_config = nil)
|
|
34
|
+
retry_option = retry_option(job)
|
|
35
|
+
|
|
36
|
+
case retry_option
|
|
37
|
+
when Integer
|
|
38
|
+
retry_option
|
|
39
|
+
when TrueClass, NilClass
|
|
40
|
+
max_retries(sidekiq_config) || DEFAULT_MAX_RETRIES
|
|
41
|
+
else
|
|
42
|
+
0
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def retry_count(job)
|
|
47
|
+
job && job["retry_count"]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def will_retry?(job)
|
|
51
|
+
count = retry_count(job)
|
|
52
|
+
next_count = count.nil? ? 0 : count + 1
|
|
53
|
+
retryable?(job) && next_count < limit(job)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def retry_option(job)
|
|
59
|
+
job && job["retry"]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def max_retries(_sidekiq_config)
|
|
63
|
+
default_config[:max_retries]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
class Sidekiq7 < RetryPolicy
|
|
67
|
+
def initialize(default_config = ::Sidekiq.default_configuration)
|
|
68
|
+
super
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
# Sidekiq 7.1.5+ passes the config to the error handler, so we should use that.
|
|
74
|
+
# Sidekiq 7.0 -> 7.1.5 provides ::Sidekiq.default_configuration.
|
|
75
|
+
def max_retries(sidekiq_config)
|
|
76
|
+
config = sidekiq_config.is_a?(::Sidekiq::Config) ? sidekiq_config : default_config
|
|
77
|
+
config[:max_retries]
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
class Sidekiq6 < RetryPolicy
|
|
82
|
+
def initialize(default_config = ::Sidekiq)
|
|
83
|
+
super
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
class Legacy < RetryPolicy
|
|
88
|
+
def initialize(default_config = ::Sidekiq)
|
|
89
|
+
super
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
private
|
|
93
|
+
|
|
94
|
+
def max_retries(_sidekiq_config)
|
|
95
|
+
default_config.options[:max_retries]
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -1,109 +1,60 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "sentry/sidekiq/
|
|
3
|
+
require "sentry/sidekiq/job_extension"
|
|
4
4
|
|
|
5
5
|
module Sentry
|
|
6
6
|
module Sidekiq
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
span.set_data(Span::DataConventions::MESSAGING_MESSAGE_ID, id)
|
|
12
|
-
span.set_data(Span::DataConventions::MESSAGING_DESTINATION_NAME, queue)
|
|
13
|
-
span.set_data(Span::DataConventions::MESSAGING_MESSAGE_RECEIVE_LATENCY, latency) if latency
|
|
14
|
-
span.set_data(Span::DataConventions::MESSAGING_MESSAGE_RETRY_COUNT, retry_count) if retry_count
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
if ::Gem::Version.new(::Sidekiq::VERSION) >= ::Gem::Version.new("8.0.0")
|
|
18
|
-
def calculate_latency(job)
|
|
19
|
-
now_in_ms - job["enqueued_at"] if job["enqueued_at"]
|
|
20
|
-
end
|
|
21
|
-
else
|
|
22
|
-
def calculate_latency(job)
|
|
23
|
-
((Time.now.to_f - job["enqueued_at"]) * 1000).round if job["enqueued_at"]
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def now_in_ms
|
|
28
|
-
::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond)
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
7
|
+
# Sidekiq server middleware that runs each job in its own scope and
|
|
8
|
+
# `queue.process` transaction, using the extension for the job.
|
|
9
|
+
#
|
|
10
|
+
# @see Sentry::Sidekiq.job_extension_for
|
|
32
11
|
class SentryContextServerMiddleware
|
|
33
|
-
|
|
12
|
+
OP_NAME = Sentry::JobExtensions::Extension::PROCESS_OP
|
|
13
|
+
SPAN_ORIGIN = JobExtension::SPAN_ORIGIN
|
|
34
14
|
|
|
35
|
-
|
|
36
|
-
|
|
15
|
+
# @param extension [JobExtension] the extension for plain Sidekiq jobs
|
|
16
|
+
def initialize(extension = Sentry::Sidekiq.job_extension)
|
|
17
|
+
@extension = extension
|
|
18
|
+
end
|
|
37
19
|
|
|
38
20
|
def call(worker, job, queue)
|
|
39
21
|
return yield unless Sentry.initialized?
|
|
40
22
|
|
|
41
|
-
|
|
23
|
+
sidekiq_job = job.merge("queue" => queue)
|
|
42
24
|
|
|
43
25
|
Sentry.clone_hub_to_current_thread
|
|
44
26
|
scope = Sentry.get_current_scope
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
scope.set_tags(queue: queue, jid: job["jid"])
|
|
49
|
-
scope.set_tags(build_tags(job["tags"]))
|
|
50
|
-
scope.set_contexts(sidekiq: job.merge("queue" => queue))
|
|
51
|
-
scope.set_transaction_name(context_filter.transaction_name, source: :task)
|
|
52
|
-
transaction = start_transaction(scope, job["trace_propagation_headers"])
|
|
53
|
-
|
|
54
|
-
if transaction
|
|
55
|
-
scope.set_span(transaction)
|
|
56
|
-
|
|
57
|
-
latency = calculate_latency(job)
|
|
58
|
-
|
|
59
|
-
set_span_data(
|
|
60
|
-
transaction,
|
|
61
|
-
id: job["jid"],
|
|
62
|
-
queue: queue,
|
|
63
|
-
latency: latency,
|
|
64
|
-
retry_count: job["retry_count"] || 0
|
|
65
|
-
)
|
|
66
|
-
end
|
|
27
|
+
extension = Sentry::Sidekiq.job_extension_for(sidekiq_job, default: @extension)
|
|
28
|
+
scope.generate_propagation_context unless continues_trace?(extension, sidekiq_job)
|
|
29
|
+
transaction = extension.start_job(scope: scope, job: sidekiq_job)
|
|
67
30
|
|
|
68
31
|
begin
|
|
69
32
|
yield
|
|
70
33
|
rescue
|
|
71
|
-
|
|
34
|
+
extension.finish_job(transaction: transaction, status: 500)
|
|
72
35
|
raise
|
|
73
36
|
end
|
|
74
37
|
|
|
75
|
-
|
|
38
|
+
extension.finish_job(transaction: transaction, status: 200)
|
|
76
39
|
# don't need to use ensure here
|
|
77
40
|
# if the job failed, we need to keep the scope for error handler. and the scope will be cleared there
|
|
78
41
|
scope.clear
|
|
79
42
|
end
|
|
80
43
|
|
|
81
|
-
|
|
82
|
-
Array(tags).each_with_object({}) { |name, tags_hash| tags_hash[:"sidekiq.#{name}"] = true }
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def start_transaction(scope, env)
|
|
86
|
-
options = {
|
|
87
|
-
name: scope.transaction_name,
|
|
88
|
-
source: scope.transaction_source,
|
|
89
|
-
op: OP_NAME,
|
|
90
|
-
origin: SPAN_ORIGIN
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
transaction = Sentry.continue_trace(env, **options)
|
|
94
|
-
Sentry.start_transaction(transaction: transaction, **options)
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
def finish_transaction(transaction, status)
|
|
98
|
-
return unless transaction
|
|
44
|
+
private
|
|
99
45
|
|
|
100
|
-
|
|
101
|
-
|
|
46
|
+
def continues_trace?(extension, job)
|
|
47
|
+
extension.safely("extract trace headers", fallback: false) { !Hash(extension.extract_trace_headers(job: job)).empty? }
|
|
102
48
|
end
|
|
103
49
|
end
|
|
104
50
|
|
|
51
|
+
# Sidekiq client middleware that passes the user and trace to enqueued jobs
|
|
52
|
+
# and records their `queue.publish` spans.
|
|
105
53
|
class SentryContextClientMiddleware
|
|
106
|
-
|
|
54
|
+
# @param extension [JobExtension]
|
|
55
|
+
def initialize(extension = Sentry::Sidekiq.job_extension)
|
|
56
|
+
@extension = extension
|
|
57
|
+
end
|
|
107
58
|
|
|
108
59
|
def call(worker_class, job, queue, _redis_pool)
|
|
109
60
|
return yield unless Sentry.initialized?
|
|
@@ -114,14 +65,12 @@ module Sentry
|
|
|
114
65
|
end
|
|
115
66
|
|
|
116
67
|
if Sentry.configuration.sidekiq.propagate_traces
|
|
117
|
-
|
|
68
|
+
headers = Sentry.get_trace_propagation_headers
|
|
69
|
+
injected = @extension.safely("inject trace headers", fallback: job) { @extension.inject_trace_headers(job: job, payload: job, headers: headers) }
|
|
70
|
+
job.replace(injected) if injected.is_a?(Hash)
|
|
118
71
|
end
|
|
119
72
|
|
|
120
|
-
|
|
121
|
-
set_span_data(span, id: job["jid"], queue: queue)
|
|
122
|
-
|
|
123
|
-
yield
|
|
124
|
-
end
|
|
73
|
+
@extension.instrument_enqueue(job: job.merge("queue" => queue)) { yield }
|
|
125
74
|
end
|
|
126
75
|
end
|
|
127
76
|
end
|
data/lib/sentry-sidekiq.rb
CHANGED
|
@@ -5,6 +5,8 @@ require "sentry-ruby"
|
|
|
5
5
|
require "sentry/integrable"
|
|
6
6
|
require "sentry/sidekiq/version"
|
|
7
7
|
require "sentry/sidekiq/configuration"
|
|
8
|
+
require "sentry/sidekiq/job_extension"
|
|
9
|
+
require "sentry/sidekiq/active_job_support/job_extension"
|
|
8
10
|
require "sentry/sidekiq/error_handler"
|
|
9
11
|
require "sentry/sidekiq/sentry_context_middleware"
|
|
10
12
|
|
|
@@ -14,18 +16,35 @@ module Sentry
|
|
|
14
16
|
|
|
15
17
|
register_integration name: "sidekiq", version: Sentry::Sidekiq::VERSION
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
# Returns the extension for plain Sidekiq jobs.
|
|
20
|
+
#
|
|
21
|
+
# @return [JobExtension]
|
|
22
|
+
def self.job_extension
|
|
23
|
+
@job_extension ||= JobExtension.new
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Returns the extension for ActiveJob jobs that sentry-sidekiq reports itself.
|
|
27
|
+
#
|
|
28
|
+
# @return [ActiveJobSupport::JobExtension]
|
|
29
|
+
# @see ActiveJobSupport
|
|
30
|
+
def self.active_job_extension
|
|
31
|
+
@active_job_extension ||= ActiveJobSupport::JobExtension.new
|
|
32
|
+
end
|
|
21
33
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
# Returns the extension for the given Sidekiq job: {active_job_extension} for
|
|
35
|
+
# ActiveJob jobs, `default` otherwise.
|
|
36
|
+
#
|
|
37
|
+
# @param job [Hash] the Sidekiq job
|
|
38
|
+
# @param default [JobExtension]
|
|
39
|
+
# @return [JobExtension]
|
|
40
|
+
def self.job_extension_for(job, default: job_extension)
|
|
41
|
+
ActiveJobSupport.handles?(job) ? active_job_extension : default
|
|
25
42
|
end
|
|
26
43
|
end
|
|
27
44
|
end
|
|
28
45
|
|
|
46
|
+
require "sentry/sidekiq/railtie" if defined?(::Rails::Railtie)
|
|
47
|
+
|
|
29
48
|
Sidekiq.configure_server do |config|
|
|
30
49
|
config.error_handlers << Sentry::Sidekiq::ErrorHandler.new
|
|
31
50
|
config.server_middleware do |chain|
|
data/sentry-sidekiq.gemspec
CHANGED
|
@@ -30,6 +30,6 @@ Gem::Specification.new do |spec|
|
|
|
30
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
31
31
|
spec.require_paths = ["lib"]
|
|
32
32
|
|
|
33
|
-
spec.add_dependency "sentry-ruby", "~> 7.
|
|
33
|
+
spec.add_dependency "sentry-ruby", "~> 7.3.0"
|
|
34
34
|
spec.add_dependency "sidekiq", ">= 5.0"
|
|
35
35
|
end
|