Skip to content

Commit

Permalink
Freeze config on AppSignal start (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
tombruijn authored Jul 24, 2024
1 parent 17933fd commit 46f23f1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changesets/freeze-config-after-appsignal-has-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: major
type: change
---

Freeze the config after AppSignal has started. Prevent the config from being modified after AppSignal has started to avoid the expectation that modifying the config after starting AppSignal has any effect.
1 change: 1 addition & 0 deletions lib/appsignal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def start
Appsignal::Probes.start if config[:enable_minutely_probes]

collect_environment_metadata
@config.freeze
else
internal_logger.info("Not starting, not active for #{config.env}")
end
Expand Down
11 changes: 11 additions & 0 deletions lib/appsignal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ def validate
end
end

# Deep freeze the config object so it cannot be modified during the runtime
# of the Ruby app.
#
# @api private
# @since 4.0.0
def freeze
super
config_hash.freeze
config_hash.transform_values(&:freeze)
end

private

def config_file
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/appsignal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@
Appsignal.start
end

it "freezes the config" do
Appsignal.start

expect_frozen_error do
Appsignal.config[:ignore_actions] << "my action"
end
expect_frozen_error do
Appsignal.config.config_hash[:ignore_actions] << "my action"
end
expect_frozen_error do
Appsignal.config.config_hash.merge!(:option => :value)
end
expect_frozen_error do
Appsignal.config[:ignore_actions] = "my action"
end
end

def expect_frozen_error(&block)
expect(&block).to raise_error(FrozenError)
end

context "when allocation tracking has been enabled" do
before do
Appsignal.config.config_hash[:enable_allocation_tracking] = true
Expand Down

0 comments on commit 46f23f1

Please sign in to comment.