summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/inline_observability_filter.rb
blob: a47b57d94dd8655047593460b2b5548444ea4d3a (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
# frozen_string_literal: true

module Banzai
  module Filter
    class InlineObservabilityFilter < ::Banzai::Filter::InlineEmbedsFilter
      include Gitlab::Utils::StrongMemoize

      def call
        return doc unless Gitlab::Observability.enabled?(group)

        super
      end

      # Placeholder element for the frontend to use as an
      # injection point for observability.
      def create_element(url)
        doc.document.create_element(
          'div',
          class: 'js-render-observability',
          'data-frame-url': url
        )
      end

      # Search params for selecting observability links.
      def xpath_search
        "descendant-or-self::a[starts-with(@href, '#{gitlab_domain}/groups/') and contains(@href,'/-/observability/')]"
      end

      # Creates a new element based on the parameters
      # obtained from the target link
      def element_to_embed(node)
        url = node['href']

        embeddable_url = extract_embeddable_url(url)
        create_element(embeddable_url) if embeddable_url
      end

      private

      def extract_embeddable_url(url)
        strong_memoize_with(:embeddable_url, url) do
          Gitlab::Observability.embeddable_url(url)
        end
      end

      def group
        context[:group] || context[:project]&.group
      end
    end
  end
end