Last active
December 26, 2015 22:49
-
-
Save mipearson/7225458 to your computer and use it in GitHub Desktop.
Tracking default route usage with Rollbar
This file contains hidden or 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
class ApplicationController < ActionController::Base | |
# ... | |
before_filter :check_for_default_route | |
class DefaultRouteException < Exception | |
# Placeholder exception so that we can report request & person data back | |
# to rollbar | |
end | |
def check_for_default_route | |
ignore_user_agents = %w{bingbot googlebot crawler wget curl spider mj12bot} | |
if params[:using_default_route] | |
if ignore_user_agents.none? {|s| request.env['HTTP_USER_AGENT'].downcase.include?(s)} | |
e = DefaultRouteException.new("Default Route for #{request.path.inspect}") | |
Rollbar.report_exception(e, rollbar_request_data, rollbar_person_data) | |
end | |
params.delete(:using_default_route) | |
end | |
end | |
# ... | |
end |
This file contains hidden or 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
# config/initializers/rollbar.rb | |
require 'rollbar/rails' | |
if Rails.env.production? | |
Rollbar.configure do |config| | |
config.access_token = '...' | |
config.exception_level_filters.merge!( | |
'ApplicationController::DefaultRouteException' => 'warning' | |
) | |
end | |
else | |
Rollbar.configure do |config| | |
# Disabled in dev & test, but we want to be able to call it | |
# from controllers | |
config.enabled = false | |
end | |
end |
This file contains hidden or 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
Application.routes.draw do | |
# ... all your nice explicit routes ... | |
match '/:controller(/:action(/:id))', using_default_route: true | |
# Hack - above route is the one that's actually caught, below is the one | |
# that's used by url_for. | |
match '/:controller(/:action(/:id))' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment