2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
# A GitLab-rails specific accessor for `Labkit::Logging::ApplicationContext`
|
|
|
|
class ApplicationContext
|
|
|
|
include Gitlab::Utils::LazyAttributes
|
2021-04-17 20:07:23 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
Attribute = Struct.new(:name, :type)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
LOG_KEY = Labkit::Context::LOG_KEY
|
2022-04-04 11:22:00 +05:30
|
|
|
KNOWN_KEYS = [
|
|
|
|
:user,
|
|
|
|
:project,
|
|
|
|
:root_namespace,
|
|
|
|
:client_id,
|
|
|
|
:caller_id,
|
|
|
|
:remote_ip,
|
2022-06-21 17:19:12 +05:30
|
|
|
:job_id,
|
|
|
|
:pipeline_id,
|
2022-04-04 11:22:00 +05:30
|
|
|
:related_class,
|
2022-07-16 23:28:13 +05:30
|
|
|
:feature_category,
|
|
|
|
:artifact_size
|
2022-04-04 11:22:00 +05:30
|
|
|
].freeze
|
|
|
|
private_constant :KNOWN_KEYS
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
APPLICATION_ATTRIBUTES = [
|
|
|
|
Attribute.new(:project, Project),
|
|
|
|
Attribute.new(:namespace, Namespace),
|
|
|
|
Attribute.new(:user, User),
|
2021-04-17 20:07:23 +05:30
|
|
|
Attribute.new(:runner, ::Ci::Runner),
|
2020-04-22 19:07:51 +05:30
|
|
|
Attribute.new(:caller_id, String),
|
2021-03-08 18:12:59 +05:30
|
|
|
Attribute.new(:remote_ip, String),
|
2022-06-21 17:19:12 +05:30
|
|
|
Attribute.new(:job, ::Ci::Build),
|
2021-01-03 14:25:43 +05:30
|
|
|
Attribute.new(:related_class, String),
|
2022-07-16 23:28:13 +05:30
|
|
|
Attribute.new(:feature_category, String),
|
|
|
|
Attribute.new(:artifact, ::Ci::JobArtifact)
|
2020-03-13 15:44:24 +05:30
|
|
|
].freeze
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def self.known_keys
|
|
|
|
KNOWN_KEYS
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def self.with_context(args, &block)
|
|
|
|
application_context = new(**args)
|
|
|
|
application_context.use(&block)
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def self.with_raw_context(attributes = {}, &block)
|
|
|
|
Labkit::Context.with_context(attributes, &block)
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def self.push(args)
|
|
|
|
application_context = new(**args)
|
|
|
|
Labkit::Context.push(application_context.to_lazy_hash)
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def self.current
|
|
|
|
Labkit::Context.current.to_h
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def self.current_context_include?(attribute_name)
|
2021-04-17 20:07:23 +05:30
|
|
|
current.include?(Labkit::Context.log_key(attribute_name))
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def self.current_context_attribute(attribute_name)
|
|
|
|
Labkit::Context.current&.get_attribute(attribute_name)
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def initialize(**args)
|
2020-03-13 15:44:24 +05:30
|
|
|
unknown_attributes = args.keys - APPLICATION_ATTRIBUTES.map(&:name)
|
|
|
|
raise ArgumentError, "#{unknown_attributes} are not known keys" if unknown_attributes.any?
|
|
|
|
|
|
|
|
@set_values = args.keys
|
|
|
|
|
|
|
|
assign_attributes(args)
|
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
# rubocop: disable Metrics/CyclomaticComplexity
|
|
|
|
# rubocop: disable Metrics/PerceivedComplexity
|
2020-03-13 15:44:24 +05:30
|
|
|
def to_lazy_hash
|
|
|
|
{}.tap do |hash|
|
2022-06-21 17:19:12 +05:30
|
|
|
hash[:user] = -> { username } if include_user?
|
|
|
|
hash[:project] = -> { project_path } if include_project?
|
2020-03-13 15:44:24 +05:30
|
|
|
hash[:root_namespace] = -> { root_namespace_path } if include_namespace?
|
2021-04-17 20:07:23 +05:30
|
|
|
hash[:client_id] = -> { client } if include_client?
|
2020-03-13 15:44:24 +05:30
|
|
|
hash[:caller_id] = caller_id if set_values.include?(:caller_id)
|
2021-03-08 18:12:59 +05:30
|
|
|
hash[:remote_ip] = remote_ip if set_values.include?(:remote_ip)
|
2020-04-22 19:07:51 +05:30
|
|
|
hash[:related_class] = related_class if set_values.include?(:related_class)
|
2021-01-03 14:25:43 +05:30
|
|
|
hash[:feature_category] = feature_category if set_values.include?(:feature_category)
|
2022-06-21 17:19:12 +05:30
|
|
|
hash[:pipeline_id] = -> { job&.pipeline_id } if set_values.include?(:job)
|
|
|
|
hash[:job_id] = -> { job&.id } if set_values.include?(:job)
|
2022-07-16 23:28:13 +05:30
|
|
|
hash[:artifact_size] = -> { artifact&.size } if set_values.include?(:artifact)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
# rubocop: enable Metrics/CyclomaticComplexity
|
|
|
|
# rubocop: enable Metrics/PerceivedComplexity
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def use
|
|
|
|
Labkit::Context.with_context(to_lazy_hash) { yield }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :set_values
|
|
|
|
|
|
|
|
APPLICATION_ATTRIBUTES.each do |attr|
|
|
|
|
lazy_attr_reader attr.name, type: attr.type
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_attributes(values)
|
|
|
|
values.slice(*APPLICATION_ATTRIBUTES.map(&:name)).each do |name, value|
|
|
|
|
instance_variable_set("@#{name}", value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_path
|
2022-06-21 17:19:12 +05:30
|
|
|
associated_routable = project || runner_project || job_project
|
2021-04-17 20:07:23 +05:30
|
|
|
associated_routable&.full_path
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2022-06-21 17:19:12 +05:30
|
|
|
associated_user = user || job_user
|
|
|
|
associated_user&.username
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def root_namespace_path
|
2022-06-21 17:19:12 +05:30
|
|
|
associated_routable = namespace || project || runner_project || runner_group || job_project
|
2021-04-17 20:07:23 +05:30
|
|
|
associated_routable&.full_path_components&.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_namespace?
|
2022-06-21 17:19:12 +05:30
|
|
|
set_values.include?(:namespace) || set_values.include?(:project) || set_values.include?(:runner) || set_values.include?(:job)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def include_client?
|
|
|
|
set_values.include?(:user) || set_values.include?(:runner) || set_values.include?(:remote_ip)
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def include_user?
|
|
|
|
set_values.include?(:user) || set_values.include?(:job)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_project?
|
|
|
|
set_values.include?(:project) || set_values.include?(:runner) || set_values.include?(:job)
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def client
|
2022-06-21 17:19:12 +05:30
|
|
|
if runner
|
2021-04-17 20:07:23 +05:30
|
|
|
"runner/#{runner.id}"
|
2022-06-21 17:19:12 +05:30
|
|
|
elsif user
|
|
|
|
"user/#{user.id}"
|
2020-03-13 15:44:24 +05:30
|
|
|
else
|
2021-04-17 20:07:23 +05:30
|
|
|
"ip/#{remote_ip}"
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def runner_project
|
|
|
|
strong_memoize(:runner_project) do
|
|
|
|
next unless runner&.project_type?
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
runner_projects = runner.runner_projects.take(2) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
runner_projects.first.project if runner_projects.one?
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def runner_group
|
|
|
|
strong_memoize(:runner_group) do
|
|
|
|
next unless runner&.group_type?
|
|
|
|
|
|
|
|
runner.groups.first
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
def job_project
|
|
|
|
strong_memoize(:job_project) do
|
|
|
|
job&.project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_user
|
|
|
|
strong_memoize(:job_user) do
|
|
|
|
job&.user
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Gitlab::ApplicationContext.prepend_mod_with('Gitlab::ApplicationContext')
|