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

url.rb « dashboard « metrics « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7b901861ef77afb85fd2ffc2b146519e042820a (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: true

# Manages url matching for metrics dashboards.
module Gitlab
  module Metrics
    module Dashboard
      class Url
        class << self
          include Gitlab::Utils::StrongMemoize

          QUERY_PATTERN = '(?<query>\?[a-zA-Z0-9%.()+_=-]+(&[a-zA-Z0-9%.()+_=-]+)*)?'
          ANCHOR_PATTERN = '(?<anchor>\#[a-z0-9_-]+)?'
          DASH_PATTERN = '(?:/-)'

          # Matches dashboard urls for a Grafana embed.
          #
          # EX - https://<host>/<namespace>/<project>/grafana/metrics_dashboard
          def grafana_regex
            strong_memoize(:grafana_regex) do
              regex_for_project_metrics(
                %r{
                  #{DASH_PATTERN}?
                  /grafana
                  /metrics_dashboard
                }xo
              )
            end
          end

          # Matches dashboard urls for a metric chart embed
          # for cluster metrics.
          # This regex needs to match the dashboard URL as well, not just the trigger URL.
          # The inline_metrics_redactor_filter.rb uses this regex to match against
          # the dashboard URL.
          #
          # EX - https://<host>/<namespace>/<project>/-/clusters/<cluster_id>/?group=Cluster%20Health&title=Memory%20Usage&y_label=Memory%20(GiB)
          # dashboard URL - https://<host>/<namespace>/<project>/-/clusters/<cluster_id>/metrics_dashboard?group=Cluster%20Health&title=Memory%20Usage&y_label=Memory%20(GiB)
          def clusters_regex
            strong_memoize(:clusters_regex) do
              regex_for_project_metrics(
                %r{
                  #{DASH_PATTERN}?
                  /clusters
                  /(?<cluster_id>\d+)
                  /?
                  ( (/metrics) | ( /metrics_dashboard\.json ) )?
                }xo
              )
            end
          end

          # Matches dashboard urls for a metric chart embed
          # for a specifc firing GitLab alert
          #
          # EX - https://<host>/<namespace>/<project>/prometheus/alerts/<alert_id>/metrics_dashboard
          def alert_regex
            strong_memoize(:alert_regex) do
              regex_for_project_metrics(
                %r{
                  #{DASH_PATTERN}?
                  /prometheus
                  /alerts
                  /(?<alert>\d+)
                  /metrics_dashboard(\.json)?
                }xo
              )
            end
          end

          # Parses query params out from full url string into hash.
          #
          # Ex) 'https://<root>/<project>/<environment>/metrics?title=Title&group=Group'
          #       --> { title: 'Title', group: 'Group' }
          def parse_query(url)
            query_string = URI.parse(url).query.to_s

            CGI.parse(query_string)
              .transform_values { |value| value.first }
              .symbolize_keys
          end

          private

          def environment_metrics_regex
            %r{
              #{DASH_PATTERN}?
              /environments
              /(?<environment>\d+)
              /(metrics_dashboard|metrics)
            }xo
          end

          def non_environment_metrics_regex
            %r{
              #{DASH_PATTERN}
              /metrics
              (?=                             # Lookahead to ensure there is an environment query param
                \?
                .*
                environment=(?<environment>\d+)
                .*
              )
            }xo
          end

          def regex_for_project_metrics(path_suffix_pattern)
            %r{
              ^(?<url>
                #{gitlab_host_pattern}
                #{project_path_pattern}
                #{path_suffix_pattern}
                #{QUERY_PATTERN}
                #{ANCHOR_PATTERN}
              )$
            }x
          end

          def gitlab_host_pattern
            Regexp.escape(gitlab_domain)
          end

          def project_path_pattern
            "\/#{Project.reference_pattern}"
          end

          def gitlab_domain
            Gitlab.config.gitlab.url
          end
        end
      end
    end
  end
end