summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/dashboard/stages/variable_endpoint_inserter.rb
blob: 20e7fe477e5a2d2114b9868233ff2b230b6459a3 (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
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Dashboard
      module Stages
        class VariableEndpointInserter < BaseStage
          VARIABLE_TYPE_METRIC_LABEL_VALUES = 'metric_label_values'

          def transform!
            raise Errors::DashboardProcessingError.new(_('Environment is required for Stages::VariableEndpointInserter')) unless params[:environment]

            for_variables do |variable_name, variable|
              if variable.is_a?(Hash) && variable[:type] == VARIABLE_TYPE_METRIC_LABEL_VALUES
                variable[:options][:prometheus_endpoint_path] = endpoint_for_variable(variable.dig(:options, :series_selector))
              end
            end
          end

          private

          def endpoint_for_variable(series_selector)
            Gitlab::Routing.url_helpers.prometheus_api_project_environment_path(
              project,
              params[:environment],
              proxy_path: ::Prometheus::ProxyService::PROMETHEUS_SERIES_API,
              match: Array(series_selector)
            )
          end
        end
      end
    end
  end
end