Grape
Grape applications are officially supported. Instrumenting Grape applications requires some manual setup. Follow the installation steps in AppSignal, starting by clicking 'Add app' on the accounts screen.
Installation
After installing and configuring the AppSignal gem, add the AppSignal integration after the Grape app is loaded.
The Grape integration is required from the AppSignal Ruby gem, and the EventHandler instrumentation and instrumentation middleware are added in the following code examples.
# config.ru require "appsignal" # Add this require # Load the Grape integration Appsignal.load(:grape) # Add the AppSignal Rack EventHandler # AppSignal for Ruby gem 3.8+ required use ::Rack::Events, [Appsignal::Rack::EventHandler.new] # Require the app require_relative "api" # Start AppSignal Appsignal.start # Start the app run Acme::API
The next step is to add the AppSignal middleware to the application's base API class. It will report any unhandled exceptions from the application.
# api.rb module Acme class API < Grape::API # Add this line insert_before Grape::Middleware::Error, Appsignal::Rack::GrapeMiddleware # Include this middleware # ... end end
Legacy installation
Applications using AppSignal for Ruby gem 3.11 or older, follow these steps to install AppSignal in Grape applications.
The Grape integration is required from the AppSignal Ruby gem, and the EventHandler instrumentation and instrumentation middleware are added in the following code examples.
# config.ru require "appsignal/integrations/grape" # Add this require # Configure AppSignal Appsignal.config = Appsignal::Config.new( Dir.pwd, # The root of your app ENV["RACK_ENV"] # The environment of your app (development/production) ) # Start AppSignal Appsignal.start_logger # Not required in Ruby gem 3.9.3+ Appsignal.start # Add the AppSignal Rack EventHandler # AppSignal for Ruby gem 3.8+ required use ::Rack::Events, [Appsignal::Rack::EventHandler.new] # Require the app require_relative "api" # Start the app run Acme::API
The next step is to add the AppSignal middleware to the application's base API class. It will report any unhandled exceptions from the application.
# api.rb module Acme class API < Grape::API # Add this line insert_before Grape::Middleware::Error, Appsignal::Grape::Middleware # Include this middleware # ... end end
Mounted Grape apps
Mounting Grape applications on Ruby on Rails applications is supported. The Ruby gem needs to be a recent version to properly instrument requests for mounted Grape apps.
Ignoring errors
To ignore a specific Grape error, set the grape.skip_appsignal_error
flag in the request environment. Setting this flag to true
will instruct AppSignal not to report any errors during the request.
Use this flag only if you need to dynamically ignore errors from a Grape application. Specify error classes in the ignore_errors
option to ignore them for the entire application. For more information on muting notifications for specific errors see our notification settings documentation.
get "/" do env["grape.skip_appsignal_error"] = true # Add this line to an endpoint or callback raise "uh oh" # Example error, don't copy this end
Reporting errors from rescue_from
If an error is rescued in the app using rescue_from,
AppSignal will not receive and track it. To still report the error, call Appsignal.report_error
in the rescue_from
block. (Use the Appsignal.set_error
helper when using Ruby gem version 3 or older.)
class Api < ::Grape::API insert_before Grape::Middleware::Error, Appsignal::Grape::Middleware format :json rescue_from :all do |error| Appsignal.report_error(error) error!({ :error => "error message" }.to_json, 500) end end