Verified Commit fe580970 authored by Alejandro Rodríguez's avatar Alejandro Rodríguez 🌴
Browse files

Add metadata to headers in Rack middleware

parent c4bc58d3
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
# frozen_string_literal: true

require "action_dispatch"
require "json"

module Labkit
  module Middleware
    HEADER = "X-Gitlab-Meta"

    # This is a rack middleware to be inserted in GitLab-rails
    # It makes sure that there's always a root context containing the correlation
    # id.
@@ -15,8 +18,12 @@ module Labkit
      end

      def call(env)
        Labkit::Context.with_context(Labkit::Context::CORRELATION_ID_KEY => correlation_id(env)) do
          @app.call(env)
        Labkit::Context.with_context(Labkit::Context::CORRELATION_ID_KEY => correlation_id(env)) do |context|
          status, headers, response = @app.call(env)

          headers[HEADER] = context_to_json(context)

          [status, headers, response]
        end
      end

@@ -29,6 +36,14 @@ module Labkit
      def request(env)
        ActionDispatch::Request.new(env)
      end

      def context_to_json(context)
        context
          .to_h
          .transform_keys { |k| k.delete_prefix("meta.") }
          .merge("version" => "1")
          .to_json
      end
    end
  end
end
+21 −6
Original line number Diff line number Diff line
@@ -2,23 +2,38 @@

describe Labkit::Middleware::Rack do
  let(:app) { double("app") }
  let(:correlation_id) { "the id" }
  let(:metadata) { { "feature_category" => "issue_tracking" } }
  let(:header_metadata) { metadata.merge({ "version" => "1", Labkit::Context::CORRELATION_ID_KEY => correlation_id }) }
  let(:env) { {} }
  let(:fake_request) { double("request") }

  before do
    allow(ActionDispatch::Request).to receive(:new).with(env).and_return(fake_request)
    allow(fake_request).to receive(:request_id).and_return(correlation_id)
  end

  describe "#call" do
    it "adds the correlation id from the request to the context" do
      fake_request = double("request")

      expect(ActionDispatch::Request).to receive(:new).with(env).and_return(fake_request)
      expect(fake_request).to receive(:request_id).and_return("the id")
      expect(Labkit::Context).to receive(:with_context).with(a_hash_including(Labkit::Context::CORRELATION_ID_KEY => "the id"))
      expect(Labkit::Context).to receive(:with_context).with(a_hash_including(Labkit::Context::CORRELATION_ID_KEY => correlation_id))

      described_class.new(app).call(env)
    end

    it "calls the app" do
      expect(app).to receive(:call).with(env)
      expect(app).to receive(:call).with(env).and_return([nil, {}, nil])

      described_class.new(app).call(env)
    end

    it "injects meta headers" do
      Labkit::Context.push(metadata)

      expect(app).to receive(:call).with(env).and_return([nil, {}, nil])

      _, headers, _ = described_class.new(app).call(env)

      expect(JSON.parse(headers["X-Gitlab-Meta"])).to eq(header_metadata)
    end
  end
end