diff --git a/CHANGELOG.md b/CHANGELOG.md index cbb98316..f75634d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Adds Pronto Generator with Gihub Action. ([@TheZero0-ctrl][]) * Adds Doorkeeper Generator with Devise. ([@TheZero0-ctrl][]) * Adds Avo generator. ([@mausamp][]) +* Adds Sentry generator. ([@mausamp][]) ## 0.13.0 (March 26th, 2024) * Adds Letter Opener generator. ([@coolprobn][]) diff --git a/README.md b/README.md index e3fb6c24..ee4a5875 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ The boring generator introduces following generators: - Install VCR: `rails generate boring:vcr:install --testing_framework= --stubbing_libraries=` - Install Avo: `rails generate boring:avo:install` - Install Doorkeeper with devise: `rails generate boring:devise:doorkeeper:install` +- Install Sentry: `rails generate boring:sentry:install --use_env_variable --breadcrumbs_logger=` ## Screencasts diff --git a/lib/generators/boring/sentry/install/install_generator.rb b/lib/generators/boring/sentry/install/install_generator.rb new file mode 100644 index 00000000..932b987b --- /dev/null +++ b/lib/generators/boring/sentry/install/install_generator.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module Boring + module Sentry + class InstallGenerator < Rails::Generators::Base + source_root File.expand_path("templates", __dir__) + desc 'Adds Sentry to the app' + + class_option :use_env_variable, type: :boolean, aliases: '-ev', + desc: 'Use ENV variable for Sentry. By default Rails credentials will be used.' + class_option :breadcrumbs_logger, type: :array, aliases: '-bl', default: [:active_support_logger, :http_logger], + desc: 'Set the breadcrumbs logger. By default [:active_support_logger, :http_logger] will be used.' + + def add_sentry_gems + say 'Adding Sentry gem', :green + + Bundler.with_unbundled_env do + run 'bundle add sentry-ruby sentry-rails' + end + end + + def configure_sentry_gem + say 'Configuring Sentry gem', :green + + @sentry_dsn_key = sentry_dsn_key + @breadcrumbs_logger_options = options[:breadcrumbs_logger].map(&:to_sym) + + template 'sentry.rb', 'config/initializers/sentry.rb' + + show_alert_message + end + + private + + def sentry_dsn_key + if options[:use_env_variable] + "ENV['SENTRY_DSN_KEY']" + else + "Rails.application.credentials.dig(:sentry, :dsn_key)" + end + end + + def show_alert_message + say "❗️❗️\nThe DSN key for Sentry will be used from `#{sentry_dsn_key}`. You can change this value if it doesn't match with your app.\n", :yellow + end + end + end +end diff --git a/lib/generators/boring/sentry/install/templates/sentry.rb b/lib/generators/boring/sentry/install/templates/sentry.rb new file mode 100644 index 00000000..32c95936 --- /dev/null +++ b/lib/generators/boring/sentry/install/templates/sentry.rb @@ -0,0 +1,7 @@ +Sentry.init do |config| + config.dsn = <%= @sentry_dsn_key %> + # enable performance monitoring + config.enable_tracing = true + # get breadcrumbs from logs + config.breadcrumbs_logger = <%= @breadcrumbs_logger_options %> +end \ No newline at end of file diff --git a/test/generators/sentry_install_generator_test.rb b/test/generators/sentry_install_generator_test.rb new file mode 100644 index 00000000..4a2ca49e --- /dev/null +++ b/test/generators/sentry_install_generator_test.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'generators/boring/sentry/install/install_generator' + +class SentryInstallGeneratorTest < Rails::Generators::TestCase + tests Boring::Sentry::InstallGenerator + setup :build_app + teardown :teardown_app + + include GeneratorHelper + include ActiveSupport::Testing::Isolation + + def destination_root + app_path + end + + def test_should_configure_sentry_gem + Dir.chdir(app_path) do + quietly { run_generator } + + assert_gem 'sentry-ruby' + assert_gem 'sentry-rails' + + assert_file 'config/initializers/sentry.rb' do |content| + assert_match(/config.dsn = Rails.application.credentials.dig\(:sentry, :dsn_key\)/, content) + assert_match(/config.breadcrumbs_logger = \[:active_support_logger, :http_logger\]\n/, content) + end + end + end + + def test_should_configure_dsn_key_as_env_variable + Dir.chdir(app_path) do + quietly { run_generator [destination_root, '--use_env_variable'] } + + assert_gem 'sentry-ruby' + assert_gem 'sentry-rails' + + assert_file 'config/initializers/sentry.rb' do |content| + assert_match(/config.dsn = ENV\['SENTRY_DSN_KEY'\]\n/, content) + end + end + end + + def test_should_configure_custom_breadcrumbs_logger + Dir.chdir(app_path) do + quietly { run_generator [destination_root, "--breadcrumbs_logger=http_logger"]} + + assert_gem 'sentry-ruby' + assert_gem 'sentry-rails' + + assert_file 'config/initializers/sentry.rb' do |content| + assert_match(/config.breadcrumbs_logger = \[:http_logger\]\n/, content) + end + end + end +end