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

Mongo breadcrumb: Summarize the length of long arrays #799

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion lib/bugsnag/integrations/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MongoBreadcrumbSubscriber
MONGO_EVENT_PREFIX = "mongo."
MONGO_COMMAND_KEY = :bugsnag_mongo_commands
MAX_FILTER_DEPTH = 5
MAX_ARRAY_LENGTH = 3

##
# Listens to the 'started' event, storing the command for later usage
Expand Down Expand Up @@ -60,6 +61,7 @@ def leave_mongo_breadcrumb(event_name, event)
filter = sanitize_filter_hash(command["filter"])
meta_data[:filter] = JSON.dump(filter)
end
meta_data[:sort] = JSON.dump(command["sort"]) unless command["sort"].nil?
end
meta_data[:message] = event.message if defined?(event.message)

Expand Down Expand Up @@ -91,7 +93,11 @@ def sanitize_filter_value(value, depth)
if depth >= MAX_FILTER_DEPTH
'[MAX_FILTER_DEPTH_REACHED]'
elsif value.is_a?(Array)
value.map { |array_value| sanitize_filter_value(array_value, depth) }
if value.size > MAX_ARRAY_LENGTH && value.none? { |v| v.is_a?(Hash) || v.is_a?(Array) }
["LENGTH=#{value.size}"]
else
value.map { |array_value| sanitize_filter_value(array_value, depth) }
end
elsif value.is_a?(Hash)
sanitize_filter_hash(value, depth)
else
Expand Down
27 changes: 27 additions & 0 deletions spec/integrations/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ def require(path)
)
subscriber.send(:leave_mongo_breadcrumb, event_name, event)
end

it "adds a JSON string of sort data" do
command["sort"] = {"a" => 1, "b" => -1}
expect(subscriber).to receive(:pop_command).with("123456").and_return(command)
expect(Bugsnag).to receive(:leave_breadcrumb).with(
"Mongo query #{event_name}",
{
:event_name => "mongo.#{event_name}",
:command_name => "command",
:database_name => "database",
:operation_id => "1234567890",
:request_id => "123456",
:duration => "123.456",
:collection => "collection_name_command",
:sort => '{"a":1,"b":-1}'
},
"process",
:auto
)
subscriber.send(:leave_mongo_breadcrumb, event_name, event)
end
end
end

Expand All @@ -195,6 +216,12 @@ def require(path)
expect(subscriber.send(:sanitize_filter_value, [1, [2, [3]]], 0)).to eq(['?', ['?', ['?']]])
end

it "returns LENGTH= for long arrays" do
actual = subscriber.send(:sanitize_filter_value, [1, [2, 2, 2], 3, 4, [5, 5, 5, 5], 6], 0)
expected = ['?', %w[? ? ?], '?', '?', ['LENGTH=4'], '?']
expect(actual).to eq(expected)
end

it "calls #sanitize_filter_hash for hash values" do
expect(subscriber).to receive(:sanitize_filter_hash).with({:a => 1}, 1)
subscriber.send(:sanitize_filter_value, {:a => 1}, 0)
Expand Down