Skip to content

Commit

Permalink
Merge pull request #1748 from newrelic/NR1512_subscribe_actionview
Browse files Browse the repository at this point in the history
Subscribe to additional Action View topic
  • Loading branch information
hannahramadan authored Jan 21, 2023
2 parents e675cb8 + 7e3a4f8 commit 57d6f61
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/new_relic/agent/instrumentation/action_view_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,25 @@ module NewRelic
module Agent
module Instrumentation
class ActionViewSubscriber < NotificationsSubscriber
def start(name, id, payload) # THREAD_LOCAL_ACCESS
def start_segment(name, id, payload)
parent = segment_stack[id].last
metric_name = format_metric_name(name, payload, parent)

event = ActionViewEvent.new(metric_name, payload[:identifier])
if state.is_execution_traced? && recordable?(name, metric_name)

if recordable?(name, metric_name)
event.finishable = Tracer.start_segment(name: metric_name)
end
push_segment(id, event)
rescue => e
log_notification_error(e, name, 'start')
end

def finish(name, id, payload)
def finish_segment(id, payload)
if segment = pop_segment(id)
if exception = exception_object(payload)
segment.notice_error(exception)
end
segment.finish
end
rescue => e
log_notification_error(e, name, 'finish')
end

def format_metric_name(event_name, payload, parent)
Expand Down Expand Up @@ -61,12 +58,15 @@ def recordable?(event_name, metric_name)
RENDER_TEMPLATE_EVENT_NAME = 'render_template.action_view'.freeze
RENDER_PARTIAL_EVENT_NAME = 'render_partial.action_view'.freeze
RENDER_COLLECTION_EVENT_NAME = 'render_collection.action_view'.freeze
RENDER_LAYOUT_EVENT_NAME = 'render_layout.action_view'.freeze

def metric_action(name)
case name
when /#{RENDER_TEMPLATE_EVENT_NAME}$/o then 'Rendering'
when RENDER_PARTIAL_EVENT_NAME then 'Partial'
when RENDER_COLLECTION_EVENT_NAME then 'Partial'
when RENDER_LAYOUT_EVENT_NAME then 'Layout'
else 'Unknown'
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ def test_records_metrics_for_simple_collection
assert_metrics_recorded('View/model/_user.html.erb/Partial' => expected)
end

def test_records_metrics_for_simple_layout
params = {:identifier => '/root/app/views/model/_layout.html.erb'}
nr_freeze_process_time
in_transaction do
@subscriber.start('render_layout.action_view', :id, params)
@subscriber.start('!render_layout.action_view', :id,
:virtual_path => 'model/_layout')
advance_process_time(2.0)
@subscriber.finish('!render_layout.action_view', :id,
:virtual_path => 'model/_layout')
@subscriber.finish('render_layout.action_view', :id, params)
end
expected = {:call_count => 1, :total_call_time => 2.0}

assert_metrics_recorded('View/model/_layout.html.erb/Layout' => expected)
end

def test_records_metrics_for_layout
nr_freeze_process_time
in_transaction do
Expand Down Expand Up @@ -263,4 +280,18 @@ def test_metric_path_cannot_identify_empty_collection_render_event
def test_metric_path_index_html_erb
assert_equal('model/index.html.erb', @subscriber.metric_path('render_template.action_view', 'model/index.html.erb'))
end

def test_finish_segment_when_no_segment
@subscriber.stub :pop_segment, nil do
assert_nil @subscriber.send(:finish_segment, :id, {})
end
end

def test_metric_action_nil_name
assert_equal 'Unknown', @subscriber.metric_action(nil)
end

def test_metric_action_unknown_name
assert_equal 'Unknown', @subscriber.metric_action('cheez-it')
end
end

0 comments on commit 57d6f61

Please sign in to comment.