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