summaryrefslogtreecommitdiff
path: root/lib/gitlab/usage/service_ping_report.rb
blob: 794f33730437945cdfb749a0406f3d60d5aeecba (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
# frozen_string_literal: true

module Gitlab
  module Usage
    class ServicePingReport
      class << self
        def for(output:, cached: false)
          case output.to_sym
          when :all_metrics_values
            with_instrumentation_classes(all_metrics_values(cached), :with_value)
          when :metrics_queries
            with_instrumentation_classes(metrics_queries, :with_instrumentation)
          when :non_sql_metrics_values
            with_instrumentation_classes(non_sql_metrics_values, :with_instrumentation)
          end
        end

        private

        def with_instrumentation_classes(old_payload, output_method)
          if Feature.enabled?(:merge_service_ping_instrumented_metrics, default_enabled: :yaml)

            instrumented_metrics_key_paths = Gitlab::Usage::ServicePing::PayloadKeysProcessor.new(old_payload).missing_instrumented_metrics_key_paths

            instrumented_payload = Gitlab::Usage::ServicePing::InstrumentedPayload.new(instrumented_metrics_key_paths, output_method).build

            old_payload.deep_merge(instrumented_payload)
          else
            old_payload
          end
        end

        def all_metrics_values(cached)
          Rails.cache.fetch('usage_data', force: !cached, expires_in: 2.weeks) do
            Gitlab::UsageData.data
          end
        end

        def metrics_queries
          Gitlab::UsageDataQueries.data
        end

        def non_sql_metrics_values
          Gitlab::UsageDataNonSqlMetrics.data
        end
      end
    end
  end
end