-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmiddleware.rb
47 lines (38 loc) · 1.15 KB
/
middleware.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
module ZipkinTracer
module Sidekiq
class Middleware
attr_reader :config, :tracer, :traceable_workers
def initialize(config)
@config = Config.new(nil, config).freeze
@tracer = TracerFactory.new.tracer(@config)
@traceable_workers = config.fetch(:traceable_workers, [])
end
def call(worker, job, queue, &block)
return block.call unless traceable_worker?(worker)
trace(worker, job, queue, &block)
end
private
def traceable_worker?(worker)
traceable_workers.include?(:all) || traceable_workers.include?(worker_name(worker))
end
def trace(worker, job, queue, &block)
trace_id = TraceGenerator.new.next_trace_id
span_name = worker_name(worker)
result = TraceContainer.with_trace_id(trace_id) do
if trace_id.sampled?
tracer.with_new_span(trace_id, span_name) do
result = block.call
end
else
result = block.call
end
end
tracer.flush!
result
end
def worker_name(worker)
worker.class.to_s.to_sym
end
end
end
end