summaryrefslogtreecommitdiff
path: root/lib/gitlab/application_context.rb
blob: b4bbb309c36fb9acf3492fac689eb3ae423ace21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

module Gitlab
  # A GitLab-rails specific accessor for `Labkit::Logging::ApplicationContext`
  class ApplicationContext
    include Gitlab::Utils::LazyAttributes

    Attribute = Struct.new(:name, :type)

    APPLICATION_ATTRIBUTES = [
      Attribute.new(:project, Project),
      Attribute.new(:namespace, Namespace),
      Attribute.new(:user, User),
      Attribute.new(:caller_id, String),
      Attribute.new(:related_class, String),
      Attribute.new(:feature_category, String)
    ].freeze

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

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

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

    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

    def to_lazy_hash
      {}.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?
        hash[:caller_id] = caller_id if set_values.include?(:caller_id)
        hash[:related_class] = related_class if set_values.include?(:related_class)
        hash[:feature_category] = feature_category if set_values.include?(:feature_category)
      end
    end

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

    private

    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

    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

    def include_namespace?
      set_values.include?(:namespace) || set_values.include?(:project)
    end
  end
end

Gitlab::ApplicationContext.prepend_if_ee('EE::Gitlab::ApplicationContext')