Skip to content

Commit

Permalink
Do not warn about nil being set as SampleData
Browse files Browse the repository at this point in the history
The default value is nil so it would always log this warning when the
value gets set for the first time.
  • Loading branch information
tombruijn committed Aug 23, 2024
1 parent 9d39995 commit 0a658e5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: fix
---

Do not log a warning for `nil` data being added as sample data, but silently ignore it because we don't support it.
16 changes: 10 additions & 6 deletions lib/appsignal/sample_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def set_empty_value!
end

def value
value = nil
value = UNSET_VALUE
@blocks.map! do |block_or_value|
new_value =
if block_or_value.respond_to?(:call)
Expand Down Expand Up @@ -63,6 +63,8 @@ def empty?

private

UNSET_VALUE = nil

# Method called by `dup` and `clone` to create a duplicate instance.
# Make sure the `@blocks` variable is also properly duplicated.
def initialize_copy(original)
Expand All @@ -81,11 +83,13 @@ def accepted_type?(value)

def merge_values(value_original, value_new)
unless value_new.instance_of?(value_original.class)
Appsignal.internal_logger.warn(
"The sample data '#{@key}' changed type from " \
"'#{value_original.class}' to '#{value_new.class}'. " \
"These types can not be merged. Using new '#{value_new.class}' type."
)
unless value_original == UNSET_VALUE
Appsignal.internal_logger.warn(
"The sample data '#{@key}' changed type from " \
"'#{value_original.class}' to '#{value_new.class}'. " \
"These types can not be merged. Using new '#{value_new.class}' type."
)
end
return value_new
end

Expand Down
27 changes: 26 additions & 1 deletion spec/lib/appsignal/sample_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@

describe "#add" do
it "sets the given value" do
data.add(:key1 => "value 1")
logs =
capture_logs do
data.add(:key1 => "value 1")
end

expect(data.value).to eq(:key1 => "value 1")

expect(logs).to_not contains_log(
:error,
"Sample data 'data_key': Unsupported data type 'NilClass' received: nil"
)
end

it "adds the given value with the block being leading" do
Expand All @@ -14,6 +22,23 @@
expect(data.value).to eq(:key2 => "value 2")
end

it "doesn't add nil to the data" do
logs =
capture_logs do
data.add([1])
data.add(nil)
data.add { nil }
data.add([2, 3])
end

expect(data.value).to eq([1, 2, 3])
expect(logs).to contains_log(
:error,
"Sample data 'data_key': Unsupported data type 'NilClass' received: nil"
)
expect(logs).to_not contains_log(:warn, "The sample data 'data_key' changed type")
end

it "merges multiple values" do
data.add(:key1 => "value 1")
data.add(:key2 => "value 2")
Expand Down

0 comments on commit 0a658e5

Please sign in to comment.