endpoint_inserter.rb 1.0 KB
Newer Older
1 2 3 4 5 6 7
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Dashboard
      module Stages
        class EndpointInserter < BaseStage
8
          MissingQueryError = Class.new(DashboardProcessingError)
9 10 11 12 13 14 15 16 17 18

          def transform!
            for_metrics do |metric|
              metric[:prometheus_endpoint_path] = endpoint_for_metric(metric)
            end
          end

          private

          def endpoint_for_metric(metric)
S
syasonik 已提交
19
            Gitlab::Routing.url_helpers.prometheus_api_project_environment_path(
20 21 22 23 24 25 26 27 28 29 30 31 32 33
              project,
              environment,
              proxy_path: query_type(metric),
              query: query_for_metric(metric)
            )
          end

          def query_type(metric)
            metric[:query] ? :query : :query_range
          end

          def query_for_metric(metric)
            query = metric[query_type(metric)]

34
            raise MissingQueryError.new('Each "metric" must define one of :query or :query_range') unless query
35 36 37 38 39 40 41 42

            query
          end
        end
      end
    end
  end
end