-
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 support for nested Rack middlewares #1100
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changesets/support-nested-genericinstrumentation-middleware.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
bump: patch | ||
type: change | ||
--- | ||
|
||
Improve support for instrumentation of nested pure Rack and Sinatra apps. It will now report more of the request's duration and events. This also improves support for apps that have multiple Rack GenericInstrumentation or SinatraInstrumentation middlewares. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rack" | ||
|
||
module Appsignal | ||
# @api private | ||
module Rack | ||
class AbstractMiddleware | ||
def initialize(app, options = {}) | ||
Appsignal.internal_logger.debug "Initializing #{self.class}" | ||
@app = app | ||
@options = options | ||
@request_class = options.fetch(:request_class, ::Rack::Request) | ||
@params_method = options.fetch(:params_method, :params) | ||
@instrument_span_name = options.fetch(:instrument_span_name, "process.abstract") | ||
end | ||
|
||
def call(env) | ||
if Appsignal.active? | ||
request = request_for(env) | ||
# Supported nested instrumentation middlewares by checking if there's | ||
# already a transaction active for this request. | ||
wrapped_instrumentation = env.key?(Appsignal::Rack::APPSIGNAL_TRANSACTION) | ||
transaction = | ||
if wrapped_instrumentation | ||
env[Appsignal::Rack::APPSIGNAL_TRANSACTION] | ||
else | ||
Appsignal::Transaction.create( | ||
SecureRandom.uuid, | ||
Appsignal::Transaction::HTTP_REQUEST, | ||
request | ||
) | ||
end | ||
|
||
add_transaction_metadata_before(transaction, request) | ||
if wrapped_instrumentation | ||
instrument_wrapped_request(request, transaction) | ||
else | ||
# Set transaction on the request environment so other nested | ||
# middleware can detect if there is parent instrumentation | ||
# middleware active. | ||
env[Appsignal::Rack::APPSIGNAL_TRANSACTION] = transaction | ||
instrument_request(request, transaction) | ||
end | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
private | ||
|
||
# Another instrumentation middleware is active earlier in the stack, so | ||
# don't report any exceptions here, the top instrumentation middleware | ||
# will be the one reporting the exception. | ||
# | ||
# Either another {GenericInstrumentation} or {EventHandler} is higher in | ||
# the stack and will report the exception and complete the transaction. | ||
# | ||
# @see {#instrument_request} | ||
def instrument_wrapped_request(request, transaction) | ||
instrument_app_call(request.env) | ||
ensure | ||
add_transaction_metadata_after(transaction, request) | ||
end | ||
|
||
# Instrument the request fully. This is used by the top instrumentation | ||
# middleware in the middleware stack. Unlike | ||
# {#instrument_wrapped_request} this will report any exceptions being | ||
# raised. | ||
# | ||
# @see {#instrument_wrapped_request} | ||
def instrument_request(request, transaction) | ||
instrument_app_call(request.env) | ||
rescue Exception => error # rubocop:disable Lint/RescueException | ||
transaction.set_error(error) | ||
raise error | ||
ensure | ||
add_transaction_metadata_after(transaction, request) | ||
|
||
# Complete transaction because this is the top instrumentation middleware. | ||
Appsignal::Transaction.complete_current! | ||
end | ||
|
||
def instrument_app_call(env) | ||
Appsignal.instrument(@instrument_span_name) do | ||
@app.call(env) | ||
end | ||
end | ||
|
||
# Add metadata to the transaction based on the request environment. | ||
# Override this method to set metadata before the app is called. | ||
# Call `super` to also include the default set metadata. | ||
def add_transaction_metadata_before(transaction, request) | ||
params = params_for(request) | ||
transaction.params = params if params | ||
end | ||
|
||
# Add metadata to the transaction based on the request environment. | ||
# Override this method to set metadata after the app is called. | ||
# Call `super` to also include the default set metadata. | ||
def add_transaction_metadata_after(transaction, request) | ||
default_action = | ||
request.env["appsignal.route"] || request.env["appsignal.action"] | ||
transaction.set_action_if_nil(default_action) | ||
transaction.set_metadata("path", request.path) | ||
transaction.set_metadata("method", request.request_method) | ||
transaction.set_http_or_background_queue_start | ||
end | ||
|
||
def params_for(request) | ||
return unless request.respond_to?(@params_method) | ||
|
||
request.send(@params_method) | ||
rescue => error | ||
# Getting params from the request has been know to fail. | ||
Appsignal.internal_logger.debug( | ||
"Exception while getting params in #{self.class} from '#{@params_method}': #{error}" | ||
) | ||
nil | ||
end | ||
|
||
def request_for(env) | ||
@request_class.new(env) | ||
end | ||
end | ||
end | ||
end |
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
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
Oops, something went wrong.
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.
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.
Perhaps we could use
.rack
as the category group, which already has a defined color.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.
I want to go through all these event names some time later, because they report
process_action
which makes no sense for a rack middleware. Thegeneric
group isn't a good name either, we can change it to rack, but I'd rather do that later.