Skip to content

Commit

Permalink
Fix issue with invalid breadcrumb metadata (#974)
Browse files Browse the repository at this point in the history
We had a report of breadcrumbs not working when the metadata was
invalid. This required some manual intervention from us to fix the data.
Avoid this occurring in the future by not allowing any other metadata
than a Hash object.

Closes appsignal/support#269
  • Loading branch information
tombruijn authored Jun 27, 2023
1 parent e5e79d9 commit 9cdee8a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .changesets/check-argument-type-of-breadcrumb-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
bump: "patch"
type: "fix"
---

Log error when the argument type of the breadcrumb metadata is invalid. This metadata argument should be a Hash, and other values are not supported. More information can be found in the [Ruby gem breadcrumb documentation](https://docs.appsignal.com/ruby/instrumentation/breadcrumbs.html).

```ruby
Appsignal.add_breadcrumb(
"breadcrumb category",
"breadcrumb action",
"some message",
{ :metadata_key => "some value" } # This needs to be a Hash object
)
```
6 changes: 6 additions & 0 deletions lib/appsignal/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ def set_tags(given_tags = {})
# @see https://docs.appsignal.com/ruby/instrumentation/breadcrumbs.html
# Breadcrumb reference
def add_breadcrumb(category, action, message = "", metadata = {}, time = Time.now.utc)
unless metadata.is_a? Hash
Appsignal.logger.error "add_breadcrumb: Cannot add breadcrumb. " \
"The given metadata argument is not a Hash."
return
end

@breadcrumbs.push(
:time => time.to_i,
:category => category,
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/appsignal/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,19 @@ def current_transaction
expect(breadcrumb["metadata"]).to eq({})
end
end

context "with metadata argument that's not a Hash" do
it "does not add the breadcrumb and logs and error" do
transaction.add_breadcrumb("category", "action", "message", "invalid metadata")
transaction.sample_data

expect(transaction.to_h["sample_data"]["breadcrumbs"]).to be_empty
expect(log_contents(log)).to contains_log(
:error,
"add_breadcrumb: Cannot add breadcrumb. The given metadata argument is not a Hash."
)
end
end
end

describe "#set_action" do
Expand Down

0 comments on commit 9cdee8a

Please sign in to comment.