Class: Gitlab::ApplicationContext

Inherits:
Object
  • Object
show all
Includes:
Utils::LazyAttributes, Utils::StrongMemoize
Defined in:
lib/gitlab/application_context.rb

Overview

A GitLab-rails specific accessor for `Labkit::Logging::ApplicationContext`

Defined Under Namespace

Classes: Attribute

Constant Summary collapse

LOG_KEY =
Labkit::Context::LOG_KEY
APPLICATION_ATTRIBUTES =
[
  Attribute.new(:project, Project),
  Attribute.new(:namespace, Namespace),
  Attribute.new(:user, User),
  Attribute.new(:runner, ::Ci::Runner),
  Attribute.new(:caller_id, String),
  Attribute.new(:remote_ip, String),
  Attribute.new(:job, ::Ci::Build),
  Attribute.new(:related_class, String),
  Attribute.new(:feature_category, String),
  Attribute.new(:artifact, ::Ci::JobArtifact)
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(**args) ⇒ ApplicationContext

Returns a new instance of ApplicationContext.

Raises:

  • (ArgumentError)

70
71
72
73
74
75
76
77
# File 'lib/gitlab/application_context.rb', line 70

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)
end

Class Method Details

.currentObject


58
59
60
# File 'lib/gitlab/application_context.rb', line 58

def self.current
  Labkit::Context.current.to_h
end

.current_context_attribute(attribute_name) ⇒ Object


66
67
68
# File 'lib/gitlab/application_context.rb', line 66

def self.current_context_attribute(attribute_name)
  Labkit::Context.current&.get_attribute(attribute_name)
end

.current_context_include?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/gitlab/application_context.rb', line 62

def self.current_context_include?(attribute_name)
  current.include?(Labkit::Context.log_key(attribute_name))
end

.known_keysObject


40
41
42
# File 'lib/gitlab/application_context.rb', line 40

def self.known_keys
  KNOWN_KEYS
end

.push(args) ⇒ Object


53
54
55
56
# File 'lib/gitlab/application_context.rb', line 53

def self.push(args)
  application_context = new(**args)
  Labkit::Context.push(application_context.to_lazy_hash)
end

.with_context(args, &block) ⇒ Object


44
45
46
47
# File 'lib/gitlab/application_context.rb', line 44

def self.with_context(args, &block)
  application_context = new(**args)
  application_context.use(&block)
end

.with_raw_context(attributes = {}, &block) ⇒ Object


49
50
51
# File 'lib/gitlab/application_context.rb', line 49

def self.with_raw_context(attributes = {}, &block)
  Labkit::Context.with_context(attributes, &block)
end

Instance Method Details

#to_lazy_hashObject

rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gitlab/application_context.rb', line 81

def to_lazy_hash
  {}.tap do |hash|
    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?
    hash[:caller_id] = caller_id if set_values.include?(:caller_id)
    hash[:remote_ip] = remote_ip if set_values.include?(:remote_ip)
    hash[:related_class] = related_class if set_values.include?(:related_class)
    hash[:feature_category] = feature_category if set_values.include?(:feature_category)
    hash[:pipeline_id] = -> { job&.pipeline_id } if set_values.include?(:job)
    hash[:job_id] = -> { job&.id } if set_values.include?(:job)
    hash[:artifact_size] = -> { artifact&.size } if set_values.include?(:artifact)
  end
end

#useObject

rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity


99
100
101
# File 'lib/gitlab/application_context.rb', line 99

def use
  Labkit::Context.with_context(to_lazy_hash) { yield }
end