Module: Datadog::RailsRendererPatcher

Includes:
Patcher
Defined in:
lib/ddtrace/contrib/rails/core_extensions.rb

Overview

RailsRendererPatcher contains function to patch Rails rendering libraries. rubocop:disable Lint/RescueException rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ModuleLength

Class Method Summary collapse

Methods included from Patcher

included

Methods included from Patcher::CommonMethods

#do_once, #without_warnings

Class Method Details

.patch_partial_renderer(klass) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 89

def patch_partial_renderer(klass)
  do_once(:patch_partial_renderer) do
    klass.class_eval do
      def render_with_datadog(*args, &block)
        # Create a tracing context and start the rendering span
        tracing_context = {}
        Datadog::Contrib::Rails::ActionView.start_render_partial(tracing_context: tracing_context)
        tracing_contexts[current_span_id] = tracing_context

        render_without_datadog(*args)
      rescue Exception => e
        # attach the exception to the tracing context if any
        tracing_contexts[current_span_id][:exception] = e
        raise e
      ensure
        # Ensure that the template `Span` is finished even during exceptions
        # Remove the existing tracing context (to avoid leaks)
        tracing_contexts.delete(current_span_id)

        # Then finish the span associated with the context
        Datadog::Contrib::Rails::ActionView.finish_render_partial(tracing_context: tracing_context)
      end

      def render_partial_with_datadog(*args)
        begin
          # update the tracing context with computed values before the rendering
          template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(@template.try('identifier'))
          tracing_contexts[current_span_id][:template_name] = template_name
        rescue StandardError => e
          Datadog::Tracer.log.debug(e.message)
        end

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

      # Table of tracing contexts, one per partial/span, keyed by span_id
      # because there will be multiple concurrent contexts, depending on how
      # many partials are nested within one another.
      def tracing_contexts
        @tracing_contexts ||= {}
      end

      def current_span_id
        Datadog.configuration[:rails][:tracer].call_context.current_span.span_id
      end

      # method aliasing to patch the class
      alias_method :render_without_datadog, :render
      alias_method :render, :render_with_datadog
      alias_method :render_partial_without_datadog, :render_partial
      alias_method :render_partial, :render_partial_with_datadog
    end
  end
end

.patch_rendererObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 11

def patch_renderer
  do_once(:patch_renderer) do
    if defined?(::ActionView::TemplateRenderer) && defined?(::ActionView::PartialRenderer)
      patch_template_renderer(::ActionView::TemplateRenderer)
      patch_partial_renderer(::ActionView::PartialRenderer)
    elsif defined?(::ActionView::Rendering) && defined?(::ActionView::Partials::PartialRenderer)
      # NOTE: Rails < 3.1 compatibility: different classes are used
      patch_template_renderer(::ActionView::Rendering)
      patch_partial_renderer(::ActionView::Partials::PartialRenderer)
    else
      Datadog::Tracer.log.debug('Expected Template/Partial classes not found; template rendering disabled')
    end
  end
end

.patch_template_renderer(klass) ⇒ Object



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
83
84
85
86
87
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 26

def patch_template_renderer(klass)
  do_once(:patch_template_renderer) do
    klass.class_eval do
      def render_with_datadog(*args, &block)
        # create a tracing context and start the rendering span
        # NOTE: Rails < 3.1 compatibility: preserve the tracing
        # context when a partial is rendered
        @tracing_context ||= {}
        if @tracing_context.empty?
          Datadog::Contrib::Rails::ActionView.start_render_template(tracing_context: @tracing_context)
        end

        render_without_datadog(*args, &block)
      rescue Exception => e
        # attach the exception to the tracing context if any
        @tracing_context[:exception] = e
        raise e
      ensure
        # ensure that the template `Span` is finished even during exceptions
        Datadog::Contrib::Rails::ActionView.finish_render_template(tracing_context: @tracing_context)
      end

      def render_template_with_datadog(*args)
        begin
          # arguments based on render_template signature (stable since Rails 3.2)
          template = args[0]
          layout_name = args[1]

          # update the tracing context with computed values before the rendering
          template_name = template.try('identifier')
          template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(template_name)
          layout = if layout_name.is_a?(String)
                     # NOTE: Rails < 3.1 compatibility: the second argument is the layout name
                     layout_name
                   else
                     layout_name.try(:[], 'virtual_path')
                   end
          @tracing_context[:template_name] = template_name
          @tracing_context[:layout] = layout
        rescue StandardError => e
          Datadog::Tracer.log.debug(e.message)
        end

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

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

      if klass.private_method_defined?(:render_template) || klass.method_defined?(:render_template)
        alias_method :render_template_without_datadog, :render_template
        alias_method :render_template, :render_template_with_datadog
      else
        # NOTE: Rails < 3.1 compatibility: the method name is different
        alias_method :render_template_without_datadog, :_render_template
        alias_method :_render_template, :render_template_with_datadog
      end
    end
  end
end