Module: Datadog::RailsRendererPatcher

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

Class Method Details

.patch_partial_renderer(klass) ⇒ Object



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

def patch_partial_renderer(klass)
  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)
      render_without_datadog(*args)
    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_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_context[: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

    # 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

.patch_rendererObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 9

def patch_renderer
  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

.patch_template_renderer(klass) ⇒ Object



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
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 22

def patch_template_renderer(klass)
  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)
    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