2019-07-10 07:27:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Manages url matching for metrics dashboards.
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Dashboard
|
|
|
|
class Url
|
|
|
|
class << self
|
2020-02-12 07:09:01 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-02-13 19:09:07 -05:00
|
|
|
|
2020-02-15 16:08:49 -05:00
|
|
|
QUERY_PATTERN = '(?<query>\?[a-zA-Z0-9%.()+_=-]+(&[a-zA-Z0-9%.()+_=-]+)*)?'
|
|
|
|
ANCHOR_PATTERN = '(?<anchor>\#[a-z0-9_-]+)?'
|
2020-08-31 11:10:41 -04:00
|
|
|
DASH_PATTERN = '(?:/-)'
|
2020-02-15 16:08:49 -05:00
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
# Matches urls for a metrics dashboard.
|
|
|
|
# This regex needs to match the old metrics URL, the new metrics URL,
|
|
|
|
# and the dashboard URL (inline_metrics_redactor_filter.rb
|
|
|
|
# uses this regex to match against the dashboard URL.)
|
2019-07-10 07:27:25 -04:00
|
|
|
#
|
2020-08-31 11:10:41 -04:00
|
|
|
# EX - Old URL: https://<host>/<namespace>/<project>/environments/<env_id>/metrics
|
|
|
|
# OR
|
|
|
|
# New URL: https://<host>/<namespace>/<project>/-/metrics?environment=<env_id>
|
|
|
|
# OR
|
|
|
|
# dashboard URL: https://<host>/<namespace>/<project>/environments/<env_id>/metrics_dashboard
|
2020-02-12 07:09:01 -05:00
|
|
|
def metrics_regex
|
|
|
|
strong_memoize(:metrics_regex) do
|
|
|
|
regex_for_project_metrics(
|
|
|
|
%r{
|
2020-08-31 11:10:41 -04:00
|
|
|
( #{environment_metrics_regex} ) | ( #{non_environment_metrics_regex} )
|
2020-02-12 07:09:01 -05:00
|
|
|
}x
|
2019-10-31 11:06:41 -04:00
|
|
|
)
|
2020-02-12 07:09:01 -05:00
|
|
|
end
|
2019-10-31 11:06:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Matches dashboard urls for a Grafana embed.
|
|
|
|
#
|
|
|
|
# EX - https://<host>/<namespace>/<project>/grafana/metrics_dashboard
|
|
|
|
def grafana_regex
|
2020-02-12 07:09:01 -05:00
|
|
|
strong_memoize(:grafana_regex) do
|
|
|
|
regex_for_project_metrics(
|
|
|
|
%r{
|
2020-08-31 11:10:41 -04:00
|
|
|
#{DASH_PATTERN}?
|
2020-02-12 07:09:01 -05:00
|
|
|
/grafana
|
|
|
|
/metrics_dashboard
|
|
|
|
}x
|
2019-07-10 07:27:25 -04:00
|
|
|
)
|
2020-02-12 07:09:01 -05:00
|
|
|
end
|
2019-07-10 07:27:25 -04:00
|
|
|
end
|
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
# Matches dashboard urls for a metric chart embed
|
2020-08-31 11:10:41 -04:00
|
|
|
# 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.
|
2020-08-18 08:10:16 -04:00
|
|
|
#
|
|
|
|
# EX - https://<host>/<namespace>/<project>/-/clusters/<cluster_id>/?group=Cluster%20Health&title=Memory%20Usage&y_label=Memory%20(GiB)
|
2020-08-31 11:10:41 -04:00
|
|
|
# dashboard URL - https://<host>/<namespace>/<project>/-/clusters/<cluster_id>/metrics_dashboard?group=Cluster%20Health&title=Memory%20Usage&y_label=Memory%20(GiB)
|
2020-08-18 08:10:16 -04:00
|
|
|
def clusters_regex
|
|
|
|
strong_memoize(:clusters_regex) do
|
|
|
|
regex_for_project_metrics(
|
|
|
|
%r{
|
2020-08-31 11:10:41 -04:00
|
|
|
#{DASH_PATTERN}?
|
2020-08-18 08:10:16 -04:00
|
|
|
/clusters
|
|
|
|
/(?<cluster_id>\d+)
|
|
|
|
/?
|
2020-08-31 11:10:41 -04:00
|
|
|
( (/metrics) | ( /metrics_dashboard\.json ) )?
|
2020-08-18 08:10:16 -04:00
|
|
|
}x
|
|
|
|
)
|
|
|
|
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{
|
2020-08-31 11:10:41 -04:00
|
|
|
#{DASH_PATTERN}?
|
2020-08-18 08:10:16 -04:00
|
|
|
/prometheus
|
|
|
|
/alerts
|
|
|
|
/(?<alert>\d+)
|
2020-08-31 11:10:41 -04:00
|
|
|
/metrics_dashboard(\.json)?
|
2020-08-18 08:10:16 -04:00
|
|
|
}x
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-15 17:38:29 -04:00
|
|
|
# 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
|
|
|
|
|
2019-07-10 07:27:25 -04:00
|
|
|
# Builds a metrics dashboard url based on the passed in arguments
|
|
|
|
def build_dashboard_url(*args)
|
|
|
|
Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_environment_url(*args)
|
|
|
|
end
|
2019-10-31 11:06:41 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
def environment_metrics_regex
|
|
|
|
%r{
|
|
|
|
#{DASH_PATTERN}?
|
|
|
|
/environments
|
|
|
|
/(?<environment>\d+)
|
|
|
|
/(metrics_dashboard|metrics)
|
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
|
|
|
def non_environment_metrics_regex
|
|
|
|
%r{
|
|
|
|
#{DASH_PATTERN}
|
|
|
|
/metrics
|
|
|
|
(?= # Lookahead to ensure there is an environment query param
|
|
|
|
\?
|
|
|
|
.*
|
|
|
|
environment=(?<environment>\d+)
|
|
|
|
.*
|
|
|
|
)
|
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
2020-02-12 07:09:01 -05:00
|
|
|
def regex_for_project_metrics(path_suffix_pattern)
|
|
|
|
%r{
|
2020-08-31 11:10:41 -04:00
|
|
|
^(?<url>
|
2020-02-12 07:09:01 -05:00
|
|
|
#{gitlab_host_pattern}
|
|
|
|
#{project_path_pattern}
|
|
|
|
#{path_suffix_pattern}
|
2020-02-15 16:08:49 -05:00
|
|
|
#{QUERY_PATTERN}
|
|
|
|
#{ANCHOR_PATTERN}
|
2020-08-31 11:10:41 -04:00
|
|
|
)$
|
2020-02-12 07:09:01 -05:00
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_host_pattern
|
2020-07-31 20:09:45 -04:00
|
|
|
Regexp.escape(gitlab_domain)
|
2019-10-31 11:06:41 -04:00
|
|
|
end
|
|
|
|
|
2020-02-12 07:09:01 -05:00
|
|
|
def project_path_pattern
|
2019-10-31 11:06:41 -04:00
|
|
|
"\/#{Project.reference_pattern}"
|
|
|
|
end
|
2020-07-31 20:09:45 -04:00
|
|
|
|
|
|
|
def gitlab_domain
|
|
|
|
Gitlab.config.gitlab.url
|
|
|
|
end
|
2019-07-10 07:27:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|