-
Notifications
You must be signed in to change notification settings - Fork 116
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
Improve nested Sinatra app instrumentation #1097
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sinatra apps, mounted in Rails app, would run into the issue that the Rails middleware has already created a transaction for the request. It would "force" a new transaction to be made, which loses information from everything that happened before it. Previously, before PR #1089, it would also leave a transaction that was not closed properly. Even with that change, for one request, now two transactions are created, one for Rails and one for the nested Sinatra app. This change reads if there's a current transaction from the request env, and uses that instead of creating a new one. Some logic in the Transaction class would read from the request object given to it on `Transaction.create` to set metadata like parameters, so these need to be set manually now. It will also make sure not to close the transaction if one existed already before this middleware was called. Part of #329, the Rack middleware refactor.
Build fixed in #1098 |
luismiramirez
approved these changes
Jun 18, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏🏼
tombruijn
added a commit
that referenced
this pull request
Jun 19, 2024
When more of our Rack, Rails and Sinatra middlewares are nested in one app, improve reporting for those requests. This follows PRs #1089 for Rails and #1097 for Sinatra. This change improves this reporting for Rack apps using the GenericInstrumentation middleware. Other than supporting this scenario better, no one should notice this change. ## Refactor details I've refactored the SinatraInstrumentation middleware to inherit from a new AbstractMiddleware which includes much of the behavior from the GenericInstrumentation and previous SinatraInstrumentation implementations. All the logic for nested instrumentation middlewares are now handled in this AbstractMiddleware. Subclasses of AbstractMiddleware can set their library's specific metadata using the `add_transaction_metadata_before` and `add_transaction_metadata_after` methods, along with specifying the `request_class`, `params_method` and `instrument_span_name` settings. This already works with the EventHandler, as both set the transaction on the request env to detect nested instrumentation. An app could add both the EventHandler and a subclass of the AbstractMiddleware, and it would report the request transaction properly. ## GenericInstrumentation I've kept the GenericInstrumentation middleware. The only thing it really does different that we don't want to put in the AbstractMiddleware is the fallback to the "unknown" action name. I didn't want to break existing behavior, so that is all it still does. If we move the GenericInstrumentation action name fallback to the AbstractMiddleware, we may be reporting more requests than we want for other middlewares that inherit from it. For example, for Rails app, I also want to use this AbstractMiddleware, and it relies on asset requests having no action name so the extension can drop them. That way we don't report transactions for asset requests. ## Next steps If this change is approved, I will update the other Rack instrumentations, like Rails, Grape and Padrino.
tombruijn
added a commit
that referenced
this pull request
Jun 19, 2024
When more of our Rack, Rails and Sinatra middlewares are nested in one app, improve reporting for those requests. This follows PRs #1089 for Rails and #1097 for Sinatra. This change improves this reporting for Rack apps using the GenericInstrumentation middleware. Other than supporting this scenario better, no one should notice this change. ## Refactor details I've refactored the SinatraInstrumentation middleware to inherit from a new AbstractMiddleware which includes much of the behavior from the GenericInstrumentation and previous SinatraInstrumentation implementations. All the logic for nested instrumentation middlewares are now handled in this AbstractMiddleware. Subclasses of AbstractMiddleware can set their library's specific metadata using the `add_transaction_metadata_before` and `add_transaction_metadata_after` methods, along with specifying the `request_class`, `params_method` and `instrument_span_name` settings. This already works with the EventHandler, as both set the transaction on the request env to detect nested instrumentation. An app could add both the EventHandler and a subclass of the AbstractMiddleware, and it would report the request transaction properly. ## GenericInstrumentation I've kept the GenericInstrumentation middleware. The only thing it really does different that we don't want to put in the AbstractMiddleware is the fallback to the "unknown" action name. I didn't want to break existing behavior, so that is all it still does. If we move the GenericInstrumentation action name fallback to the AbstractMiddleware, we may be reporting more requests than we want for other middlewares that inherit from it. For example, for Rails app, I also want to use this AbstractMiddleware, and it relies on asset requests having no action name so the extension can drop them. That way we don't report transactions for asset requests. ## Next steps If this change is approved, I will update the other Rack instrumentations, like Rails, Grape and Padrino.
tombruijn
added a commit
that referenced
this pull request
Jun 21, 2024
When more of our Rack, Rails and Sinatra middlewares are nested in one app, improve reporting for those requests. This follows PRs #1089 for Rails and #1097 for Sinatra. This change improves this reporting for Rack apps using the GenericInstrumentation middleware. Other than supporting this scenario better, no one should notice this change. ## Refactor details I've refactored the SinatraInstrumentation middleware to inherit from a new AbstractMiddleware which includes much of the behavior from the GenericInstrumentation and previous SinatraInstrumentation implementations. All the logic for nested instrumentation middlewares are now handled in this AbstractMiddleware. Subclasses of AbstractMiddleware can set their library's specific metadata using the `add_transaction_metadata_before` and `add_transaction_metadata_after` methods, along with specifying the `request_class`, `params_method` and `instrument_span_name` settings. This already works with the EventHandler, as both set the transaction on the request env to detect nested instrumentation. An app could add both the EventHandler and a subclass of the AbstractMiddleware, and it would report the request transaction properly. ## GenericInstrumentation I've kept the GenericInstrumentation middleware. The only thing it really does different that we don't want to put in the AbstractMiddleware is the fallback to the "unknown" action name. I didn't want to break existing behavior, so that is all it still does. If we move the GenericInstrumentation action name fallback to the AbstractMiddleware, we may be reporting more requests than we want for other middlewares that inherit from it. For example, for Rails app, I also want to use this AbstractMiddleware, and it relies on asset requests having no action name so the extension can drop them. That way we don't report transactions for asset requests. ## Next steps If this change is approved, I will update the other Rack instrumentations, like Rails, Grape and Padrino.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Sinatra apps, mounted in Rails app, would run into the issue that the Rails middleware has already created a transaction for the request. It could "force" a new transaction to be made, which loses information from everything that happened before it.
Previously, before PR #1089, it would also leave a transaction that was not closed properly. Even with that change, for one request, now two transactions are created, one for Rails and one for the nested Sinatra app.
This change reads if there's a current transaction from the request env, and uses that instead of creating a new one. Some logic in the Transaction class would read from the request object given to it on
Transaction.create
to set metadata like parameters, so these need to be set manually now.It will also make sure not to close the transaction if one existed already before this middleware was called.
Part of #329, the Rack middleware refactor.