Sha256: 28cfc27efa63ae2c9beef488776c296d3288bd14728f0d4699c6ff9f12726e81

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

# typed: false
require 'ddtrace/contrib/action_view/ext'

module Datadog
  module Contrib
    module ActionView
      module Instrumentation
        # Legacy instrumentation for partial rendering for Rails < 4
        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.logger.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.resource = template_name
              active_datadog_span.set_tag(
                Ext::TAG_TEMPLATE_NAME,
                template_name
              )

              # Measure service stats
              Contrib::Analytics.set_measured(active_datadog_span)
            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 < 4 partial rendering
          # ActiveSupport events are used instead for Rails >= 4
          module RailsLessThan4
            include PartialRenderer

            def datadog_template(*args)
              @template
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddtrace-0.54.2 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb
ddtrace-0.54.1 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb
ddtrace-0.54.0 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb
ddtrace-0.53.0 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb
ddtrace-0.52.0 lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb