Module: Datadog::Contrib::ActionView::Instrumentation::TemplateRenderer::Rails30

Defined in:
lib/ddtrace/contrib/action_view/instrumentation/template_renderer.rb

Overview

Rails < 3.1 template rendering

Class Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object

rubocop:disable Metrics/MethodLength



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ddtrace/contrib/action_view/instrumentation/template_renderer.rb', line 12

def self.prepended(base)
  # rubocop:disable Metrics/BlockLength
  base.class_eval do
    def render_with_datadog(*args, &block)
      # NOTE: This check exists purely for Rails 3.0 compatibility.
      #       The 'if' part can be removed when support for Rails 3.0 is removed.
      if active_datadog_span
        render_without_datadog(*args, &block)
      else
        datadog_tracer.trace(
          Ext::SPAN_RENDER_TEMPLATE,
          span_type: Datadog::Ext::HTTP::TEMPLATE
        ) do |span|
          with_datadog_span(span) { render_without_datadog(*args, &block) }
        end
      end
    end

    def render_template_with_datadog(*args)
      begin
        template = args[0]
        layout_name = args[1]

        # update the tracing context with computed values before the rendering
        template_name = template.try('identifier')
        template_name = Utils.normalize_template_name(template_name)

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

        if layout_name
          active_datadog_span.set_tag(
            Ext::TAG_LAYOUT,
            layout_name
          )
        end
      rescue StandardError => e
        Datadog::Tracer.log.debug(e.message)
      end

      # execute the original function anyway
      render_template_without_datadog(*args)
    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

    # method aliasing to patch the class
    alias_method :render_without_datadog, :render
    alias_method :render, :render_with_datadog

    alias_method :render_template_without_datadog, :_render_template
    alias_method :_render_template, :render_template_with_datadog
  end
end