Skip to content

Commit

Permalink
Log error when sample data is invalid type (#992)
Browse files Browse the repository at this point in the history
Log an error when a value other than an Array or Hash is given to sample
data to help debug why the sample data is not appearing.

Related Slack conversation:
https://appsignal.slack.com/archives/CNPP953E2/p1693295505307779
  • Loading branch information
tombruijn authored Aug 29, 2023
1 parent 5d2b6bb commit a42da92
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
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

0 comments on commit a42da92

Please sign in to comment.