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

Controller methods not in controller class don't warn #2061

Merged
merged 5 commits into from
Jun 12, 2023

Conversation

hannahramadan
Copy link
Contributor

@hannahramadan hannahramadan commented Jun 8, 2023

Rails controllers can be a special case because by default, controllers in Rails automatically render views with names that correspond to valid routes. This means that a controller method may not have a corresponding method in the controller class.

This PR adds a check to see if we have a valid method that isn't attached to a controller, and if so, can we figure out what controller it belongs to.

The check makes an assumption of a simple rails app with all controllers living inside app/controllers, so if a customer's app breaks this pattern, the check fails, we don't report on method metrics, and we also don't get false warnings. If we can determine the controller, the method information is inserted at the top of the file.

Community issue #2048

@hannahramadan hannahramadan changed the title WIP: Initial commit Controllers without methods don't warn Jun 9, 2023
@hannahramadan hannahramadan changed the title Controllers without methods don't warn Controller methods not in controller class don't warn Jun 9, 2023
@hannahramadan hannahramadan marked this pull request as ready for review June 9, 2023 19:03
Comment on lines 19 to 27
class String
def underscore
'test_controller'
end

def join(*args)
'User/fake_app/app/controllers/test_controller.rb'
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool idea! Are these methods still being used? If so, can you show me how they work?

lib/new_relic/agent/method_tracer_helpers.rb Show resolved Hide resolved
lib/new_relic/agent/method_tracer_helpers.rb Outdated Show resolved Hide resolved
lib/new_relic/agent/method_tracer_helpers.rb Show resolved Hide resolved
path = Rails.root.join("app/controllers/#{klass.name.underscore}.rb")
return [] unless File.exist?(path)

[name, [path.to_s, 1], is_class_method]
Copy link
Contributor

Choose a reason for hiding this comment

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

path should be a string already and not need the #to_s call

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like out of the 4 method arguments, only klass is used. Maybe we should refactor this method to only return either a path or a nil, and then the caller itself can say "if I got a nil, return [] but if I got a path, return a 4 item array using the returned path and the 3 items I already have".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When testing, the result of calling Rails.root is a Pathname. I'm not sure of all the implications of that, but because in other cases we get a String, I thought it would be safer. Can you see the downsides to it being kept as a Pathname object?

I'll work on that refactor!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah,ok. Check to see if #join returns a string instead of another Pathname object. A string won't need a to_s but another Pathname will.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like .join returns another Pathname

Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect. Sorry for the confusion. The to_s stays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fallwith thoughts on this refactor a9201f1

Copy link
Contributor

@tannalynn tannalynn left a comment

Choose a reason for hiding this comment

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

Other than things already being discussed, I think things look good! We should add a changelog entry for this before merging though

Previously, when a new thread was spawned the agent would continue using the current transaction to record data on, even if this transaction had finished already in a different thread. Now the agent will only use the current transaction in the new thread if it is not yet finished.
@hannahramadan hannahramadan force-pushed the mlt_controller_without_method branch from 23bd3ac to 50a1239 Compare June 11, 2023 23:56
@github-actions
Copy link
Contributor

SimpleCov Report

Coverage Threshold
Line 94.08% 94%
Branch 85.53% 85%

fallwith
fallwith previously approved these changes Jun 12, 2023
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Kayla Reopelle (she/her) <[email protected]>
Copy link
Contributor

@kaylareopelle kaylareopelle left a comment

Choose a reason for hiding this comment

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

🦖 🦕 🦖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants