Class: Gitlab::ApplicationContext
- Inherits:
-
Object
- Object
- Gitlab::ApplicationContext
- 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
Class Method Summary collapse
-
.allowed_job_keys ⇒ Object
Sidekiq jobs may be deleted by matching keys in ApplicationContext.
- .application_attributes ⇒ Object
- .current ⇒ Object
- .current_context_attribute(attribute_name) ⇒ Object
- .current_context_include?(attribute_name) ⇒ Boolean
- .known_keys ⇒ Object
- .push(args) ⇒ Object
- .with_context(args, &block) ⇒ Object
- .with_raw_context(attributes = {}, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(**args) ⇒ ApplicationContext
constructor
A new instance of ApplicationContext.
-
#to_lazy_hash ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity – inherently leads to higher cyclomatic due to all the conditional assignments, the added complexity from adding more abstractions like
assign_hash_if_valueis not worth the tradeoff. -
#use ⇒ Object
rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/AbcSize rubocop: enable Metrics/PerceivedComplexity.
Constructor Details
#initialize(**args) ⇒ ApplicationContext
Returns a new instance of ApplicationContext.
123 124 125 126 127 128 129 130 131 |
# File 'lib/gitlab/application_context.rb', line 123 def initialize(**args) unknown_attributes = args.keys - self.class.application_attributes.map(&:name) raise ArgumentError, "#{unknown_attributes} are not known keys" if unknown_attributes.any? @set_values = args.keys assign_attributes(args) set_attr_readers end |
Class Method Details
.allowed_job_keys ⇒ Object
Sidekiq jobs may be deleted by matching keys in ApplicationContext. Filter out keys that aren’t available in Sidekiq jobs.
89 90 91 |
# File 'lib/gitlab/application_context.rb', line 89 def self.allowed_job_keys known_keys - WEB_ONLY_KEYS end |
.application_attributes ⇒ Object
93 94 95 |
# File 'lib/gitlab/application_context.rb', line 93 def self.application_attributes APPLICATION_ATTRIBUTES end |
.current ⇒ Object
111 112 113 |
# File 'lib/gitlab/application_context.rb', line 111 def self.current Labkit::Context.current.to_h end |
.current_context_attribute(attribute_name) ⇒ Object
119 120 121 |
# File 'lib/gitlab/application_context.rb', line 119 def self.current_context_attribute(attribute_name) Labkit::Context.current&.get_attribute(attribute_name) end |
.current_context_include?(attribute_name) ⇒ Boolean
115 116 117 |
# File 'lib/gitlab/application_context.rb', line 115 def self.current_context_include?(attribute_name) current.include?(Labkit::Context.log_key(attribute_name)) end |
.known_keys ⇒ Object
83 84 85 |
# File 'lib/gitlab/application_context.rb', line 83 def self.known_keys KNOWN_KEYS end |
.push(args) ⇒ Object
106 107 108 109 |
# File 'lib/gitlab/application_context.rb', line 106 def self.push(args) application_context = new(**args) Labkit::Context.push(application_context.to_lazy_hash) end |
.with_context(args, &block) ⇒ Object
97 98 99 100 |
# File 'lib/gitlab/application_context.rb', line 97 def self.with_context(args, &block) application_context = new(**args) application_context.use(&block) end |
.with_raw_context(attributes = {}, &block) ⇒ Object
102 103 104 |
# File 'lib/gitlab/application_context.rb', line 102 def self.with_raw_context(attributes = {}, &block) Labkit::Context.with_context(attributes, &block) end |
Instance Method Details
#to_lazy_hash ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity – inherently leads to higher cyclomatic due to
all the conditional assignments, the added complexity from adding more abstractions like
`assign_hash_if_value` is not worth the tradeoff.
rubocop: disable Metrics/PerceivedComplexity – same as above
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/gitlab/application_context.rb', line 138 def to_lazy_hash {}.tap do |hash| 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) assign_hash_if_value(hash, :artifact_used_cdn) assign_hash_if_value(hash, :artifacts_dependencies_size) assign_hash_if_value(hash, :artifacts_dependencies_count) assign_hash_if_value(hash, :merge_action_status) assign_hash_if_value(hash, :bulk_import_entity_id) assign_hash_if_value(hash, :sidekiq_destination_shard_redis) assign_hash_if_value(hash, :auth_fail_reason) assign_hash_if_value(hash, :auth_fail_token_id) assign_hash_if_value(hash, :auth_fail_requested_scopes) assign_hash_if_value(hash, :http_router_rule_action) assign_hash_if_value(hash, :http_router_rule_type) assign_hash_if_value(hash, :bulk_import_entity_id) hash[:user] = -> { username } if include_user? hash[Labkit::Fields::GL_USER_ID] = -> { user_id } if include_user? hash[:scoped_user] = -> { scoped_user&.username } if include_scoped_user? hash[:scoped_user_id] = -> { scoped_user&.id } if include_scoped_user? hash[:project] = -> { project_path } if include_project? hash[:organization_id] = -> { organization&.id } if set_values.include?(:organization) hash[:root_namespace] = -> { root_namespace_path } if include_namespace? hash[:client_id] = -> { client } if include_client? 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) hash[:kubernetes_agent_id] = -> { kubernetes_agent&.id } if set_values.include?(:kubernetes_agent) end end |
#use ⇒ Object
rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/AbcSize rubocop: enable Metrics/PerceivedComplexity
176 177 178 |
# File 'lib/gitlab/application_context.rb', line 176 def use Labkit::Context.with_context(to_lazy_hash) { yield } end |