summaryrefslogtreecommitdiff
path: root/lib/gitlab/usage/service_ping/instrumented_payload.rb
blob: e04e2e589b22067995673ca9282edab11ec7077a (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
# frozen_string_literal: true

# Service Ping payload build using the instrumentation classes
# for given metrics key_paths and output method
module Gitlab
  module Usage
    module ServicePing
      class InstrumentedPayload
        attr_reader :metrics_key_paths
        attr_reader :output_method

        def initialize(metrics_key_paths, output_method)
          @metrics_key_paths = metrics_key_paths
          @output_method = output_method
        end

        def build
          metrics_key_paths.map do |key_path|
            compute_instrumental_value(key_path, output_method)
          end.reduce({}, :deep_merge)
        end

        private

        # Not all metrics defintions have instrumentation classes
        # The value can be computed only for those that have it
        def instrumented_metrics_defintions
          Gitlab::Usage::MetricDefinition.with_instrumentation_class
        end

        def compute_instrumental_value(key_path, output_method)
          definition = instrumented_metrics_defintions.find { |df| df.key_path == key_path }

          return {} unless definition.present?

          Gitlab::Usage::Metric.new(definition).method(output_method).call
        end
      end
    end
  end
end