2019-09-30 21:07:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Manages url matching for metrics dashboards.
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Dashboard
|
|
|
|
class Url
|
|
|
|
class << self
|
2020-03-13 15:44:24 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
QUERY_PATTERN = '(?<query>\?[a-zA-Z0-9%.()+_=-]+(&[a-zA-Z0-9%.()+_=-]+)*)?'
|
|
|
|
ANCHOR_PATTERN = '(?<anchor>\#[a-z0-9_-]+)?'
|
|
|
|
OPTIONAL_DASH_PATTERN = '(?:/-)?'
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
# Matches urls for a metrics dashboard. This could be
|
|
|
|
# either the /metrics endpoint or the /metrics_dashboard
|
|
|
|
# endpoint.
|
|
|
|
#
|
|
|
|
# EX - https://<host>/<namespace>/<project>/environments/<env_id>/metrics
|
2020-03-13 15:44:24 +05:30
|
|
|
def metrics_regex
|
|
|
|
strong_memoize(:metrics_regex) do
|
|
|
|
regex_for_project_metrics(
|
|
|
|
%r{
|
|
|
|
/environments
|
|
|
|
/(?<environment>\d+)
|
2020-05-24 23:13:21 +05:30
|
|
|
/(metrics_dashboard|metrics)
|
2020-03-13 15:44:24 +05:30
|
|
|
}x
|
2019-12-26 22:10:19 +05:30
|
|
|
)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Matches dashboard urls for a Grafana embed.
|
|
|
|
#
|
|
|
|
# EX - https://<host>/<namespace>/<project>/grafana/metrics_dashboard
|
|
|
|
def grafana_regex
|
2020-03-13 15:44:24 +05:30
|
|
|
strong_memoize(:grafana_regex) do
|
|
|
|
regex_for_project_metrics(
|
|
|
|
%r{
|
|
|
|
/grafana
|
|
|
|
/metrics_dashboard
|
|
|
|
}x
|
2019-09-30 21:07:59 +05:30
|
|
|
)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
# Matches dashboard urls for a metric chart embed
|
|
|
|
# for cluster metrics
|
|
|
|
#
|
|
|
|
# EX - https://<host>/<namespace>/<project>/-/clusters/<cluster_id>/?group=Cluster%20Health&title=Memory%20Usage&y_label=Memory%20(GiB)
|
|
|
|
def clusters_regex
|
|
|
|
strong_memoize(:clusters_regex) do
|
|
|
|
regex_for_project_metrics(
|
|
|
|
%r{
|
|
|
|
/clusters
|
|
|
|
/(?<cluster_id>\d+)
|
|
|
|
/?
|
|
|
|
}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{
|
|
|
|
/prometheus
|
|
|
|
/alerts
|
|
|
|
/(?<alert>\d+)
|
|
|
|
/metrics_dashboard
|
|
|
|
}x
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
# 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-09-30 21:07:59 +05:30
|
|
|
# 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-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def regex_for_project_metrics(path_suffix_pattern)
|
|
|
|
%r{
|
|
|
|
(?<url>
|
|
|
|
#{gitlab_host_pattern}
|
|
|
|
#{project_path_pattern}
|
|
|
|
#{OPTIONAL_DASH_PATTERN}
|
|
|
|
#{path_suffix_pattern}
|
|
|
|
#{QUERY_PATTERN}
|
|
|
|
#{ANCHOR_PATTERN}
|
|
|
|
)
|
|
|
|
}x
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def gitlab_host_pattern
|
2020-10-24 23:57:45 +05:30
|
|
|
Regexp.escape(gitlab_domain)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def project_path_pattern
|
|
|
|
"\/#{Project.reference_pattern}"
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
def gitlab_domain
|
|
|
|
Gitlab.config.gitlab.url
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|