-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from TruemarkDev/generator-for-sentry
Generator for Sentry
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |