-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the Grape middleware to the rack directory
Move the Appsignal::Grape::Middleware constant to Appsignal::Rack::GrapeMiddleware so it matches the format of some of the other middleware we have. Move it to the `rack/` directory as well so all the middleware are in one place. People can still require the `appsignal/integrations/grape` file like previously, same as we support for `appsignal/integrations/sinatra`. The integrations file can stay to automate or include any other things we need.
- Loading branch information
Showing
4 changed files
with
124 additions
and
49 deletions.
There are no files selected for viewing
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,18 @@ | ||
--- | ||
bump: patch | ||
type: deprecate | ||
--- | ||
|
||
Deprecate `Appsignal::Grape::Middleware` constant in favor of `Appsignal::Rack::GrapeMiddleware` constant. | ||
|
||
To fix this deprecation warning, update the usage of `Appsignal::Grape::Middleware` like this: | ||
|
||
```ruby | ||
# Grape only apps | ||
insert_before Grape::Middleware::Error, Appsignal::Rack::GrapeMiddleware | ||
# or | ||
use Appsignal::Rack::GrapeMiddleware | ||
|
||
# Grape on Rails app | ||
use Appsignal::Rack::GrapeMiddleware | ||
``` |
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
module Rack | ||
# @api private | ||
class GrapeMiddleware < ::Grape::Middleware::Base | ||
def call(env) | ||
if Appsignal.active? | ||
call_with_appsignal_monitoring(env) | ||
else | ||
app.call(env) | ||
end | ||
end | ||
|
||
def call_with_appsignal_monitoring(env) | ||
request = ::Rack::Request.new(env) | ||
transaction = Appsignal::Transaction.create( | ||
SecureRandom.uuid, | ||
Appsignal::Transaction::HTTP_REQUEST, | ||
request | ||
) | ||
begin | ||
app.call(env) | ||
rescue Exception => error # rubocop:disable Lint/RescueException | ||
# Do not set error if "grape.skip_appsignal_error" is set to `true`. | ||
transaction.set_error(error) unless env["grape.skip_appsignal_error"] | ||
raise error | ||
ensure | ||
request_method = request.request_method.to_s.upcase | ||
path = request.path # Path without namespaces | ||
endpoint = env["api.endpoint"] | ||
|
||
if endpoint&.options | ||
options = endpoint.options | ||
request_method = options[:method].first.to_s.upcase | ||
klass = options[:for] | ||
namespace = endpoint.namespace | ||
namespace = "" if namespace == "/" | ||
|
||
path = options[:path].first.to_s | ||
path = "/#{path}" if path[0] != "/" | ||
path = "#{namespace}#{path}" | ||
|
||
transaction.set_action_if_nil("#{request_method}::#{klass}##{path}") | ||
end | ||
|
||
transaction.set_http_or_background_queue_start | ||
transaction.set_metadata("path", path) | ||
transaction.set_metadata("method", request_method) | ||
Appsignal::Transaction.complete_current! | ||
end | ||
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