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

logger.rb « mergeability « merge_requests « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b45d231e03d688a63626b5475e04b874a1ae990 (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
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true

module MergeRequests
  module Mergeability
    class Logger
      include Gitlab::Utils::StrongMemoize

      def initialize(merge_request:, destination: Gitlab::AppJsonLogger)
        @destination = destination
        @merge_request = merge_request
      end

      def commit
        return unless enabled?

        commit_logs
      end

      def instrument(mergeability_name:)
        raise ArgumentError, 'block not given' unless block_given?

        return yield unless enabled?

        op_start_db_counters = current_db_counter_payload
        op_started_at = current_monotonic_time

        result = yield

        observe("mergeability.#{mergeability_name}.duration_s", current_monotonic_time - op_started_at)

        observe_sql_counters(mergeability_name, op_start_db_counters, current_db_counter_payload)

        result
      end

      private

      attr_reader :destination, :merge_request

      def observe(name, value)
        return unless enabled?

        observations[name.to_s].push(value)
      end

      def commit_logs
        attributes = Gitlab::ApplicationContext.current.merge({
                                                                mergeability_project_id: merge_request.project.id
                                                              })

        attributes[:mergeability_merge_request_id] = merge_request.id
        attributes.merge!(observations_hash)
        attributes.compact!
        attributes.stringify_keys!

        destination.info(attributes)
      end

      def observations_hash
        transformed = observations.transform_values do |values|
          next if values.empty?

          {
            'values' => values
          }
        end.compact

        transformed.each_with_object({}) do |key, hash|
          key[1].each { |k, v| hash["#{key[0]}.#{k}"] = v }
        end
      end

      def observations
        strong_memoize(:observations) do
          Hash.new { |hash, key| hash[key] = [] }
        end
      end

      def observe_sql_counters(name, start_db_counters, end_db_counters)
        end_db_counters.each do |key, value|
          result = value - start_db_counters.fetch(key, 0)
          next if result == 0

          observe("mergeability.#{name}.#{key}", result)
        end
      end

      def current_db_counter_payload
        ::Gitlab::Metrics::Subscribers::ActiveRecord.db_counter_payload
      end

      def enabled?
        strong_memoize(:enabled) do
          ::Feature.enabled?(:mergeability_checks_logger, merge_request.project)
        end
      end

      def current_monotonic_time
        ::Gitlab::Metrics::System.monotonic_time
      end
    end
  end
end