Class: EzLogsAgent::Capturers::JobCapturer::ClientMiddleware

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

Overview

Sidekiq client middleware that propagates correlation_id from the current request context into enqueued job payloads.

This middleware runs at enqueue-time (when a job is scheduled), NOT at execution-time.

Behavior

  • Reads EzLogsAgent::Correlation.current

  • If present, injects it into job

  • Never overwrites existing job

  • Never raises exceptions

  • Always yields to next middleware

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_client do |config|
  config.client_middleware do |chain|
    chain.add EzLogsAgent::Capturers::JobCapturer::ClientMiddleware
  end
end

Instance Method Summary collapse

Instance Method Details

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

This method returns an undefined value.

Sidekiq client middleware hook.

Parameters:

  • worker_class (Class)

    The worker class being enqueued

  • job (Hash)

    The Sidekiq job payload (mutable)

  • queue (String)

    The queue name

  • redis_pool (ConnectionPool)

    Sidekiq’s Redis connection pool

Yields:

  • Passes control to the next middleware in the chain



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ez_logs_agent/capturers/job_capturer.rb', line 42

def call(worker_class, job, queue, redis_pool)
  # Defensive: ensure job is a Hash
  return yield unless job.is_a?(Hash)

  begin
    # Read current correlation_id from context
    correlation_id = EzLogsAgent::Correlation.current

    # Only inject if:
    # 1. correlation_id is present and not empty
    # 2. job doesn't already have one
    if correlation_id && correlation_id.respond_to?(:empty?) && !correlation_id.empty? && !job.key?("correlation_id")
      job["correlation_id"] = correlation_id
    end
  rescue StandardError => e
    # Never crash the host application during correlation injection
    # Log error, then continue
    EzLogsAgent::Logger.error("[JobCapturer::ClientMiddleware] Error during correlation injection: #{e.class} - #{e.message}")
  end

  # Always yield to next middleware
  yield
end