Sha256: 46dcda130b48806af8fef38cf91a4d97a30f74398a1a95fed4a058f026a186b1

Contents?: true

Size: 1.7 KB

Versions: 77

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'json'

module GitlabQuality
  module TestTooling
    module SystemLogs
      module Finders
        class JsonLogFinder
          def initialize(base_path, file_path)
            @base_path = base_path
            @file_path = file_path
          end

          def find(correlation_id)
            log_file_path = "#{@base_path}/#{@file_path}"
            logs = []

            if File.exist?(log_file_path) && !correlation_id.nil?
              File.foreach(log_file_path) do |line|
                begin
                  json_line = JSON.parse(line, symbolize_names: true)
                rescue JSON::ParserError
                  Runtime::Logger.debug("JsonLogFinder#find attempted to parse invalid JSON: #{line}")

                  next
                end

                if (json_line[:correlation_id])&.casecmp?(correlation_id)
                  normalized_line = normalize_keys(json_line)
                  logs << new_log(normalized_line)
                end
              end
            end

            logs
          end

          def new_log(_data)
            raise 'abstract method new_log must be defined!'
          end

          private

          def normalize_keys(json_line)
            normalized_hash = {}

            json_line.each_key do |old_key|
              key_string = old_key.to_s

              if key_string.include?('.')
                normalized_key = key_string.tr('.', '_').to_sym
                normalized_hash[normalized_key] = json_line[old_key]
              else
                normalized_hash[old_key] = json_line[old_key]
              end
            end

            normalized_hash
          end
        end
      end
    end
  end
end

Version data entries

77 entries across 77 versions & 1 rubygems

Version Path
gitlab_quality-test_tooling-2.5.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-2.4.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-2.3.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-2.2.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-2.1.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-2.0.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.39.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.38.1 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.38.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.37.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.36.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.35.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.34.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.33.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.32.1 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.32.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.31.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.30.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.29.1 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.29.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb