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

Use AbstractMiddleware for Rails middleware #1107

Merged
merged 5 commits into from
Jun 27, 2024
Merged

Conversation

tombruijn
Copy link
Member

@tombruijn tombruijn commented Jun 21, 2024

Part of #329

Fix private API declarations

Define what is a private API on the classes themselves, and not the parent module for Rack middlewares.

Guard against request method throwing an error

We've seen in the Rails instrumentation that the ActionDispatch::Request class can raise an error on an invalid request method type. Guard against it and don't try to set a value on error.

Add a test for setting custom params

Make sure the AbstractMiddleware allows apps to set custom params, and that they're not overwritten by the middleware.

Use AbstractMiddleware for Rails middleware

Use our new AbstractMiddleware as a basis for the Rails instrumentation middleware. This moves all responsibility for handling nested instrumentation middleware to the AbstractMiddleware.

The Rails middleware can now focus on only the part it needs to do: the action name and some additional metadata.

The RailsInstrumentation middleware previously didn't create an event, but the AbstractMiddleware does do that now. It will report a new middleware.rails event now.

I didn't add a changeset for this change as it's mostly an internal refactor.

Fix error reporting for Rails instrumentation

Updating the RailsInstrumentation middleware to inherit from the AbstractMiddleware broke the error reporting for the middleware. It relied on the EventHandler to report errors, but before it gets there, Rails's exception handling middleware rescues the error and shows an error page instead.

Add a config option to the AbstractMiddleware to configure if it should report errors or not. Enable it for the RailsInstrumentation to restore the error reporting functionality.

@tombruijn tombruijn self-assigned this Jun 21, 2024
@tombruijn tombruijn mentioned this pull request Jun 21, 2024
36 tasks
@tombruijn tombruijn changed the base branch from main to rack-params June 21, 2024 20:36
@tombruijn tombruijn changed the base branch from rack-params to main June 24, 2024 13:13
@tombruijn tombruijn force-pushed the rails-middleware-refactor branch 2 times, most recently from 9bc8bc3 to b7cdd26 Compare June 24, 2024 20:21
@backlog-helper

This comment has been minimized.

Copy link
Contributor

@unflxw unflxw left a comment

Choose a reason for hiding this comment

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

Tested it, seems to work as described.

I'm curious where this middleware.rails event comes from? Does it represent our own instrumentation middleware? If so, would it be desirable not to show it in the event timeline?

@tombruijn
Copy link
Member Author

I'm curious where this middleware.rails event comes from? Does it represent our own instrumentation middleware? If so, would it be desirable not to show it in the event timeline?

It comes from the RailsInstrumentation middleware, which is the point we previously started instrumentating requests. Every subclass of AbstractMiddleware adds its own instrumentation event. Not sure if it's the best name 🤷‍♂️

@tombruijn
Copy link
Member Author

@unflxw do you think it would be better to omit this instrumentation event? On a timeline like below it might be difficult to understand the differences between the action dispatch and rails events.

image

Define what is a private API on the classes themselves, and not the
parent module for Rack middlewares.
We've seen in the Rails instrumentation that the
`ActionDispatch::Request` class can raise an error on an invalid request
method type. Guard against it and don't try to set a value on error.
Make sure the AbstractMiddleware allows apps to set custom params, and
that they're not overwritten by the middleware.
Use our new AbstractMiddleware as a basis for the Rails instrumentation
middleware. This moves all responsibility for handling nested
instrumentation middleware to the AbstractMiddleware.

The Rails middleware can now focus on only the part it needs to do: the
action name and some additional metadata.

The RailsInstrumentation middleware previously didn't create an event,
but the AbstractMiddleware does do that now. It will report a new
`middleware.rails` event now.

I didn't add a changeset for this change as it's mostly an internal
refactor.
Updating the RailsInstrumentation middleware to inherit from the
AbstractMiddleware broke the error reporting for the middleware.
It relied on the EventHandler to report errors, but before it gets
there, Rails's exception handling middleware rescues the error and shows
an error page instead.

Add a config option to the AbstractMiddleware to configure if it should
report errors or not. Enable it for the RailsInstrumentation to restore
the error reporting functionality.
@tombruijn
Copy link
Member Author

Turns out I broke error reporting in this PR for Rails apps. I've restored that functionality in the last commit.

@backlog-helper

This comment has been minimized.

@tombruijn
Copy link
Member Author

tombruijn commented Jun 26, 2024

@unflxw I thought I'd ask you for another review since your last review. Only the last commit has changed since you last reviewed.

@unflxw
Copy link
Contributor

unflxw commented Jun 27, 2024

@tombruijn If it doesn't represent anything meaningful for the customer's application, I think we shouldn't show it in the event timeline. I think customers will see it and assume it represents an "interesting" middleware in their Rails application (like middleware.express does) rather than a middleware that AppSignal added.

@backlog-helper
Copy link


This is a message from the daily scheduled checks.

New issue guide | Backlog management | Rules | Feedback

@tombruijn
Copy link
Member Author

@unflxw I can make the abstract middleware not create an event for the Rails middleware. Let's do that in another PR, this one has been open long enough.

@tombruijn tombruijn merged commit c0c3815 into main Jun 27, 2024
16 checks passed
tombruijn added a commit that referenced this pull request Jun 27, 2024
As discussed in PR #1107, do not create a transaction event for a Rails
apps using the RailsInstrumentation middleware. The ActiveSupport
Notifications and Rack EventHandler events are enough. The
`middleware.rails` event didn't add much.

Closes #1120

[skip changeset] because the related change in PR #1107 has not been
released yet.
tombruijn added a commit that referenced this pull request Jun 28, 2024
As discussed in PR #1107, do not create a transaction event for a Rails
apps using the RailsInstrumentation middleware. The ActiveSupport
Notifications and Rack EventHandler events are enough. The
`middleware.rails` event didn't add much.

Closes #1120

[skip changeset] because the related change in PR #1107 has not been
released yet.
@tombruijn tombruijn deleted the rails-middleware-refactor branch July 10, 2024 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants