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
|
|
|
|
|
2020-03-26 08:07:48 -04:00
|
|
|
Attribute = Struct.new(:name, :type)
|
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),
|
2020-03-26 08:07:48 -04:00
|
|
|
Attribute.new(:caller_id, String),
|
|
|
|
Attribute.new(:related_class, String)
|
2020-01-17 16:08:29 -05:00
|
|
|
].freeze
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def self.push(args)
|
|
|
|
application_context = new(**args)
|
|
|
|
Labkit::Context.push(application_context.to_lazy_hash)
|
|
|
|
end
|
|
|
|
|
2020-04-15 14:09:36 -04:00
|
|
|
def self.current_context_include?(attribute_name)
|
|
|
|
Labkit::Context.current.to_h.include?(Labkit::Context.log_key(attribute_name))
|
|
|
|
end
|
|
|
|
|
2020-01-17 16:08:29 -05:00
|
|
|
def initialize(**args)
|
|
|
|
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
|
|
|
|
|
|
|
|
def to_lazy_hash
|
2020-01-17 16:08:29 -05:00
|
|
|
{}.tap do |hash|
|
|
|
|
hash[:user] = -> { username } if set_values.include?(:user)
|
|
|
|
hash[:project] = -> { project_path } if set_values.include?(:project)
|
|
|
|
hash[:root_namespace] = -> { root_namespace_path } if include_namespace?
|
2020-01-23 10:08:46 -05:00
|
|
|
hash[:caller_id] = caller_id if set_values.include?(:caller_id)
|
2020-03-26 08:07:48 -04:00
|
|
|
hash[:related_class] = related_class if set_values.include?(:related_class)
|
2020-01-17 16:08:29 -05:00
|
|
|
end
|
2020-01-03 10:08:33 -05:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
project&.full_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
user&.username
|
|
|
|
end
|
|
|
|
|
|
|
|
def root_namespace_path
|
|
|
|
if namespace
|
|
|
|
namespace.full_path_components.first
|
|
|
|
else
|
|
|
|
project&.full_path_components&.first
|
|
|
|
end
|
|
|
|
end
|
2020-01-17 16:08:29 -05:00
|
|
|
|
|
|
|
def include_namespace?
|
|
|
|
set_values.include?(:namespace) || set_values.include?(:project)
|
|
|
|
end
|
2020-01-03 10:08:33 -05:00
|
|
|
end
|
|
|
|
end
|
2020-01-23 10:08:46 -05:00
|
|
|
|
|
|
|
Gitlab::ApplicationContext.prepend_if_ee('EE::Gitlab::ApplicationContext')
|