forked from yabeda-rb/yabeda-sidekiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidekiq_spec.rb
307 lines (277 loc) · 10.9 KB
/
sidekiq_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# frozen_string_literal: true
RSpec.describe Yabeda::Sidekiq do
it "has a version number" do
expect(Yabeda::Sidekiq::VERSION).not_to be nil
end
it "configures middlewares" do
config = Sidekiq.respond_to?(:default_configuration) ? Sidekiq.default_configuration : Sidekiq
expect(config.client_middleware).to include(have_attributes(klass: Yabeda::Sidekiq::ClientMiddleware))
end
describe "plain Sidekiq jobs" do
it "counts enqueues" do
expect do
SamplePlainJob.perform_async
SamplePlainJob.perform_async
FailingPlainJob.perform_async
end.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_enqueued_total).with(
{ queue: "default", worker: "SamplePlainJob" } => 2,
{ queue: "default", worker: "FailingPlainJob" } => 1,
)
end
context "when label_for_error_class_on_sidekiq_jobs_failed is set to true" do
around do |example|
old_value = described_class.config.label_for_error_class_on_sidekiq_jobs_failed
described_class.config.label_for_error_class_on_sidekiq_jobs_failed = true
example.run
described_class.config.label_for_error_class_on_sidekiq_jobs_failed = old_value
end
it "counts failed total and executed total with correct labels", sidekiq: :inline do
expect do
SamplePlainJob.perform_async
SamplePlainJob.perform_async
begin
FailingPlainJob.perform_async
rescue StandardError
nil
end
end.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_failed_total).with(
{ queue: "default", worker: "FailingPlainJob", error: "FailingPlainJob::SpecialError" } => 1,
).and \
increment_yabeda_counter(Yabeda.sidekiq.jobs_executed_total).with(
{ queue: "default", worker: "SamplePlainJob" } => 2,
{ queue: "default", worker: "FailingPlainJob" } => 1,
)
end
it "does not add jobs_failed_total error label to labels used for jobs_executed_total", sidekiq: :inline do
expect do
SamplePlainJob.perform_async
SamplePlainJob.perform_async
begin
FailingPlainJob.perform_async
rescue StandardError
nil
end
end.not_to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_executed_total).with(
{ queue: "default", worker: "FailingPlainJob", error: "FailingPlainJob::SpecialError" } => 1,
)
end
end
describe "re-routing jobs by middleware" do
around do |example|
add_reroute_jobs_middleware
example.run
remove_reroute_jobs_middleware
end
it "counts enqueues" do
expect do
SamplePlainJob.perform_async
SamplePlainJob.perform_async
FailingPlainJob.perform_async
end.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_enqueued_total).with(
{ queue: "rerouted_queue", worker: "SamplePlainJob" } => 2,
{ queue: "rerouted_queue", worker: "FailingPlainJob" } => 1,
).and \
increment_yabeda_counter(Yabeda.sidekiq.jobs_rerouted_total).with(
{ from_queue: "default", to_queue: "rerouted_queue", worker: "SamplePlainJob" } => 2,
{ from_queue: "default", to_queue: "rerouted_queue", worker: "FailingPlainJob" } => 1,
)
end
end
it "measures runtime", sidekiq: :inline do
expect do
SamplePlainJob.perform_async
SamplePlainJob.perform_async
begin
FailingPlainJob.perform_async
rescue StandardError
nil
end
end.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_executed_total).with(
{ queue: "default", worker: "SamplePlainJob" } => 2,
{ queue: "default", worker: "FailingPlainJob" } => 1,
).and \
increment_yabeda_counter(Yabeda.sidekiq.jobs_success_total).with(
{ queue: "default", worker: "SamplePlainJob" } => 2,
).and \
increment_yabeda_counter(Yabeda.sidekiq.jobs_failed_total).with(
{ queue: "default", worker: "FailingPlainJob" } => 1,
).and \
measure_yabeda_histogram(Yabeda.sidekiq.job_runtime).with(
{ queue: "default", worker: "SamplePlainJob" } => kind_of(Numeric),
{ queue: "default", worker: "FailingPlainJob" } => kind_of(Numeric),
)
end
end
describe "ActiveJob jobs" do
it "counts enqueues" do
expect { SampleActiveJob.perform_later }.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_enqueued_total).with(
{ queue: "default", worker: "SampleActiveJob" } => 1,
)
end
describe "re-routing jobs by middleware" do
around do |example|
add_reroute_jobs_middleware
example.run
remove_reroute_jobs_middleware
end
it "counts enqueues" do
expect { SampleActiveJob.perform_later }.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_enqueued_total).with(
{ queue: "rerouted_queue", worker: "SampleActiveJob" } => 1,
).and \
increment_yabeda_counter(Yabeda.sidekiq.jobs_rerouted_total).with(
{ from_queue: "default", to_queue: "rerouted_queue", worker: "SampleActiveJob" } => 1,
)
end
end
context "when label_for_error_class_on_sidekiq_jobs_failed is set to true" do
around do |example|
old_value = described_class.config.label_for_error_class_on_sidekiq_jobs_failed
described_class.config.label_for_error_class_on_sidekiq_jobs_failed = true
example.run
described_class.config.label_for_error_class_on_sidekiq_jobs_failed = old_value
end
it "counts enqueues and uses the default label for the error class", sidekiq: :inline do
expect do
SampleActiveJob.perform_later
SampleActiveJob.perform_later
begin
FailingActiveJob.perform_later
rescue StandardError
nil
end
end.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_failed_total).with(
{ queue: "default", worker: "FailingActiveJob", error: "FailingActiveJob::SpecialError" } => 1,
)
end
end
it "measures runtime", sidekiq: :inline do
expect do
SampleActiveJob.perform_later
SampleActiveJob.perform_later
begin
FailingActiveJob.perform_later
rescue StandardError
nil
end
end.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_executed_total).with(
{ queue: "default", worker: "SampleActiveJob" } => 2,
{ queue: "default", worker: "FailingActiveJob" } => 1,
).and \
increment_yabeda_counter(Yabeda.sidekiq.jobs_success_total).with(
{ queue: "default", worker: "SampleActiveJob" } => 2,
).and \
increment_yabeda_counter(Yabeda.sidekiq.jobs_failed_total).with(
{ queue: "default", worker: "FailingActiveJob" } => 1,
).and \
measure_yabeda_histogram(Yabeda.sidekiq.job_runtime).with(
{ queue: "default", worker: "SampleActiveJob" } => kind_of(Numeric),
{ queue: "default", worker: "FailingActiveJob" } => kind_of(Numeric),
)
end
end
describe "#yabeda_tags worker method" do
it "uses custom labels for both sidekiq and application metrics", sidekiq: :inline do
expect { SampleComplexJob.perform_async }.to \
increment_yabeda_counter(Yabeda.sidekiq.jobs_executed_total).with(
{ queue: "default", worker: "SampleComplexJob", implicit: true } => 1,
).and \
measure_yabeda_histogram(Yabeda.sidekiq.job_runtime).with(
{ queue: "default", worker: "SampleComplexJob", implicit: true } => kind_of(Numeric),
).and \
increment_yabeda_counter(Yabeda.test.whatever).with(
{ explicit: true, implicit: true } => 1,
)
end
end
describe "collection of Sidekiq statistics" do
before do
allow(Sidekiq::Stats).to receive(:new).and_return(
OpenStruct.new(
processes_size: 1,
workers_size: 10,
retry_size: 1,
scheduled_size: 2,
dead_size: 3,
processed: 42,
failed: 13,
queues: { "default" => 5, "mailers" => 4 },
),
)
allow(Sidekiq::Queue).to receive(:all).and_return(
[
OpenStruct.new({ name: "default", latency: 0.5 }),
OpenStruct.new({ name: "mailers", latency: 0 }),
],
)
end
it "collects queue latencies" do
expect { Yabeda.collect! }.to \
update_yabeda_gauge(Yabeda.sidekiq.queue_latency).with(
{ queue: "default" } => 0.5,
{ queue: "mailers" } => 0.0,
)
end
it "collects queue sizes" do
expect { Yabeda.collect! }.to \
update_yabeda_gauge(Yabeda.sidekiq.jobs_waiting_count).with(
{ queue: "default" } => 5,
{ queue: "mailers" } => 4,
)
end
it "collects named queues stats", :aggregate_failures do
expect { Yabeda.collect! }.to \
update_yabeda_gauge(Yabeda.sidekiq.jobs_retry_count).with(1).and \
update_yabeda_gauge(Yabeda.sidekiq.jobs_dead_count).with(3).and \
update_yabeda_gauge(Yabeda.sidekiq.jobs_scheduled_count).with(2)
end
it "measures maximum runtime of currently running jobs", sidekiq: :inline do
workers = []
workers.push(Thread.new { SampleLongRunningJob.perform_async })
sleep 0.015 # Ruby can sleep less than requested
workers.push(Thread.new { SampleLongRunningJob.perform_async })
expect { Yabeda.collect! }.to \
update_yabeda_gauge(Yabeda.sidekiq.running_job_runtime).with(
{ queue: "default", worker: "SampleLongRunningJob" } => (be >= 0.010),
)
sleep 0.015 # Ruby can sleep less than requested
begin
FailingActiveJob.perform_later
rescue StandardError
nil
end
expect { Yabeda.collect! }.to \
update_yabeda_gauge(Yabeda.sidekiq.running_job_runtime).with(
{ queue: "default", worker: "SampleLongRunningJob" } => (be >= 0.020),
)
# When all jobs are completed, metric should respond with zero
workers.map(&:join)
expect { Yabeda.collect! }.to \
update_yabeda_gauge(Yabeda.sidekiq.running_job_runtime).with(
{ queue: "default", worker: "SampleLongRunningJob" } => 0.0,
)
end
end
def add_reroute_jobs_middleware
::Sidekiq.configure_server do |config|
config.client_middleware do |chain|
chain.insert_before Yabeda::Sidekiq::ClientMiddleware, ReRouteJobsMiddleware
end
end
end
def remove_reroute_jobs_middleware
::Sidekiq.configure_server do |config|
config.client_middleware do |chain|
chain.remove ReRouteJobsMiddleware
end
end
end
end