From d72a8909ee2af34bd0abcd36911b76d881fae072 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sat, 10 Aug 2024 10:20:15 +0100 Subject: [PATCH] Chore: Fix and improve error reporting Because: - Browser errors were not being reported - Sentry has new performance profiling features we are This commit: - Load sentry browser from a CDN so it is loaded and available to report any errors before the rest of our JS - Adds a revision initializer so we can attach the specific release to Sentry errors. - Enable profile for the Sentry Ruby SDK. --- Gemfile | 1 + Gemfile.lock | 2 + app/javascript/error_tracking.js | 30 --------- app/javascript/main.js | 1 + app/javascript/src/sentry.js | 21 ++++++ app/views/layouts/_error_tracking.html.erb | 14 ---- app/views/layouts/admin/base.html.erb | 8 +-- app/views/layouts/application.html.erb | 8 +-- app/views/layouts/component_preview.html.erb | 4 +- app/views/layouts/errors.html.erb | 7 +- app/views/shared/_sentry.html.erb | 13 ++++ config/initializers/sentry.rb | 13 ++-- package.json | 1 - yarn.lock | 71 -------------------- 14 files changed, 60 insertions(+), 134 deletions(-) delete mode 100644 app/javascript/error_tracking.js create mode 100644 app/javascript/src/sentry.js delete mode 100644 app/views/layouts/_error_tracking.html.erb create mode 100644 app/views/shared/_sentry.html.erb diff --git a/Gemfile b/Gemfile index 088ac4e372..03c6eefab0 100644 --- a/Gemfile +++ b/Gemfile @@ -46,6 +46,7 @@ gem 'sentry-ruby', '~> 5.17' gem 'sentry-sidekiq', '~> 5.18' gem 'sidekiq', '~> 7.2' gem 'sidekiq-cron', '~> 1.12' +gem 'stackprof', '~> 0.2' gem 'stimulus-rails', '~> 1.3' gem 'turbo-rails', '~> 2.0' gem 'view_component', '~> 3.12' diff --git a/Gemfile.lock b/Gemfile.lock index b219a75d21..555561f563 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -522,6 +522,7 @@ GEM snaky_hash (2.0.1) hashie version_gem (~> 1.1, >= 1.1.1) + stackprof (0.2.26) statsd-ruby (1.5.0) stimulus-rails (1.3.3) railties (>= 6.0.0) @@ -635,6 +636,7 @@ DEPENDENCIES sidekiq (~> 7.2) sidekiq-cron (~> 1.12) simplecov (~> 0.22) + stackprof (~> 0.2) stimulus-rails (~> 1.3) turbo-rails (~> 2.0) vcr (~> 6.2) diff --git a/app/javascript/error_tracking.js b/app/javascript/error_tracking.js deleted file mode 100644 index 8639e0a3c1..0000000000 --- a/app/javascript/error_tracking.js +++ /dev/null @@ -1,30 +0,0 @@ -import * as Sentry from '@sentry/browser'; - -const { - SENTRY_DSN, currentUserSignedIn, currentUserId, currentUserEmail, -} = window; - -Sentry.init({ - dsn: SENTRY_DSN, - environment: 'production', - allowUrls: [ - /https?:\/\/(www\.)?theodinproject\.com/, - ], - ignoreErrors: [ - 'Possible side-effect in debug-evaluate', - 'Unexpected end of input', - 'Invalid or unexpected token', - 'missing ) after argument list', - ], -}); - -Sentry.configureScope((scope) => { - if (currentUserSignedIn) { - scope.setUser({ - id: currentUserId, - email: currentUserEmail, - }); - } -}); - -window.Sentry = Sentry; diff --git a/app/javascript/main.js b/app/javascript/main.js index fe42e84ba9..c41c64e56e 100644 --- a/app/javascript/main.js +++ b/app/javascript/main.js @@ -1,3 +1,4 @@ +import './src/sentry'; import './controllers'; import '@hotwired/turbo-rails'; import './src/custom_turbo_stream_actions'; diff --git a/app/javascript/src/sentry.js b/app/javascript/src/sentry.js new file mode 100644 index 0000000000..2052e3abee --- /dev/null +++ b/app/javascript/src/sentry.js @@ -0,0 +1,21 @@ +/* eslint-disable no-undef, no-unused-expressions */ + +const { + SENTRY_DSN, currentUserSignedIn, currentUserId, currentUsername, +} = window; + +window.Sentry = window.Sentry || {}; +window.Sentry && Sentry.onLoad && Sentry.onLoad(() => { + Sentry.init({ + dsn: SENTRY_DSN, + environment: 'production', + debug: true, + }); + + if (currentUserSignedIn) { + Sentry.setUser({ + id: currentUserId, + name: currentUsername, + }); + } +}); diff --git a/app/views/layouts/_error_tracking.html.erb b/app/views/layouts/_error_tracking.html.erb deleted file mode 100644 index 255226f214..0000000000 --- a/app/views/layouts/_error_tracking.html.erb +++ /dev/null @@ -1,14 +0,0 @@ - - <% if false %> - - - <% append_javascript_pack_tag 'error_tracking' %> -<% end %> diff --git a/app/views/layouts/admin/base.html.erb b/app/views/layouts/admin/base.html.erb index 946ec7ecf9..e3934e4a05 100644 --- a/app/views/layouts/admin/base.html.erb +++ b/app/views/layouts/admin/base.html.erb @@ -1,9 +1,6 @@ - <% content_for :error_tracking do %> - <%= render 'layouts/error_tracking' %> - <% end %> <%= content_for(:title) || 'Admin | The Odin Project' %> @@ -26,8 +23,9 @@ - <%= stylesheet_link_tag 'application', 'main', media: 'all', 'data-turbo-track': 'reload' %> - <%= javascript_include_tag 'main', 'data-turbo-track': 'reload', defer: true %> + <%= stylesheet_link_tag 'application', 'main', media: 'all', data: { turbo_track: 'reload' } %> + <%= render 'shared/sentry' %> + <%= javascript_include_tag 'main', defer: true, data: { turbo_track: 'reload' } %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2662bcb22b..0f9dcab630 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,9 +1,6 @@ - <% content_for :error_tracking do %> - <%= render 'layouts/error_tracking' %> - <% end %> <%= content_for(:title) || 'Your Career in Web Development Starts Here | The Odin Project' %> @@ -24,9 +21,10 @@ - <%= stylesheet_link_tag 'application', 'main', media: 'all', 'data-turbo-track': 'reload' %> + <%= stylesheet_link_tag 'application', 'main', media: 'all', data: { turbo_track: 'reload' } %> + <%= render 'shared/sentry' %> <%= render 'shared/analytics' %> - <%= javascript_include_tag 'main', 'data-turbo-track': 'reload', type: 'module' %> + <%= javascript_include_tag 'main', defer: true, data: { turbo_track: 'reload' } %> diff --git a/app/views/layouts/component_preview.html.erb b/app/views/layouts/component_preview.html.erb index 5a74f6c418..4f8296566c 100644 --- a/app/views/layouts/component_preview.html.erb +++ b/app/views/layouts/component_preview.html.erb @@ -5,8 +5,8 @@ Component Previews - <%= stylesheet_link_tag 'application', 'main' media: 'all', 'data-turbo-track': 'reload' %> - <%= javascript_include_tag 'main', 'data-turbo-track': 'reload', defer: true %> + <%= stylesheet_link_tag 'application', 'main', media: 'all', data: { turbo_track: 'reload' } %> + <%= javascript_include_tag 'main', defer: true, data: { turbo_track: 'reload' } %>
- <%= stylesheet_link_tag 'application', 'main', media: 'all', 'data-turbo-track': 'reload' %> - <%= javascript_include_tag 'main', 'data-turbo-track': 'reload', defer: true %> + + <%= stylesheet_link_tag 'application', 'main', media: 'all', data: { turbo_track: 'reload' } %> + <%= render 'shared/sentry' %> + <%= render 'shared/analytics' %> + <%= javascript_include_tag 'main', defer: true, data: { turbo_track: 'reload' } %>
diff --git a/app/views/shared/_sentry.html.erb b/app/views/shared/_sentry.html.erb new file mode 100644 index 0000000000..d41eabc9df --- /dev/null +++ b/app/views/shared/_sentry.html.erb @@ -0,0 +1,13 @@ +<% if Rails.env.production? %> + + + +<% end %> diff --git a/config/initializers/sentry.rb b/config/initializers/sentry.rb index d5cce8fefb..6c5fde17b1 100644 --- a/config/initializers/sentry.rb +++ b/config/initializers/sentry.rb @@ -1,13 +1,18 @@ Sentry.init do |config| config.dsn = ENV['SENTRY_DSN'] - config.breadcrumbs_logger = [:active_support_logger] - config.enabled_environments = %w[production] + config.breadcrumbs_logger = %i[active_support_logger http_logger] + config.send_default_pii = true + + # enable tracing + config.traces_sample_rate = 0.1 + + # enable profiling + # this is relative to traces_sample_rate + config.profiles_sample_rate = 1.0 filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters) config.before_send = lambda { |event, _| filter.filter(event.to_hash) } - - config.send_default_pii = true end diff --git a/package.json b/package.json index aa3b12d08d..15e3b3fd9f 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,6 @@ "@hotwired/stimulus": "^3.2.2", "@hotwired/turbo-rails": "^8.0.2", "@rails/request.js": "^0.0.9", - "@sentry/browser": "^7.108.0", "@stimulus/polyfills": "^2.0.0", "@tailwindcss/forms": "^0.5.7", "@tailwindcss/typography": "^0.5.10", diff --git a/yarn.lock b/yarn.lock index 76f8bc10c6..3d11fe1883 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1259,77 +1259,6 @@ resolved "https://registry.yarnpkg.com/@rails/request.js/-/request.js-0.0.9.tgz#89e2a575405dc07eb8a9b3d2fe04289e1f057cd0" integrity sha512-VleYUyrA3rwKMvYnz7MI9Ada85Vekjb/WVz7NuGgDO24Y3Zy9FFSpDMQW+ea/tlftD+CdX/W/sUosRA9/HkDOQ== -"@sentry-internal/feedback@7.108.0": - version "7.108.0" - resolved "https://registry.yarnpkg.com/@sentry-internal/feedback/-/feedback-7.108.0.tgz#7033352abd304f1383ec47640e056a0dfd5132b7" - integrity sha512-8JcgZEnk1uWrXJhsd3iRvFtEiVeaWOEhN0NZwhwQXHfvODqep6JtrkY1yCIyxbpA37aZmrPc2JhyotRERGfUjg== - dependencies: - "@sentry/core" "7.108.0" - "@sentry/types" "7.108.0" - "@sentry/utils" "7.108.0" - -"@sentry-internal/replay-canvas@7.108.0": - version "7.108.0" - resolved "https://registry.yarnpkg.com/@sentry-internal/replay-canvas/-/replay-canvas-7.108.0.tgz#641133c19c0e1c423617b8d791f53d6cd0b0a862" - integrity sha512-R5tvjGqWUV5vSk0N1eBgVW7wIADinrkfDEBZ9FyKP2mXHBobsyNGt30heJDEqYmVqluRqjU2NuIRapsnnrpGnA== - dependencies: - "@sentry/core" "7.108.0" - "@sentry/replay" "7.108.0" - "@sentry/types" "7.108.0" - "@sentry/utils" "7.108.0" - -"@sentry-internal/tracing@7.108.0": - version "7.108.0" - resolved "https://registry.yarnpkg.com/@sentry-internal/tracing/-/tracing-7.108.0.tgz#d1e660701fb860cfae72b6ebfa8fb267533421fa" - integrity sha512-zuK5XsTsb+U+hgn3SPetYDAogrXsM16U/LLoMW7+TlC6UjlHGYQvmX3o+M2vntejoU1QZS8m1bCAZSMWEypAEw== - dependencies: - "@sentry/core" "7.108.0" - "@sentry/types" "7.108.0" - "@sentry/utils" "7.108.0" - -"@sentry/browser@^7.108.0": - version "7.108.0" - resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.108.0.tgz#b95810bb6572b63781f253615896f5afb1a3a5c0" - integrity sha512-FNpzsdTvGvdHJMUelqEouUXMZU7jC+dpN7CdT6IoHVVFEkoAgrjMVUhXZoQ/dmCkdKWHmFSQhJ8Fm6V+e9Aq0A== - dependencies: - "@sentry-internal/feedback" "7.108.0" - "@sentry-internal/replay-canvas" "7.108.0" - "@sentry-internal/tracing" "7.108.0" - "@sentry/core" "7.108.0" - "@sentry/replay" "7.108.0" - "@sentry/types" "7.108.0" - "@sentry/utils" "7.108.0" - -"@sentry/core@7.108.0": - version "7.108.0" - resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.108.0.tgz#a27e8d6f85f59c5730ce86071474f15ac899fde0" - integrity sha512-I/VNZCFgLASxHZaD0EtxZRM34WG9w2gozqgrKGNMzAymwmQ3K9g/1qmBy4e6iS3YRptb7J5UhQkZQHrcwBbjWQ== - dependencies: - "@sentry/types" "7.108.0" - "@sentry/utils" "7.108.0" - -"@sentry/replay@7.108.0": - version "7.108.0" - resolved "https://registry.yarnpkg.com/@sentry/replay/-/replay-7.108.0.tgz#baa679bd19b4e3729e607d3f84cff5048aeb3415" - integrity sha512-jo8fDOzcZJclP1+4n9jUtVxTlBFT9hXwxhAMrhrt70FV/nfmCtYQMD3bzIj79nwbhUtFP6pN39JH1o7Xqt1hxQ== - dependencies: - "@sentry-internal/tracing" "7.108.0" - "@sentry/core" "7.108.0" - "@sentry/types" "7.108.0" - "@sentry/utils" "7.108.0" - -"@sentry/types@7.108.0": - version "7.108.0" - resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.108.0.tgz#5ceb959c4dabe511fc441fec8c2465f2d624900f" - integrity sha512-bKtHITmBN3kqtqE5eVvL8mY8znM05vEodENwRpcm6TSrrBjC2RnwNWVwGstYDdHpNfFuKwC8mLY9bgMJcENo8g== - -"@sentry/utils@7.108.0": - version "7.108.0" - resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.108.0.tgz#0231042956aed2ef35809891592238530349dfd9" - integrity sha512-a45yEFD5qtgZaIFRAcFkG8C8lnDzn6t4LfLXuV4OafGAy/3ZAN3XN8wDnrruHkiUezSSANGsLg3bXaLW/JLvJw== - dependencies: - "@sentry/types" "7.108.0" - "@stimulus/polyfills@^2.0.0": version "2.0.0" resolved "https://registry.yarnpkg.com/@stimulus/polyfills/-/polyfills-2.0.0.tgz#64b3e247c762330f80d88e993d1d26b24e3c13b1"