Sha256: 8d9375e418e9369f7cf8fff106235cc7a10c526b50684c7b758b5ffd9f3fd035

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

require 'ddtrace/contrib/action_view/ext'

module Datadog
  module Contrib
    module ActionView
      module Instrumentation
        # Instrumentation for partial rendering
        module PartialRenderer
          def render(*args, &block)
            datadog_tracer.trace(
              Ext::SPAN_RENDER_PARTIAL,
              span_type: Datadog::Ext::HTTP::TEMPLATE
            ) do |span|
              with_datadog_span(span) { super(*args) }
            end
          end

          def render_partial(*args)
            begin
              template = datadog_template(*args)

              datadog_render_partial(template)
            rescue StandardError => e
              Datadog::Tracer.log.debug(e.message)
            end

            # execute the original function anyway
            super(*args)
          end

          def datadog_render_partial(template)
            template_name = Utils.normalize_template_name(template.try('identifier'))

            if template_name
              active_datadog_span.set_tag(
                Ext::TAG_TEMPLATE_NAME,
                template_name
              )
            end
          end

          private

          attr_accessor :active_datadog_span

          def datadog_tracer
            Datadog.configuration[:action_view][:tracer]
          end

          def with_datadog_span(span)
            self.active_datadog_span = span
            yield
          ensure
            self.active_datadog_span = nil
          end

          # Rails < 6 partial rendering
          module RailsLessThan6
            include PartialRenderer

            def datadog_template(*args)
              @template
            end
          end

          # Rails >= 6 partial rendering
          module Rails6Plus
            include PartialRenderer

            def datadog_template(*args)
              args[1]
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
ls-trace-0.1.2 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb
ddtrace-0.29.1 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb
ls-trace-0.1.1 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb
ddtrace-0.29.0 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb
ddtrace-0.28.0 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb