From 8c70f8c5e6ba74e608156157fd6fbfa58050b28d Mon Sep 17 00:00:00 2001 From: Mathieu Jobin Date: Fri, 18 Dec 2020 22:53:18 +0900 Subject: [PATCH 1/3] add catch-all controller to log all request --- app/assets/stylesheets/catch_all.scss | 3 +++ app/controllers/catch_all_controller.rb | 4 ++++ app/helpers/catch_all_helper.rb | 2 ++ app/views/catch_all/log_request.html.erb | 2 ++ config/routes.rb | 1 + test/controllers/catch_all_controller_test.rb | 8 ++++++++ 6 files changed, 20 insertions(+) create mode 100644 app/assets/stylesheets/catch_all.scss create mode 100644 app/controllers/catch_all_controller.rb create mode 100644 app/helpers/catch_all_helper.rb create mode 100644 app/views/catch_all/log_request.html.erb create mode 100644 test/controllers/catch_all_controller_test.rb diff --git a/app/assets/stylesheets/catch_all.scss b/app/assets/stylesheets/catch_all.scss new file mode 100644 index 0000000..475855a --- /dev/null +++ b/app/assets/stylesheets/catch_all.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the catch_all controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/controllers/catch_all_controller.rb b/app/controllers/catch_all_controller.rb new file mode 100644 index 0000000..b3e1be0 --- /dev/null +++ b/app/controllers/catch_all_controller.rb @@ -0,0 +1,4 @@ +class CatchAllController < ApplicationController + def log_request + end +end diff --git a/app/helpers/catch_all_helper.rb b/app/helpers/catch_all_helper.rb new file mode 100644 index 0000000..62c69b5 --- /dev/null +++ b/app/helpers/catch_all_helper.rb @@ -0,0 +1,2 @@ +module CatchAllHelper +end diff --git a/app/views/catch_all/log_request.html.erb b/app/views/catch_all/log_request.html.erb new file mode 100644 index 0000000..8f74c64 --- /dev/null +++ b/app/views/catch_all/log_request.html.erb @@ -0,0 +1,2 @@ +

CatchAll#log_request

+

Find me in app/views/catch_all/log_request.html.erb

diff --git a/config/routes.rb b/config/routes.rb index c06383a..029d7e8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ Rails.application.routes.draw do + get 'catch_all/log_request' # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end diff --git a/test/controllers/catch_all_controller_test.rb b/test/controllers/catch_all_controller_test.rb new file mode 100644 index 0000000..0478ee4 --- /dev/null +++ b/test/controllers/catch_all_controller_test.rb @@ -0,0 +1,8 @@ +require "test_helper" + +class CatchAllControllerTest < ActionDispatch::IntegrationTest + test "should get log_request" do + get catch_all_log_request_url + assert_response :success + end +end From 2d3134f8b725440f97ac182a9c3d9fdeb947dd7c Mon Sep 17 00:00:00 2001 From: Mathieu Jobin Date: Fri, 18 Dec 2020 22:58:56 +0900 Subject: [PATCH 2/3] add catch all route --- config/routes.rb | 3 ++- test/controllers/catch_all_controller_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 029d7e8..62e33b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ +# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html Rails.application.routes.draw do get 'catch_all/log_request' - # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html + get '*magic_request_path', to: 'catch_all#log_request' end diff --git a/test/controllers/catch_all_controller_test.rb b/test/controllers/catch_all_controller_test.rb index 0478ee4..5f9fc4f 100644 --- a/test/controllers/catch_all_controller_test.rb +++ b/test/controllers/catch_all_controller_test.rb @@ -5,4 +5,9 @@ class CatchAllControllerTest < ActionDispatch::IntegrationTest get catch_all_log_request_url assert_response :success end + + test "hitting any paths should get log_request" do + get "/foo/bar" + assert_response :success + end end From d034f01edccbfafa6afa8e5764088eaf35f3b9fa Mon Sep 17 00:00:00 2001 From: Mathieu Jobin Date: Fri, 18 Dec 2020 23:09:39 +0900 Subject: [PATCH 3/3] setup sentry and raise an exception to catch logging --- Gemfile | 4 ++++ Gemfile.lock | 13 +++++++++++++ app/controllers/catch_all_controller.rb | 1 + config/initializers/sentry-init.rb | 13 +++++++++++++ db/schema.rb | 15 +++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 config/initializers/sentry-init.rb create mode 100644 db/schema.rb diff --git a/Gemfile b/Gemfile index f6eb708..3a4142c 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,10 @@ gem 'jbuilder', '~> 2.7' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' + +gem "sentry-ruby" +gem "sentry-rails" + # Use Active Storage variant # gem 'image_processing', '~> 1.2' diff --git a/Gemfile.lock b/Gemfile.lock index b4bb82e..0e7efd4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,9 @@ GEM concurrent-ruby (1.1.7) crass (1.0.6) erubi (1.10.0) + faraday (1.1.0) + multipart-post (>= 1.2, < 3) + ruby2_keywords ffi (1.13.1) globalid (0.4.2) activesupport (>= 4.2.0) @@ -102,6 +105,7 @@ GEM mini_portile2 (2.4.0) minitest (5.14.2) msgpack (1.3.3) + multipart-post (2.1.1) nio4r (2.5.4) nokogiri (1.10.10) mini_portile2 (~> 2.4.0) @@ -146,6 +150,7 @@ GEM rb-inotify (0.10.1) ffi (~> 1.0) regexp_parser (1.8.2) + ruby2_keywords (0.0.2) rubyzip (2.3.0) sass-rails (6.0.0) sassc-rails (~> 2.1, >= 2.1.1) @@ -161,6 +166,12 @@ GEM childprocess (>= 0.5, < 4.0) rubyzip (>= 1.2.2) semantic_range (2.3.1) + sentry-rails (4.1.0) + rails (>= 5.0) + sentry-ruby (>= 4.1.0) + sentry-ruby (4.1.0) + concurrent-ruby + faraday (>= 1.0) spring (2.1.1) sprockets (4.0.2) concurrent-ruby (~> 1.0) @@ -212,6 +223,8 @@ DEPENDENCIES rails (~> 6.1.0) sass-rails (>= 6) selenium-webdriver + sentry-rails + sentry-ruby spring sqlite3 (~> 1.4) turbolinks (~> 5) diff --git a/app/controllers/catch_all_controller.rb b/app/controllers/catch_all_controller.rb index b3e1be0..617f6f7 100644 --- a/app/controllers/catch_all_controller.rb +++ b/app/controllers/catch_all_controller.rb @@ -1,4 +1,5 @@ class CatchAllController < ApplicationController def log_request + raise "random url called" end end diff --git a/config/initializers/sentry-init.rb b/config/initializers/sentry-init.rb new file mode 100644 index 0000000..2b0e684 --- /dev/null +++ b/config/initializers/sentry-init.rb @@ -0,0 +1,13 @@ +Sentry.init do |config| + config.dsn = 'https://21e1d6525c40490e99d912ddea3969a1@o213594.ingest.sentry.io/5562963' + config.breadcrumbs_logger = [:active_support_logger] + + # To activate performance monitoring, set one of these options. + # We recommend adjusting the value in production: + config.traces_sample_rate = 0.5 + # or + config.traces_sampler = lambda do |context| + true + end +end + diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..77bcf0f --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,15 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 0) do + +end