Skip to content

Commit

Permalink
Add Ruby config file option to installer
Browse files Browse the repository at this point in the history
To replace the YAML config file people need to know they can configure
the gem with a Ruby config file too. Add this option to the installer so
people can choose this config option when installing AppSignal in their
application.

I've made it the first listed method as it's the most flexible
configuration method. The YAML file option has the "legacy" label next
to it now to discourage people from using it.
  • Loading branch information
tombruijn committed Nov 5, 2024
1 parent 4c9017d commit 0d2e2bd
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .changesets/add-ruby-config-file-method-to-installer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: add
---

Add the `config/appsignal.rb` Ruby config file method to installer, `appsignal install`.
58 changes: 46 additions & 12 deletions lib/appsignal/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,35 +229,49 @@ def install_for_unknown_framework(config)
done_notice
end

def configure(config, environments, name_overwritten)
def configure(config, environments, name_overwritten) # rubocop:disable Metrics/AbcSize
install_for_capistrano

ENV["APPSIGNAL_APP_ENV"] = "development"

puts "How do you want to configure AppSignal?"
puts " (1) a config file"
puts " (2) environment variables"
puts " (1) a Ruby config file"
puts " (2) a YAML config file (legacy)"
puts " (3) environment variables"
puts
puts " See our docs for information on the different configuration methods: "
puts " https://docs.appsignal.com/ruby/configuration.html"
puts
loop do
print " Choose (1/2): "
loop do # rubocop:disable Metrics/BlockLength
print " Choose (1-3): "
case ask_for_input
when "1"
puts
print "Writing config file"
print "Writing Ruby config file"
periods
puts
puts colorize " Config file written to config/appsignal.yml", :green
write_config_file(
write_ruby_config_file(
:push_api_key => config[:push_api_key],
:app_name => config[:name],
:environments => environments
)
puts colorize " Config file written to config/appsignal.rb", :green
puts
break
when "2"
puts
print "Writing YAML config file"
periods
puts
write_yaml_config_file(
:push_api_key => config[:push_api_key],
:app_name => config[:name],
:environments => environments
)
puts colorize " Config file written to config/appsignal.yml", :green
puts
break
when "3"
ENV["APPSIGNAL_ACTIVE"] = "true"
ENV["APPSIGNAL_PUSH_API_KEY"] = config[:push_api_key]
ENV["APPSIGNAL_APP_NAME"] = config[:name]
Expand Down Expand Up @@ -325,17 +339,37 @@ def rails_environments
).map { |o| File.basename(o, ".rb") }.sort - EXCLUDED_ENVIRONMENTS
end

def write_config_file(data)
filename = File.join(
def write_ruby_config_file(data)
template = File.join(
File.dirname(__FILE__),
"../../../resources/appsignal.rb.erb"
)
write_config_file(
template,
File.join(Dir.pwd, "config/appsignal.rb"),
data
)
end

def write_yaml_config_file(data)
template = File.join(
File.dirname(__FILE__),
"../../../resources/appsignal.yml.erb"
)
file_contents = File.read(filename)
write_config_file(
template,
File.join(Dir.pwd, "config/appsignal.yml"),
data
)
end

def write_config_file(template_path, path, data)
file_contents = File.read(template_path)
template = ERB.new(file_contents, :trim_mode => "-")
config = template.result(OpenStruct.new(data).instance_eval { binding })

FileUtils.mkdir_p(File.join(Dir.pwd, "config"))
File.write(File.join(Dir.pwd, "config/appsignal.yml"), config)
File.write(path, config)
end

def new_config
Expand Down
32 changes: 32 additions & 0 deletions resources/appsignal.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# AppSignal Ruby gem configuration
# Visit our documentation for a list of all available configuration options.
# https://docs.appsignal.com/ruby/configuration/options.html
Appsignal.configure do |config|
config.activate_if_environment(<%= environments.map(&:inspect).join(", ") %>)
config.name = <%= app_name.inspect %>
# The application's Push API key
# We recommend removing this line and setting this option with the
# APPSIGNAL_PUSH_API_KEY environment variable instead.
# https://docs.appsignal.com/ruby/configuration/options.html#option-push_api_key
config.push_api_key = "<%= push_api_key %>"

# Actions that should not be monitored by AppSignal
# config.ignore_actions << "ApplicationController#isup"

# Errors that should not be recorded by AppSignal
# For more information see our docs:
# https://docs.appsignal.com/ruby/configuration/ignore-errors.html
# config.ignore_errors += [
# "Exception",
# "NoMemoryError",
# "ScriptError",
# "LoadError",
# "NotImplementedError",
# "SyntaxError",
# "SecurityError",
# "SignalException",
# "Interrupt",
# "SystemExit",
# "SystemStackError"
# ]
end
Loading

0 comments on commit 0d2e2bd

Please sign in to comment.