Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include additional AC topics in Rails subscription #2745

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@
NewRelic::Agent::Instrumentation::ActionControllerSubscriber \
.subscribe(/^process_action.action_controller$/)

subs = %w[send_file
subs = %w[exist_fragment?
expire_fragment
halted_callback
read_fragment
redirect_to
send_data
send_file
send_stream
redirect_to
halted_callback
unpermitted_parameters]
write_fragment
unpermitted_parameters].map { |s| Regexp.escape(s) }

# have to double escape period because its going from string -> regex
NewRelic::Agent::Instrumentation::ActionControllerOtherSubscriber \
.subscribe(Regexp.new("^(#{subs.join('|')})\\.action_controller$"))
.subscribe(Regexp.new("^(?:#{subs.join('|')})\\.action_controller$"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the addition of ?: doing as a non-regex pro?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, wrapping part of the pattern in parens will cause the part of the match to be captured. By using (?:) instead of (), I'm telling both the parser and the future human reviewer that we don't actually intend to do any capturing with this regex and simply want to use the parens for grouping.

end
end
83 changes: 82 additions & 1 deletion test/multiverse/suites/rails/action_controller_other_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
# frozen_string_literal: true

require './app'

if defined?(ActionController::Live)

class DataController < ApplicationController
CACHE_KEY = :the_key

# send_file
def send_test_file
send_file(Rails.root + '../../../../README.md')
Expand Down Expand Up @@ -35,6 +36,26 @@ def do_a_redirect
def not_allowed
params.permit(:only_this)
end

# exist_fragment?
def exist_fragment
fragment_exist?(CACHE_KEY)
end

# expire_fragment
def expire_test_fragment
expire_fragment(CACHE_KEY)
end

# read_fragment
def read_test_fragment
read_fragment(CACHE_KEY)
end

# write_fragment
def write_test_fragment
write_fragment(CACHE_KEY, 'fragment')
end
end

class ActionControllerDataTest < ActionDispatch::IntegrationTest
Expand Down Expand Up @@ -121,6 +142,66 @@ def test_unpermitted_parameters
assert_metrics_recorded(['Controller/data/not_allowed', segment_name])
end

def test_exist_fragment?
skip if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new('6.0.0')

get('/data/exist_fragment')

node = find_node_with_name_matching(last_transaction_trace, /ActionController/)

assert node, 'Could not find an >>ActionController<< metric while testing >>exist_fragment?<<'
child = node.children.detect { |n| n.metric_name.include?('FileStore/exist') }

assert child, 'Could not find a >>Filestore/exist<< child of the ActionController node!'
assert_includes child.params[:key], DataController::CACHE_KEY,
"Expected to find the cache key >>#{DataController::CACHE_KEY}<< in the node params!"
end

def test_expire_fragment
skip if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new('6.0.0')

get('/data/expire_test_fragment')

node = find_node_with_name_matching(last_transaction_trace, /ActionController/)

assert node, 'Could not find an >>ActionController<< metric while testing >>expire_fragment<<'
child = node.children.detect { |n| n.metric_name.include?('FileStore/delete') }

assert child, 'Could not find a >>Filestore/delete<< child of the ActionController node!'
assert_includes child.params[:key], DataController::CACHE_KEY,
"Expected to find the cache key >>#{DataController::CACHE_KEY}<< in the node params!"
end

def test_read_fragment
skip if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new('6.0.0')

get('/data/read_test_fragment')

node = find_node_with_name_matching(last_transaction_trace, /ActionController/)

assert node, 'Could not find an >>ActionController<< metric while testing >>read_fragment<<'
child = node.children.detect { |n| n.metric_name.include?('FileStore/read') }

assert child, 'Could not find a >>Filestore/read<< child of the ActionController node!'
assert_includes child.params[:key], DataController::CACHE_KEY,
"Expected to find the cache key >>#{DataController::CACHE_KEY}<< in the node params!"
end

def test_write_fragment
skip if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new('6.0.0')

get('/data/write_test_fragment')

node = find_node_with_name_matching(last_transaction_trace, /ActionController/)

assert node, 'Could not find an >>ActionController<< metric while testing >>write_fragment<<'
child = node.children.detect { |n| n.metric_name.include?('FileStore/write') }

assert child, 'Could not find a >>Filestore/write<< child of the ActionController node!'
assert_includes child.params[:key], DataController::CACHE_KEY,
"Expected to find the cache key >>#{DataController::CACHE_KEY}<< in the node params!"
end

class TestClassActionController; end

def test_no_metric_naming_error
Expand Down
Loading