Skip to content

Commit

Permalink
Set resource for Rails template spans (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc authored Dec 16, 2019
1 parent 84e7178 commit ba583b5
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
6 changes: 4 additions & 2 deletions lib/ddtrace/contrib/action_view/events/render_partial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def span_name
def process(span, _event, _id, payload)
span.span_type = Datadog::Ext::HTTP::TEMPLATE

template_name = Utils.normalize_template_name(payload[:identifier])
span.set_tag(Ext::TAG_TEMPLATE_NAME, template_name) if template_name
if (template_name = Utils.normalize_template_name(payload[:identifier]))
span.resource = template_name
span.set_tag(Ext::TAG_TEMPLATE_NAME, template_name)
end

record_exception(span, payload)
rescue StandardError => e
Expand Down
6 changes: 4 additions & 2 deletions lib/ddtrace/contrib/action_view/events/render_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def span_name
def process(span, _event, _id, payload)
span.span_type = Datadog::Ext::HTTP::TEMPLATE

template_name = Utils.normalize_template_name(payload[:identifier])
span.set_tag(Ext::TAG_TEMPLATE_NAME, template_name) if template_name
if (template_name = Utils.normalize_template_name(payload[:identifier]))
span.resource = template_name
span.set_tag(Ext::TAG_TEMPLATE_NAME, template_name)
end

layout = payload[:layout]
span.set_tag(Ext::TAG_LAYOUT, layout) if layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def render_template_with_datadog(*args)
template_name = Utils.normalize_template_name(template_name)

if template_name
active_datadog_span.resource = template_name
active_datadog_span.set_tag(
Ext::TAG_TEMPLATE_NAME,
template_name
Expand Down Expand Up @@ -113,6 +114,7 @@ def datadog_render_template(template, layout_name)
layout = layout_name.try(:[], 'virtual_path') # Proc can be called without parameters since Rails 6

if template_name
active_datadog_span.resource = template_name
active_datadog_span.set_tag(
Ext::TAG_TEMPLATE_NAME,
template_name
Expand Down
13 changes: 7 additions & 6 deletions test/contrib/rails/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TracingControllerTest < ActionController::TestCase
span = spans[1]
assert_equal(span.name, 'rails.render_template')
assert_equal(span.span_type, 'template')
assert_equal(span.resource, 'rails.render_template')
assert_equal(span.resource, 'tracing/index.html.erb')
assert_equal(span.get_tag('rails.template_name'), 'tracing/index.html.erb') if Rails.version >= '3.2.22.5'
assert_includes(span.get_tag('rails.template_name'), 'tracing/index.html')
assert_equal(span.get_tag('rails.layout'), 'layouts/application') if Rails.version >= '3.2.22.5'
Expand All @@ -72,7 +72,8 @@ class TracingControllerTest < ActionController::TestCase
span = spans[1]
assert_equal(span.name, 'rails.render_template')
assert_equal(span.span_type, 'template')
assert_equal(span.resource, 'rails.render_template')
assert_equal(span.resource, 'tracing/index.html.erb') if Rails.version >= '3.2.22.5'
assert_includes(span.resource, 'tracing/index.html')
assert_equal(span.get_tag('rails.template_name'), 'tracing/index.html.erb') if Rails.version >= '3.2.22.5'
assert_includes(span.get_tag('rails.template_name'), 'tracing/index.html')
ensure
Expand All @@ -90,7 +91,7 @@ class TracingControllerTest < ActionController::TestCase
_, span_partial, span_template = spans
assert_equal(span_partial.name, 'rails.render_partial')
assert_equal(span_partial.span_type, 'template')
assert_equal(span_partial.resource, 'rails.render_partial')
assert_equal(span_partial.resource, 'tracing/_body.html.erb')
assert_equal(span_partial.get_tag('rails.template_name'), 'tracing/_body.html.erb') if Rails.version >= '3.2.22.5'
assert_includes(span_partial.get_tag('rails.template_name'), 'tracing/_body.html')
assert_equal(span_partial.parent, span_template)
Expand All @@ -108,12 +109,12 @@ class TracingControllerTest < ActionController::TestCase
spans = @tracer.writer.spans
assert_equal(spans.length, 4)

_, span_outer_partial, span_inner_partial, span_template = spans
_, span_inner_partial, span_outer_partial, span_template = spans

# Outer partial
assert_equal('rails.render_partial', span_outer_partial.name)
assert_equal('template', span_outer_partial.span_type)
assert_equal('rails.render_partial', span_outer_partial.resource)
assert_equal('tracing/_outer_partial.html.erb', span_outer_partial.resource)
if Rails.version >= '3.2.22.5'
assert_equal('tracing/_outer_partial.html.erb', span_outer_partial.get_tag('rails.template_name'))
end
Expand All @@ -123,7 +124,7 @@ class TracingControllerTest < ActionController::TestCase
# Inner partial
assert_equal('rails.render_partial', span_inner_partial.name)
assert_equal('template', span_inner_partial.span_type)
assert_equal('rails.render_partial', span_inner_partial.resource)
assert_equal('tracing/_inner_partial.html.erb', span_inner_partial.resource)
if Rails.version >= '3.2.22.5'
assert_equal('tracing/_inner_partial.html.erb', span_inner_partial.get_tag('rails.template_name'))
end
Expand Down
10 changes: 6 additions & 4 deletions test/contrib/rails/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class TracingControllerTest < ActionController::TestCase
assert_equal(span_template.name, 'rails.render_template')
assert_equal(span_template.status, 1)
assert_equal(span_template.span_type, 'template')
assert_equal(span_template.resource, 'rails.render_template')
assert_equal(span_template.resource, 'tracing/missing_partial.html.erb')
assert_equal(span_template.get_tag('rails.template_name'), 'tracing/missing_partial.html.erb')
assert_equal(span_template.get_tag('rails.layout'), 'layouts/application')
assert_includes(span_template.get_tag('error.msg'), error_msg)
Expand Down Expand Up @@ -153,8 +153,9 @@ class TracingControllerTest < ActionController::TestCase
assert_equal(span_template.name, 'rails.render_template')
assert_equal(span_template.status, 1)
assert_equal(span_template.span_type, 'template')
assert_equal(span_template.resource, 'rails.render_template')
assert_includes(span_template.resource, 'tracing/error.html')
if Rails.version >= '3.2.22.5'
assert_equal(span_template.resource, 'tracing/error.html.erb')
assert_equal(span_template.get_tag('rails.template_name'),
'tracing/error.html.erb')
end
Expand Down Expand Up @@ -189,8 +190,9 @@ class TracingControllerTest < ActionController::TestCase
assert_equal(span_partial.name, 'rails.render_partial')
assert_equal(span_partial.status, 1)
assert_equal(span_partial.span_type, 'template')
assert_equal(span_partial.resource, 'rails.render_partial')
assert_includes(span_partial.resource, 'tracing/_inner_error.html')
if Rails.version >= '3.2.22.5'
assert_equal(span_partial.resource, 'tracing/_inner_error.html.erb')
assert_equal(span_partial.get_tag('rails.template_name'),
'tracing/_inner_error.html.erb')
end
Expand All @@ -201,7 +203,7 @@ class TracingControllerTest < ActionController::TestCase
assert_equal(span_template.name, 'rails.render_template')
assert_equal(span_template.status, 1)
assert_equal(span_template.span_type, 'template')
assert_equal(span_template.resource, 'rails.render_template')
assert_includes(span_template.resource, 'tracing/error_partial.html')
if Rails.version >= '3.2.22.5'
assert_equal(span_template.get_tag('rails.template_name'),
'tracing/error_partial.html.erb')
Expand Down
2 changes: 1 addition & 1 deletion test/contrib/rails/rack_middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FullStackTest < ActionDispatch::IntegrationTest
assert_equal(render_span.name, 'rails.render_template')
assert_equal(render_span.span_type, 'template')
assert_equal(render_span.service, Datadog.configuration[:rails][:service_name])
assert_equal(render_span.resource, 'rails.render_template')
assert_equal(render_span.resource, 'tracing/full.html.erb')
assert_equal(render_span.get_tag('rails.template_name'), 'tracing/full.html.erb')

adapter_name = get_adapter_name
Expand Down

0 comments on commit ba583b5

Please sign in to comment.