Verified Commit eeac8300 authored by Sean McGivern's avatar Sean McGivern 🔴
Browse files

Set caller_id in Sidekiq based on wrapped job class if present

Mailer jobs in Sidekiq end up with `job` like this:

    {
      "class" => "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper",
      "wrapped" => "ActionMailer::MailDeliveryJob"
    }

We want the wrapped class for those cases as it's more specific.
parent 135c83c6
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -8,7 +8,8 @@ module Labkit
        # reinstantiate a context in which the job will run.
        class Server
          def call(_worker_class, job, _queue)
            data = job.merge(Labkit::Context.log_key(:caller_id) => job["class"])
            worker_name = (job["wrapped"].presence || job["class"]).to_s
            data = job.merge(Labkit::Context.log_key(:caller_id) => worker_name)

            Labkit::Context.with_context(data) do |_context|
              yield
+14 −0
Original line number Diff line number Diff line
@@ -36,6 +36,20 @@ describe Labkit::Middleware::Sidekiq::Server do
    it_behaves_like "calling server middleware", Labkit::Middleware::Sidekiq::Tracing::Server
  end

  context "setting caller_id" do
    it "sets caller_id to the job class" do
      expect(Labkit::Context).to receive(:with_context).with(a_hash_including("meta.caller_id" => "TestWorker"))

      Sidekiq::Client.push(job)
    end

    it "sets the caller_id to the wrapped job, if present" do
      expect(Labkit::Context).to receive(:with_context).with(a_hash_including("meta.caller_id" => "SomeOtherWorker"))

      Sidekiq::Client.push(job.merge("wrapped" => "SomeOtherWorker"))
    end
  end

  it "executes the job" do
    fake_job = TestWorker.new