diff --git a/.changesets/check-argument-type-of-breadcrumb-metadata.md b/.changesets/check-argument-type-of-breadcrumb-metadata.md new file mode 100644 index 000000000..501d32b61 --- /dev/null +++ b/.changesets/check-argument-type-of-breadcrumb-metadata.md @@ -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 +) +``` diff --git a/lib/appsignal/transaction.rb b/lib/appsignal/transaction.rb index 402f97648..ff69bf239 100644 --- a/lib/appsignal/transaction.rb +++ b/lib/appsignal/transaction.rb @@ -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, diff --git a/spec/lib/appsignal/transaction_spec.rb b/spec/lib/appsignal/transaction_spec.rb index c825be0f7..c25cd50ee 100644 --- a/spec/lib/appsignal/transaction_spec.rb +++ b/spec/lib/appsignal/transaction_spec.rb @@ -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