Skip to content

Commit

Permalink
Load RACK_ENV or RAILS_ENV in diagnose report (#923)
Browse files Browse the repository at this point in the history
When the RACK_ENV or RAILS_ENV var is set, use it to configure AppSignal
in the diagnose report CLI. Makes it easier to run the diagnose report
in a production environment that uses the RACK_ENV or RAILS_ENV var to
configure the app env.

RACK_ENV is loaded first, then RAILS_ENV.
  • Loading branch information
tombruijn authored Mar 1, 2023
1 parent 2ceeddb commit 8e67159
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changesets/load-rails_env-in-diagnose-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "change"
---

Configure AppSignal with the RACK_ENV or RAILS_ENV environment variable in diagnose CLI, if present. Makes it easier to run the diagnose CLI in production, without having to always specify the environment with the `--environment` CLI option.
2 changes: 1 addition & 1 deletion lib/appsignal/cli/diagnose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def configure_appsignal(options)

Appsignal.config = Appsignal::Config.new(
current_path,
options[:environment],
options.fetch(:environment, ENV.fetch("RACK_ENV", ENV.fetch("RAILS_ENV", nil))),
initial_config
)
Appsignal.config.write_to_environment
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/appsignal/cli/diagnose_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,39 @@ def dont_accept_prompt_to_send_diagnostics_report
end
end

context "when the source is the RACK_ENV env variable", :send_report => :no_cli_option do
let(:config) { project_fixture_config("rack_env") }
let(:options) { {} }
before do
ENV["RACK_ENV"] = "rack_env"
run
end
after { ENV.delete("RACK_ENV") }

it "outputs the RACK_ENV variable value" do
expect(output).to include(
%(environment: "rack_env" (Loaded from: initial)\n)
)
end
end

context "when the source is the RAILS_ENV env variable", :send_report => :no_cli_option do
let(:config) { project_fixture_config("rails_env") }
let(:options) { {} }
before do
ENV.delete("RACK_ENV")
ENV["RAILS_ENV"] = "rails_env"
run
end
after { ENV.delete("RAILS_ENV") }

it "outputs the RAILS_ENV variable value" do
expect(output).to include(
%(environment: "rails_env" (Loaded from: initial)\n)
)
end
end

context "when the source is multiple sources" do
let(:options) { { :environment => "development" } }
before do
Expand Down
6 changes: 6 additions & 0 deletions spec/support/fixtures/projects/valid/config/appsignal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ old_config_mixed_with_new_config:
"REQUEST_METHOD", "REQUEST_URI", "SERVER_NAME", "SERVER_PORT",
"SERVER_PROTOCOL", "HTTP_USER_AGENT"
]

rack_env:
<<: *defaults

rails_env:
<<: *defaults

0 comments on commit 8e67159

Please sign in to comment.