Commit 7d19312f authored by Oswaldo Ferreira's avatar Oswaldo Ferreira
Browse files

Prepare context and Sidekiq middleware for new attributes

parent 5ecbf14c
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ module Labkit
    LOG_KEY = "meta"
    CORRELATION_ID_KEY = "correlation_id"
    RAW_KEYS = [CORRELATION_ID_KEY].freeze
    KNOWN_KEYS = %w[user project root_namespace].freeze
    KNOWN_KEYS = %w[user project root_namespace subscription_plan caller_id].freeze

    class << self
      def with_context(attributes = {})
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ module Labkit
      # be reinstantiated by Sidekiq-server when running the job.
      class Client
        def call(_worker_class, job, _queue, _redis_pool)
          Labkit::Context.with_context do |context|
          Labkit::Context.with_context(caller_id: job["class"]) do |context|
            job.merge!(context.to_h)

            yield
+15 −0
Original line number Diff line number Diff line
@@ -17,6 +17,21 @@ describe Labkit::Context do
      expect(data_from(inner)).to eq(log_hash(described_class::CORRELATION_ID_KEY => "hello", "user" => "username"))
    end

    it "builds a context with overwritten key/values in the newer context" do
      inner = nil
      outer = nil

      described_class.with_context(caller_id: "foo") do |outer_context|
        outer = outer_context
        described_class.with_context(caller_id: "bar") do |inner_context|
          inner = inner_context
        end
      end

      expect(data_from(outer)).to include(log_hash(caller_id: "foo"))
      expect(data_from(inner)).to include(log_hash(caller_id: "bar"))
    end

    it "yields the block" do
      expect { |b| described_class.with_context(&b) }.to yield_control
    end
+4 −2
Original line number Diff line number Diff line
@@ -4,7 +4,9 @@ require "sidekiq/testing"

describe Labkit::Middleware::Sidekiq::Client do
  let(:test_worker) do
    Class.new { include Sidekiq::Worker }
    class DummyWorker
      include Sidekiq::Worker
    end
  end

  before do
@@ -30,7 +32,7 @@ describe Labkit::Middleware::Sidekiq::Client do
  end

  it "adds the metadata to a job" do
    context = { "project" => "jane.doe/bookstore", "user" => "jane.doe" }
    context = { "project" => "jane.doe/bookstore", "user" => "jane.doe", "caller_id" => "DummyWorker" }
    expected_metadata = context.transform_keys { |k| "#{Labkit::Context::LOG_KEY}.#{k}" }
    jid = nil