Class: Salopulse::Instrumentation::SidekiqServerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/salopulse/instrumentation/sidekiq_server_middleware.rb

Constant Summary collapse

PARENT_REQUEST_ID_KEY =
"salopulse_parent_request_id".freeze

Instance Method Summary collapse

Constructor Details

#initialize(client = Salopulse::Client.instance) ⇒ SidekiqServerMiddleware

Returns a new instance of SidekiqServerMiddleware.



8
9
10
# File 'lib/salopulse/instrumentation/sidekiq_server_middleware.rb', line 8

def initialize(client = Salopulse::Client.instance)
  @client = client
end

Instance Method Details

#call(worker, job, _queue) ⇒ Object



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
# File 'lib/salopulse/instrumentation/sidekiq_server_middleware.rb', line 12

def call(worker, job, _queue)
  return yield if @client.disabled?

  endpoint = derive_endpoint(worker, job)
  parent_request_id = job[PARENT_REQUEST_ID_KEY]
  Salopulse::RequestContext.start(
    endpoint: endpoint,
    http_method: "JOB",
    source: :sidekiq,
    parent_request_id: parent_request_id
  )

  status = 200
  begin
    yield
  rescue Exception => e # rubocop:disable Lint/RescueException
    status = 500
    @client.capture_exception(e)
    raise
  ensure
    begin
      @client.capture_performance(
        endpoint: endpoint,
        http_method: "JOB",
        duration_ms: Salopulse::RequestContext.elapsed_ms,
        status_code: status
      )
      @client.flush_request_scope_events
    rescue StandardError
      # never let SDK errors propagate
    end
    Salopulse::RequestContext.clear
  end
end