Skip to content

Commit 7265f14

Browse files
authored
Sidekiq 8.0 updates (#2570)
* Update CHANGELOG * [sidekiq] display Sidekiq version before suite * [sidekiq] use sidekiq-scheduler 6.0.0.beta * [sidekiq] run tests against released 8.0.0 * [sidekiq] fix handling of latency under 8.0 * [sidekiq] remove redundant spec
1 parent 5e8db8d commit 7265f14

File tree

7 files changed

+118
-45
lines changed

7 files changed

+118
-45
lines changed

.github/workflows/sentry_sidekiq_test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ jobs:
5252
sidekiq_version: 7.0
5353
- ruby_version: "3.2"
5454
sidekiq_version: 7.0
55-
options:
56-
rubyopt: "--enable-frozen-string-literal --debug=frozen-string-literal"
5755
- ruby_version: "3.2"
58-
sidekiq_version: 8.0.0.beta1
56+
sidekiq_version: 8.0.0
5957
- ruby_version: "3.3"
60-
sidekiq_version: 8.0.0.beta1
58+
sidekiq_version: 8.0.0
6159
- ruby_version: "3.4"
62-
sidekiq_version: 8.0.0.beta1
60+
sidekiq_version: 8.0.0
61+
options:
62+
rubyopt: "--enable-frozen-string-literal --debug=frozen-string-literal"
6363
exclude:
6464
- ruby_version: head
6565
- ruby_version: jruby

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Fix breadcrumb serialization error message to be an object ([#2584](https://github.com/getsentry/sentry-ruby/pull/2584))
2222
- Fixes [#2478](https://github.com/getsentry/sentry-ruby/issues/2478)
2323
- Fix compatibility issues with sidekiq-cron 2.2.0 ([#2591](https://github.com/getsentry/sentry-ruby/pull/2591))
24+
- Update sentry-sidekiq to work correctly with Sidekiq 8.0 and its new timestamp format ([#2570](https://github.com/getsentry/sentry-ruby/pull/2570))
2425

2526
### Internal
2627

sentry-sidekiq/Gemfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ end
2828

2929
if sidekiq_version == "main" || RUBY_VERSION.to_f >= 2.7 && sidekiq_version >= Gem::Version.new("6.0")
3030
gem "sidekiq-cron"
31-
gem "sidekiq-scheduler"
31+
32+
if sidekiq_version == "main" || sidekiq_version >= Gem::Version.new("8.0")
33+
gem "sidekiq-scheduler", "~> 6.0.0.beta"
34+
else
35+
gem "sidekiq-scheduler", "~> 5.0.0"
36+
end
3237
end
3338

3439
gem "rails", "> 5.0.0"

sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ def set_span_data(span, id:, queue:, latency: nil, retry_count: nil)
1313
span.set_data(Span::DataConventions::MESSAGING_MESSAGE_RECEIVE_LATENCY, latency) if latency
1414
span.set_data(Span::DataConventions::MESSAGING_MESSAGE_RETRY_COUNT, retry_count) if retry_count
1515
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
1630
end
1731

1832
class SentryContextServerMiddleware
@@ -40,7 +54,8 @@ def call(worker, job, queue)
4054
if transaction
4155
scope.set_span(transaction)
4256

43-
latency = ((Time.now.to_f - job["enqueued_at"]) * 1000).to_i if job["enqueued_at"]
57+
latency = calculate_latency(job)
58+
4459
set_span_data(
4560
transaction,
4661
id: job["jid"],

sentry-sidekiq/spec/sentry/sidekiq/error_handler_spec.rb

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,38 @@
1111
Sentry.get_current_client.transport
1212
end
1313

14-
let(:context) do
15-
{
16-
"args" => [true, true],
17-
"class" => "HardWorker",
18-
"created_at" => 1_474_922_824.910579,
19-
"enqueued_at" => 1_474_922_824.910665,
20-
"error_class" => "RuntimeError",
21-
"error_message" => "a wild exception appeared",
22-
"failed_at" => 1_474_922_825.158953,
23-
"jid" => "701ed9cfa51c84a763d56bc4",
24-
"queue" => "default",
25-
"retry" => true,
26-
"retry_count" => 0
27-
}
14+
if WITH_SIDEKIQ_8
15+
let(:context) do
16+
{
17+
"args" => [true, true],
18+
"class" => "HardWorker",
19+
"created_at" => 1_474_922_824_910,
20+
"enqueued_at" => 1_474_922_824_910,
21+
"error_class" => "RuntimeError",
22+
"error_message" => "a wild exception appeared",
23+
"failed_at" => 1_474_922_825_158,
24+
"jid" => "701ed9cfa51c84a763d56bc4",
25+
"queue" => "default",
26+
"retry" => true,
27+
"retry_count" => 0
28+
}
29+
end
30+
else
31+
let(:context) do
32+
{
33+
"args" => [true, true],
34+
"class" => "HardWorker",
35+
"created_at" => 1_474_922_824.910579,
36+
"enqueued_at" => 1_474_922_824.910665,
37+
"error_class" => "RuntimeError",
38+
"error_message" => "a wild exception appeared",
39+
"failed_at" => 1_474_922_825.158953,
40+
"jid" => "701ed9cfa51c84a763d56bc4",
41+
"queue" => "default",
42+
"retry" => true,
43+
"retry_count" => 0
44+
}
45+
end
2846
end
2947

3048
let(:processor) do

sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,36 +68,29 @@
6868
end
6969

7070
context "span data for Queues module" do
71-
it "adds a queue.process transaction with correct data" do
72-
Timecop.freeze do
73-
execute_worker(processor, HappyWorker)
74-
end
75-
76-
expect(transport.events.count).to eq(1)
71+
def timecop_delay
72+
Time.now + expected_latency / 1000
73+
end
7774

78-
transaction = transport.events[0]
79-
expect(transaction).not_to be_nil
80-
expect(transaction.spans.count).to eq(0)
81-
expect(transaction.contexts[:trace][:data]['messaging.message.id']).to eq('123123') # Default defined in #execute_worker
82-
expect(transaction.contexts[:trace][:data]['messaging.destination.name']).to eq('default')
83-
expect(transaction.contexts[:trace][:data]['messaging.message.receive.latency']).to eq(0)
84-
expect(transaction.contexts[:trace][:data]['messaging.message.retry.count']).to eq(0)
75+
let(:expected_latency) do
76+
86400000
8577
end
8678

87-
it "adds a queue.process transaction with correct latency data" do
88-
Timecop.freeze do
89-
execute_worker(processor, HappyWorker, jid: '123456', timecop_delay: Time.now + 86400)
90-
end
79+
it "adds a queue.process transaction with correct data" do
80+
execute_worker(processor, HappyWorker, jid: '123456', timecop_delay: timecop_delay)
9181

9282
expect(transport.events.count).to eq(1)
9383

9484
transaction = transport.events[0]
85+
trace = transaction.contexts[:trace]
86+
9587
expect(transaction).not_to be_nil
9688
expect(transaction.spans.count).to eq(0)
97-
expect(transaction.contexts[:trace][:data]['messaging.message.id']).to eq('123456') # Explicitly set above.
98-
expect(transaction.contexts[:trace][:data]['messaging.destination.name']).to eq('default')
99-
expect(transaction.contexts[:trace][:data]['messaging.message.receive.latency']).to eq(86400000)
100-
expect(transaction.contexts[:trace][:data]['messaging.message.retry.count']).to eq(0)
89+
90+
expect(trace[:data]['messaging.message.id']).to eq('123456')
91+
expect(trace[:data]['messaging.destination.name']).to eq('default')
92+
expect(trace[:data]['messaging.message.retry.count']).to eq(0)
93+
expect(trace[:data]['messaging.message.receive.latency']).to eq(expected_latency)
10194
end
10295

10396
if MIN_SIDEKIQ_6

sentry-sidekiq/spec/spec_helper.rb

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
MIN_SIDEKIQ_6 = Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("6.0")
1313
WITH_SIDEKIQ_7 = Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("7.0")
14+
WITH_SIDEKIQ_8 = Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("8.0.0")
1415
WITH_SIDEKIQ_6 = MIN_SIDEKIQ_6 && !WITH_SIDEKIQ_7
1516

1617
require "sidekiq/embedded" if WITH_SIDEKIQ_7
@@ -56,6 +57,14 @@
5657
c.syntax = :expect
5758
end
5859

60+
config.before :suite do
61+
puts "\n"
62+
puts "*" * 100
63+
puts "Running with Sidekiq #{Sidekiq::VERSION}"
64+
puts "*" * 100
65+
puts "\n"
66+
end
67+
5968
config.before :each do
6069
# Make sure we reset the env in case something leaks in
6170
ENV.delete('SENTRY_DSN')
@@ -273,6 +282,10 @@ def sidekiq_config(opts)
273282
WITH_SIDEKIQ_7 ? ::Sidekiq::Config.new(opts) : SidekiqConfigMock.new(opts)
274283
end
275284

285+
def now_in_ms
286+
::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond)
287+
end
288+
276289
def execute_worker(processor, klass, **options)
277290
klass_options = klass.sidekiq_options_hash || {}
278291
# for Ruby < 2.6
@@ -283,12 +296,40 @@ def execute_worker(processor, klass, **options)
283296
jid = options.delete(:jid) || "123123"
284297
timecop_delay = options.delete(:timecop_delay)
285298

286-
msg = Sidekiq.dump_json(created_at: Time.now.to_f, enqueued_at: Time.now.to_f, jid: jid, class: klass, args: [], **options)
287-
Timecop.freeze(timecop_delay) if timecop_delay
299+
timestamps = if WITH_SIDEKIQ_8
300+
current_time_ms = now_in_ms
301+
{
302+
created_at: current_time_ms,
303+
enqueued_at: current_time_ms
304+
}
305+
else
306+
{
307+
created_at: Time.now.to_f,
308+
enqueued_at: Time.now.to_f
309+
}
310+
end
311+
312+
msg = Sidekiq.dump_json(
313+
jid: jid,
314+
class: klass,
315+
args: [],
316+
**timestamps,
317+
**options
318+
)
319+
320+
if timecop_delay
321+
Timecop.mock_process_clock = true
322+
Timecop.freeze(timecop_delay)
323+
end
324+
288325
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', msg)
326+
289327
process_work(processor, work)
290328
ensure
291-
Timecop.return if timecop_delay
329+
if timecop_delay
330+
Timecop.return
331+
Timecop.mock_process_clock = false
332+
end
292333
end
293334

294335
def process_work(processor, work)

0 commit comments

Comments
 (0)