Skip to content

Commit

Permalink
Generator for Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
coolprobn committed May 31, 2024
1 parent 2523fe3 commit 0185bd1
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][])
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ The boring generator introduces following generators:
- Install VCR: `rails generate boring:vcr:install --testing_framework=<testing_framework> --stubbing_libraries=<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=<breadcrumbs_logger_options>`

## Screencasts

Expand Down
48 changes: 48 additions & 0 deletions lib/generators/boring/sentry/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/generators/boring/sentry/install/templates/sentry.rb
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions test/generators/sentry_install_generator_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0185bd1

Please sign in to comment.