Commit 5fb4ccfb authored by Elliot Forbes's avatar Elliot Forbes 2️⃣
Browse files

Merge branch 'dbarrett/deprecated_fields' into 'master'

feat(fields): Add deprecated field mappings module

See merge request !222

Merged-by: Elliot Forbes's avatarElliot Forbes <eforbes@gitlab.com>
Approved-by: default avatarPeter Leitzen <pleitzen@gitlab.com>
Approved-by: Elliot Forbes's avatarElliot Forbes <eforbes@gitlab.com>
Reviewed-by: Bob Van Landuyt's avatarBob Van Landuyt <bob@gitlab.com>
Reviewed-by: default avatarPeter Leitzen <pleitzen@gitlab.com>
Co-authored-by: default avatardbarrett <dbarrett@gitlab.com>
parents d6eb679b 80dd0d62
Loading
Loading
Loading
Loading
Loading
+49 −2
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ module Labkit
  # Fields is intended to be a SSOT for all of the common field names that
  # we emit via any observability we add to our systems.
  #
  # These fields should span multiple services. This is
  # These fields should span multiple services.
  #
  # The goal of this package is to reduce the likelihood for typos or
  # subtly different naming conventions. This will help to ensure we
@@ -30,7 +30,6 @@ module Labkit
  module Fields
    # correlation_id - string
    #
    # correlation_id - string
    # This field is used to correlate
    # the logs emitted by all of our systems.
    # This should be present in all log line
@@ -50,5 +49,53 @@ module Labkit
    # should clearly indicate what the intended use of the
    # field is and should be replicated across the labkit
    # variations.

    # Get the constant name for a field value
    # @param field_value [String] The field value (e.g., "gl_user_id")
    # @return [String, nil] The constant name (e.g., "GL_USER_ID") or nil if not found
    def self.constant_name_for(field_value)
      constants(false).find do |const_name|
        next if const_name == :Deprecated

        const_get(const_name) == field_value
      end&.to_s
    end

    module Deprecated
      # This module tracks deprecated field names and maps them to their
      # standard replacements. These mappings are used by the field scanner
      # to identify and track usage of deprecated fields in the codebase.

      MAPPINGS = {
        Fields::GL_USER_ID => %w[user_id userid],
      }.freeze

      class << self
        # Get all deprecated fields as a lookup hash
        #
        # @return [Hash{String => String}] Hash mapping deprecated field names to standard field names
        def all
          @all ||= MAPPINGS.each_with_object({}) do |(key, values), result|
            values.each { |v| result[v] = key }
          end
        end

        # Check if a field is deprecated
        #
        # @param field_name [String, Symbol] The field name to check
        # @return [Boolean] true if the field is deprecated
        def deprecated?(field_name)
          all.key?(field_name.to_s)
        end

        # Get the standard field for a deprecated field
        #
        # @param deprecated_field [String, Symbol] The deprecated field name
        # @return [String, nil] The standard field name, or nil if not found
        def standard_field_for(deprecated_field)
          all[deprecated_field.to_s]
        end
      end
    end
  end
end
+45 −0
Original line number Diff line number Diff line
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Labkit::Fields do
  describe Labkit::Fields::Deprecated do
    describe '.all' do
      it 'returns a hash mapping deprecated field names to standard field names' do
        expect(described_class.all).to be_a(Hash)
        expect(described_class.all.keys).to all(be_a(String))
        expect(described_class.all.values).to all(be_a(String))
      end

      it 'maps deprecated fields to their standard equivalents' do
        expect(described_class.all['user_id']).to eq(Labkit::Fields::GL_USER_ID)
      end
    end

    describe '.deprecated?' do
      it 'returns true for a known deprecated field' do
        result = described_class.deprecated?('user_id')
        expect(result).to be(true)
      end

      it 'returns false for a known standard field' do
        result = described_class.deprecated?(Labkit::Fields::GL_USER_NAME)
        expect(result).to be(false)
      end
    end

    describe '.standard_field_for' do
      it 'returns a standard field for a known deprecated field' do
        result = described_class.standard_field_for('user_id')
        expect(result).to be(Labkit::Fields::GL_USER_ID)
      end
    end

    describe 'MAPPINGS' do
      it 'has no duplicate deprecated field names' do
        all_deprecated = described_class::MAPPINGS.values.flatten
        expect(all_deprecated).to eq(all_deprecated.uniq)
      end
    end
  end
end