Skip to content

Instantly share code, notes, and snippets.

@mipearson
Last active December 26, 2015 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mipearson/7225458 to your computer and use it in GitHub Desktop.
Save mipearson/7225458 to your computer and use it in GitHub Desktop.
Tracking default route usage with Rollbar
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
# 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
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