Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log error when sample data is invalid type #992

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changesets/log-error-when-sample-data-is-invalid-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "change"
---

Log an error when sample data is of an invalid type. Accepted types are Array and Hash. If any other types are given, it will log an error to the `appsignal.log` file.
9 changes: 8 additions & 1 deletion lib/appsignal/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,14 @@ def set_metadata(key, value)
end

def set_sample_data(key, data)
return unless key && data && (data.is_a?(Array) || data.is_a?(Hash))
return unless key && data

if !data.is_a?(Array) && !data.is_a?(Hash)
Appsignal.logger.error(
"Invalid sample data for '#{key}'. Value is not an Array or Hash: '#{data.inspect}'"
)
return
end

@ext.set_sample_data(
key.to_s,
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/appsignal/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ def current_transaction
transaction.set_sample_data("params", "string")

expect(transaction.to_h["sample_data"]).to eq({})
expect(log_contents(log)).to contains_log :error,
%(Invalid sample data for 'params'. Value is not an Array or Hash: '"string"')
end
end

Expand Down