Module: Datadog::Contrib::Rails::ActionView

Defined in:
lib/ddtrace/contrib/rails/action_view.rb

Overview

Code used to create and handle ‘rails.render_template’ and ‘rails.render_partial’ spans.

Class Method Summary collapse

Class Method Details

.get_key(f) ⇒ Object



33
34
35
# File 'lib/ddtrace/contrib/rails/action_view.rb', line 33

def self.get_key(f)
  'datadog_actionview_' + f
end

.instrumentObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ddtrace/contrib/rails/action_view.rb', line 8

def self.instrument
  # patch Rails core components
  Datadog::RailsRendererPatcher.patch_renderer()

  # subscribe when the template rendering starts
  ::ActiveSupport::Notifications.subscribe('start_render_template.action_view') do |*args|
    start_render_template(*args)
  end

  # subscribe when the partial rendering starts
  ::ActiveSupport::Notifications.subscribe('start_render_partial.action_view') do |*args|
    start_render_partial(*args)
  end

  # subscribe when the template rendering has been processed
  ::ActiveSupport::Notifications.subscribe('render_template.action_view') do |*args|
    render_template(*args)
  end

  # subscribe when the partial rendering has been processed
  ::ActiveSupport::Notifications.subscribe('render_partial.action_view') do |*args|
    render_partial(*args)
  end
end

.render_partial(_name, start, finish, _id, payload) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ddtrace/contrib/rails/action_view.rb', line 86

def self.render_partial(_name, start, finish, _id, payload)
  key = get_key('render_partial')
  return unless Thread.current[key]
  Thread.current[key] = false

  # finish the tracing and update the execution time
  tracer = ::Rails.configuration.datadog_trace.fetch(:tracer)
  span = tracer.active_span()
  return unless span

  begin
    template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(payload.fetch(:identifier))
    span.set_tag('rails.template_name', template_name)
    span.set_error(payload[:exception]) if payload[:exception]
  ensure
    span.start_time = start
    span.finish(finish)
  end
rescue StandardError => e
  Datadog::Tracer.log.error(e.message)
end

.render_template(_name, start, finish, _id, payload) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ddtrace/contrib/rails/action_view.rb', line 63

def self.render_template(_name, start, finish, _id, payload)
  key = get_key('render_template')
  return unless Thread.current[key]
  Thread.current[key] = false

  # finish the tracing and update the execution time
  tracer = ::Rails.configuration.datadog_trace.fetch(:tracer)
  span = tracer.active_span()
  return unless span

  begin
    template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(payload.fetch(:identifier))
    span.set_tag('rails.template_name', template_name)
    span.set_tag('rails.layout', payload.fetch(:layout))
    span.set_error(payload[:exception]) if payload[:exception]
  ensure
    span.start_time = start
    span.finish(finish)
  end
rescue StandardError => e
  Datadog::Tracer.log.error(e.message)
end

.start_render_partialObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ddtrace/contrib/rails/action_view.rb', line 50

def self.start_render_partial(*)
  key = get_key('render_partial')
  return if Thread.current[key]

  tracer = ::Rails.configuration.datadog_trace.fetch(:tracer)
  type = Datadog::Ext::HTTP::TEMPLATE
  tracer.trace('rails.render_partial', span_type: type)

  Thread.current[key] = true
rescue StandardError => e
  Datadog::Tracer.log.error(e.message)
end

.start_render_templateObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ddtrace/contrib/rails/action_view.rb', line 37

def self.start_render_template(*)
  key = get_key('render_template')
  return if Thread.current[key]

  tracer = ::Rails.configuration.datadog_trace.fetch(:tracer)
  type = Datadog::Ext::HTTP::TEMPLATE
  tracer.trace('rails.render_template', span_type: type)

  Thread.current[key] = true
rescue StandardError => e
  Datadog::Tracer.log.error(e.message)
end