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

cluster_endpoint_inserter.rb « stages « dashboard « metrics « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c17982d299334b9dc070a6d9b0ef80dd99856b4 (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
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Dashboard
      module Stages
        class ClusterEndpointInserter < BaseStage
          def transform!
            verify_params

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

          private

          def admin_url(metric)
            Gitlab::Routing.url_helpers.prometheus_api_admin_cluster_path(
              params[:cluster],
              proxy_path: query_type(metric),
              query: query_for_metric(metric)
            )
          end

          def endpoint_for_metric(metric)
            case params[:cluster_type]
            when :admin
              admin_url(metric)
            when :group
              error!(_('Group is required when cluster_type is :group')) unless params[:group]
              group_url(metric)
            when :project
              error!(_('Project is required when cluster_type is :project')) unless project
              project_url(metric)
            else
              error!(_('Unrecognized cluster type'))
            end
          end

          def error!(message)
            raise Errors::DashboardProcessingError, message
          end

          def group_url(metric)
            Gitlab::Routing.url_helpers.prometheus_api_group_cluster_path(
              params[:group],
              params[:cluster],
              proxy_path: query_type(metric),
              query: query_for_metric(metric)
            )
          end

          def project_url(metric)
            Gitlab::Routing.url_helpers.prometheus_api_project_cluster_path(
              project,
              params[:cluster],
              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)]

            raise Errors::MissingQueryError, 'Each "metric" must define one of :query or :query_range' unless query

            query
          end

          def verify_params
            raise Errors::DashboardProcessingError, _('Cluster is required for Stages::ClusterEndpointInserter') unless params[:cluster]
            raise Errors::DashboardProcessingError, _('Cluster type must be specificed for Stages::ClusterEndpointInserter') unless params[:cluster_type]
          end
        end
      end
    end
  end
end