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

module Banzai
  module Filter
    class InlineObservabilityFilter < ::Banzai::Filter::InlineEmbedsFilter
      def call
        return doc unless can_view_observability?

        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::Observability.observability_url}')]"
      end

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

        create_element(url)
      end

      private

      def can_view_observability?
        Feature.enabled?(:observability_group_tab, group)
      end

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