Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/application_context.rb')
-rw-r--r--lib/gitlab/application_context.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/gitlab/application_context.rb b/lib/gitlab/application_context.rb
new file mode 100644
index 00000000000..b9190b519a0
--- /dev/null
+++ b/lib/gitlab/application_context.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+module Gitlab
+ # A GitLab-rails specific accessor for `Labkit::Logging::ApplicationContext`
+ class ApplicationContext
+ include Gitlab::Utils::LazyAttributes
+
+ def self.with_context(args, &block)
+ application_context = new(**args)
+ Labkit::Context.with_context(application_context.to_lazy_hash, &block)
+ end
+
+ def self.push(args)
+ application_context = new(**args)
+ Labkit::Context.push(application_context.to_lazy_hash)
+ end
+
+ def initialize(user: nil, project: nil, namespace: nil)
+ @user, @project, @namespace = user, project, namespace
+ end
+
+ def to_lazy_hash
+ { user: -> { username },
+ project: -> { project_path },
+ root_namespace: -> { root_namespace_path } }
+ end
+
+ private
+
+ lazy_attr_reader :user, type: User
+ lazy_attr_reader :project, type: Project
+ lazy_attr_reader :namespace, type: Namespace
+
+ 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
+ end
+end