Commit eedcd048 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'sy-move-alert-embeds-to-core' into 'master'

Move gitlab alert-based metric embeds to core

See merge request gitlab-org/gitlab!39509
parents 1986aa02 8f283c66
---
title: Move gitlab-managed alerts embeds to core as documented
merge_request: 39509
author:
type: fixed
# frozen_string_literal: true
module EE
module Banzai
module Filter
module InlineMetricsRedactorFilter
extend ::Gitlab::Utils::Override
ROUTE = ::Banzai::Filter::InlineMetricsRedactorFilter::Route
override :permissions_by_route
def permissions_by_route
super.concat([
ROUTE.new(::Gitlab::Metrics::Dashboard::Url.alert_regex, :read_prometheus_alerts)
])
end
end
end
end
end
# frozen_string_literal: true
# Manages url matching for metrics dashboards.
module EE
module Gitlab
module Metrics
module Dashboard
module Url
# 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
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Banzai::Filter::InlineMetricsRedactorFilter do
include FilterSpecHelper
let_it_be(:project) { create(:project) }
let(:input) { %(<div class="js-render-metrics" data-dashboard-url="#{url}"></div>) }
let(:doc) { filter(input) }
context 'for an alert embed' do
let_it_be(:alert) { create(:prometheus_alert, project: project) }
let(:url) do
urls.metrics_dashboard_project_prometheus_alert_url(
project,
alert.prometheus_metric_id,
environment_id: alert.environment_id,
embedded: true
)
end
before do
stub_licensed_features(prometheus_alerts: true)
end
it_behaves_like 'redacts the embed placeholder'
it_behaves_like 'retains the embed placeholder when applicable'
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Metrics::Dashboard::Url do
describe '#alert_regex' do
let(:url) do
Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_prometheus_alert_url(
'foo',
'bar',
'1',
start: '2020-02-10T12:59:49.938Z',
end: '2020-02-10T20:59:49.938Z',
anchor: "anchor"
)
end
let(:expected_params) do
{
'url' => url,
'namespace' => 'foo',
'project' => 'bar',
'alert' => '1',
'query' => "?end=2020-02-10T20%3A59%3A49.938Z&start=2020-02-10T12%3A59%3A49.938Z",
'anchor' => '#anchor'
}
end
subject { described_class.alert_regex }
it_behaves_like 'regex which matches url when expected'
end
end
......@@ -81,6 +81,10 @@ module Banzai
Route.new(
::Gitlab::Metrics::Dashboard::Url.clusters_regex,
:read_cluster
),
Route.new(
::Gitlab::Metrics::Dashboard::Url.alert_regex,
:read_prometheus_alerts
)
]
end
......@@ -147,5 +151,3 @@ module Banzai
end
end
end
Banzai::Filter::InlineMetricsRedactorFilter.prepend_if_ee('EE::Banzai::Filter::InlineMetricsRedactorFilter')
......@@ -43,6 +43,39 @@ module Gitlab
end
end
# 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
# Parses query params out from full url string into hash.
#
# Ex) 'https://<root>/<project>/<environment>/metrics?title=Title&group=Group'
......@@ -60,22 +93,6 @@ module Gitlab
Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_environment_url(*args)
end
# 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
private
def regex_for_project_metrics(path_suffix_pattern)
......@@ -107,5 +124,3 @@ module Gitlab
end
end
end
Gitlab::Metrics::Dashboard::Url.extend_if_ee('::EE::Gitlab::Metrics::Dashboard::Url')
......@@ -74,5 +74,20 @@ RSpec.describe Banzai::Filter::InlineMetricsRedactorFilter do
end
end
end
context 'for an alert embed' do
let_it_be(:alert) { create(:prometheus_alert, project: project) }
let(:url) do
urls.metrics_dashboard_project_prometheus_alert_url(
project,
alert.prometheus_metric_id,
environment_id: alert.environment_id,
embedded: true
)
end
it_behaves_like 'redacts the embed placeholder'
it_behaves_like 'retains the embed placeholder when applicable'
end
end
end
......@@ -102,6 +102,34 @@ RSpec.describe Gitlab::Metrics::Dashboard::Url do
it_behaves_like 'regex which matches url when expected'
end
describe '#alert_regex' do
let(:url) do
Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_prometheus_alert_url(
'foo',
'bar',
'1',
start: '2020-02-10T12:59:49.938Z',
end: '2020-02-10T20:59:49.938Z',
anchor: "anchor"
)
end
let(:expected_params) do
{
'url' => url,
'namespace' => 'foo',
'project' => 'bar',
'alert' => '1',
'query' => "?end=2020-02-10T20%3A59%3A49.938Z&start=2020-02-10T12%3A59%3A49.938Z",
'anchor' => '#anchor'
}
end
subject { described_class.alert_regex }
it_behaves_like 'regex which matches url when expected'
end
describe '#build_dashboard_url' do
it 'builds the url for the dashboard endpoint' do
url = described_class.build_dashboard_url('foo', 'bar', 1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment