diff options
Diffstat (limited to 'lib/banzai/filter/inline_alert_metrics_filter.rb')
-rw-r--r-- | lib/banzai/filter/inline_alert_metrics_filter.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/banzai/filter/inline_alert_metrics_filter.rb b/lib/banzai/filter/inline_alert_metrics_filter.rb new file mode 100644 index 00000000000..a6140d1ac81 --- /dev/null +++ b/lib/banzai/filter/inline_alert_metrics_filter.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Banzai + module Filter + # HTML filter that inserts a placeholder element for each + # reference to an alert dashboard. + class InlineAlertMetricsFilter < ::Banzai::Filter::InlineEmbedsFilter + include ::Gitlab::Routing + # Search params for selecting alert metrics links. A few + # simple checks is enough to boost performance without + # the cost of doing a full regex match. + def xpath_search + "descendant-or-self::a[contains(@href,'metrics_dashboard') and \ + contains(@href,'prometheus/alerts') and \ + starts-with(@href, '#{gitlab_domain}')]" + end + + # Regular expression matching alert dashboard urls + def link_pattern + ::Gitlab::Metrics::Dashboard::Url.alert_regex + end + + private + + # Endpoint FE should hit to collect the appropriate + # chart information + def metrics_dashboard_url(params) + metrics_dashboard_namespace_project_prometheus_alert_url( + params['namespace'], + params['project'], + params['alert'], + embedded: true, + format: :json, + **query_params(params['url']) + ) + end + + # Parses query params out from full url string into hash. + # + # Ex) 'https://<root>/<project>/metrics_dashboard?title=Title&group=Group' + # --> { title: 'Title', group: 'Group' } + def query_params(url) + ::Gitlab::Metrics::Dashboard::Url.parse_query(url) + end + end + end +end |