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

Add max_nesting of 10 to breadcrumbs data serialization #2583

Merged
merged 1 commit into from
Mar 17, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- Add new sidekiq config `report_only_dead_jobs` ([#2581](https://github.com/getsentry/sentry-ruby/pull/2581))
- Add `max_nesting` of 10 to breadcrumbs data serialization ([#2583](https://github.com/getsentry/sentry-ruby/pull/2583))

### Bug Fixes

Expand Down
3 changes: 2 additions & 1 deletion sentry-ruby/lib/sentry/breadcrumb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Sentry
class Breadcrumb
MAX_NESTING = 10
DATA_SERIALIZATION_ERROR_MESSAGE = "[data were removed due to serialization issues]"

# @return [String, nil]
Expand Down Expand Up @@ -60,7 +61,7 @@ def level=(level) # needed to meet the Sentry spec

def serialized_data
begin
::JSON.parse(::JSON.generate(@data))
::JSON.parse(::JSON.generate(@data, max_nesting: MAX_NESTING))
rescue Exception => e
Sentry.logger.debug(LOGGER_PROGNAME) do
<<~MSG
Expand Down
26 changes: 25 additions & 1 deletion sentry-ruby/spec/sentry/breadcrumb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
require "spec_helper"

RSpec.describe Sentry::Breadcrumb do
let(:stringio) { StringIO.new }

before do
perform_basic_setup
perform_basic_setup do |config|
config.logger = ::Logger.new(stringio)
end
end

let(:crumb) do
Expand Down Expand Up @@ -77,6 +81,16 @@
)
end

let(:very_deep_crumb) do
data = [[[[[ { a: [{ b: [[{ c: 4 }]] }] }]]]]]

Sentry::Breadcrumb.new(
category: "cow",
message: "I cause too much recursion",
data: data
)
end

it "serializes data correctly" do
result = crumb.to_hash

Expand All @@ -91,6 +105,16 @@
expect(result[:category]).to eq("baz")
expect(result[:message]).to eq("I cause issues")
expect(result[:data]).to eq("[data were removed due to serialization issues]")
expect(stringio.string).to match(/can't serialize breadcrumb data because of error: nesting of 10 is too deep/)
end

it "rescues data serialization issue for extremely nested data and ditch the data" do
result = very_deep_crumb.to_hash

expect(result[:category]).to eq("cow")
expect(result[:message]).to eq("I cause too much recursion")
expect(result[:data]).to eq("[data were removed due to serialization issues]")
expect(stringio.string).to match(/can't serialize breadcrumb data because of error: nesting of 10 is too deep/)
end
end
end
Loading