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-1.28.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.24.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.23.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.22.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.21.1 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.21.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.20.1 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.20.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.19.1 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.19.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.18.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.17.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.15.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.14.2 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.14.1 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.14.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.13.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.12.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.11.0 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb
gitlab_quality-test_tooling-1.10.1 lib/gitlab_quality/test_tooling/system_logs/finders/json_log_finder.rb