Class: EzLogsAgent::Capturers::JobCapturer::ServerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ez_logs_agent/capturers/job_capturer.rb

Overview

Sidekiq server middleware that captures background job execution as background_job events.

This middleware runs at execution-time (when the job runs), NOT at enqueue-time.

Behavior

  • Restores correlation_id from job (or generates new one)

  • Measures job execution duration

  • Captures success or failure outcome

  • Re-raises exceptions after capturing failure

  • Always clears correlation_id in ensure block

  • Respects capture_jobs configuration flag

Registration

Note: This middleware is automatically registered by Railtie. Users do NOT need to manually configure it.

For manual setup (if not using Rails), add to your Sidekiq initializer:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add EzLogsAgent::Capturers::JobCapturer::ServerMiddleware
  end
end

Instance Method Summary collapse

Instance Method Details

#call(worker, job, queue) { ... } ⇒ void

This method returns an undefined value.

Sidekiq server middleware hook.

Parameters:

  • worker (Object)

    The worker instance executing the job

  • job (Hash)

    The Sidekiq job payload

  • queue (String)

    The queue name

Yields:

  • Executes the job



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
# File 'lib/ez_logs_agent/capturers/job_capturer.rb', line 103

def call(worker, job, queue)
  # Skip capture if disabled
  return yield unless EzLogsAgent.configuration.capture_jobs

  # Skip excluded job classes (health checks, infrastructure jobs).
  # Match against the underlying ActiveJob class too, not just the wrapper.
  if excluded_job_class?(worker, job)
    EzLogsAgent::Logger.debug("[JobCapturer::ServerMiddleware] Skipping capture (excluded job class: #{resolve_job_class(worker, job)})")
    return yield
  end

  # Restore correlation_id from job payload (or generate new one)
  correlation_id = job["correlation_id"] || EzLogsAgent::Correlation.generate
  EzLogsAgent::Correlation.current = correlation_id

  # Measure execution time
  start_time = Time.now
  result = yield
  duration_ms = ((Time.now - start_time) * 1000).to_i

  # Capture success event
  capture_success(worker, job, queue, correlation_id, duration_ms, start_time)

  result
rescue StandardError => error
  # Capture failure event, then re-raise
  capture_failure(worker, job, queue, correlation_id, error, start_time)
  raise
ensure
  # Always clear correlation_id
  EzLogsAgent::Correlation.clear
end