Module: AllStak::Integrations::Sidekiq

Defined in:
lib/allstak/integrations/sidekiq.rb

Overview

Sidekiq integration.

Provides a Sidekiq server middleware that:

1. Starts a fresh trace per job (so spans/telemetry produced inside the
   job link together and don't bleed across jobs on a reused thread).
2. Wraps job execution in a "queue.process" span + breadcrumb.
3. Auto-captures the exception when a job raises, attaching worker
   class, jid, queue, and (sanitized) args as metadata, then re-raises
   so Sidekiq's own retry machinery still runs.

It also registers a ‘death_handler` so jobs that exhaust their retries are captured once more with `mechanism=sidekiq.death` for visibility.

Installation is guarded: ‘install!` is a graceful no-op when Sidekiq is not loaded in the host process, and is idempotent.

Defined Under Namespace

Classes: Middleware

Class Method Summary collapse

Class Method Details

.capture_death(job, exception) ⇒ Object

Capture a job that has exhausted all retries (Sidekiq “death”). Best-effort; never raises into Sidekiq’s death-handler loop.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/allstak/integrations/sidekiq.rb', line 48

def self.capture_death(job, exception)
  return unless AllStak.initialized?
  return if exception.nil?
  client = AllStak.client
  config = client.config
  return unless config.capture_unhandled_exceptions

  job = job || {}
  meta = (job).merge(
    "mechanism" => "sidekiq.death",
    "handled"   => false
  )
  client.errors.capture_exception(exception, metadata: meta)
rescue => e
  begin
    AllStak.client.config.debug && warn("[AllStak] sidekiq death capture failed: #{e.message}")
  rescue
    nil
  end
end

.install!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/allstak/integrations/sidekiq.rb', line 21

def self.install!
  return if @installed
  return unless defined?(::Sidekiq)

  ::Sidekiq.configure_server do |sidekiq_config|
    sidekiq_config.server_middleware do |chain|
      chain.add(AllStak::Integrations::Sidekiq::Middleware) unless chain.exists?(AllStak::Integrations::Sidekiq::Middleware)
    end

    # Retries-exhausted handler. The death handler API differs across
    # Sidekiq majors; both forms accept a (job, exception) callable.
    if sidekiq_config.respond_to?(:death_handlers)
      sidekiq_config.death_handlers << lambda do |job, exception|
        AllStak::Integrations::Sidekiq.capture_death(job, exception)
      end
    end
  end

  @installed = true
end

.installed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/allstak/integrations/sidekiq.rb', line 42

def self.installed?
  @installed == true
end

.job_metadata(job) ⇒ Object

Build sanitized job metadata from a Sidekiq job hash. Args are routed through the Sanitizer (KEY-NAME redaction only) so secrets in argument hashes are redacted here; value-pattern PII scrubbing (email/IP/CC/SSN, gated by send_default_pii) is applied authoritatively on the wire path, so we don’t double-scrub free text at this layer.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/allstak/integrations/sidekiq.rb', line 75

def self.(job)
  job ||= {}
  args = job["args"]
  sanitized_args =
    begin
      AllStak::Sanitizer.scrub(args, values: false) if args
    rescue
      nil
    end
  {
    "sidekiq.class" => job["class"] || job["wrapped"],
    "sidekiq.jid"   => job["jid"],
    "sidekiq.queue" => job["queue"],
    "sidekiq.retry_count" => job["retry_count"],
    "sidekiq.args"  => sanitized_args
  }.reject { |_, v| v.nil? }
end