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

Remove unused metadata header methods

Since we decided to go with a unified header approach
parent fe580970
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ module Labkit
    LOG_KEY = "meta"
    CORRELATION_ID_KEY = "correlation_id"
    RAW_KEYS = [CORRELATION_ID_KEY].freeze
    HEADER_PREFIX = "X-Gitlab-"

    class << self
      def with_context(attributes = {})
@@ -71,10 +70,6 @@ module Labkit
        @known_log_keys ||= (KNOWN_KEYS.map(&method(:log_key)) + RAW_KEYS).freeze
      end

      def header_name(name)
        HEADER_PREFIX + log_key(name).titlecase(keep_id_suffix: true).gsub(/\W/, "-")
      end

      private

      def contexts
@@ -103,12 +98,6 @@ module Labkit
      data[CORRELATION_ID_KEY]
    end

    def to_headers
      to_h.except(CORRELATION_ID_KEY).transform_keys do |key|
        self.class.header_name(key)
      end
    end

    def get_attribute(attribute)
      raw = call_or_value(data[log_key(attribute)])

+0 −47
Original line number Diff line number Diff line
@@ -107,25 +107,6 @@ describe Labkit::Context do
    end
  end

  describe ".header_name" do
    using RSpec::Parameterized::TableSyntax

    where(:value, :result) do
      :caller_id | "X-Gitlab-Meta-Caller-Id"
      :root_namespace | "X-Gitlab-Meta-Root-Namespace"
      :user | "X-Gitlab-Meta-User"
      :unknown | "X-Gitlab-Meta-Unknown"
      "meta.user" | "X-Gitlab-Meta-User"
      described_class::CORRELATION_ID_KEY | "X-Gitlab-Correlation-Id"
    end

    with_them do
      it do
        expect(described_class.header_name(value)).to eq(result)
      end
    end
  end

  describe "#to_h" do
    let(:expected_hash) do
      log_hash(user: "user",
@@ -181,34 +162,6 @@ describe Labkit::Context do
    end
  end

  describe "#to_headers" do
    it "returns a hash of header names to values, excluding correlation ID" do
      context = described_class.new(
        user: -> { "user" },
        root_namespace: -> { "namespace" },
        project: -> { "project" },
        caller_id: "ProjectsController#show",
        random_thing: "Random",
      )

      expect(context.to_headers).to include("X-Gitlab-Meta-User" => "user",
                                            "X-Gitlab-Meta-Root-Namespace" => "namespace",
                                            "X-Gitlab-Meta-Project" => "project",
                                            "X-Gitlab-Meta-Caller-Id" => "ProjectsController#show",
                                            "X-Gitlab-Meta-Random-Thing" => "Random")
    end

    it "does not include empty values" do
      context = described_class.new(
        user: -> { },
        root_namespace: nil,
        project: "",
      )

      expect(context.to_headers).to be_empty
    end
  end

  describe "#initialize" do
    it "assigns all keys as strings" do
      context = described_class.new(