Skip to content
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
12 changes: 10 additions & 2 deletions lib/bugsnag/integrations/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 @@ -59,6 +60,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 All @@ -84,15 +86,21 @@ def sanitize_filter_hash(filter_hash, depth = 0)
# @param value [Object] the filter value
# @param depth [Integer] the current filter depth
#
# @return [Array, Hash, String] the sanitized value
# @return [Array, Hash, String, nil] the sanitized value
def sanitize_filter_value(value, depth)
depth += 1
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)
elsif value.nil?
nil
else
'?'
end
Expand Down
35 changes: 31 additions & 4 deletions spec/integrations/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def require(path)
end

it "adds a JSON string of filter data" do
command["filter"] = {"a" => 1, "b" => 2, "$or" => [{"c" => 3}, {"d" => 4}]}
command["filter"] = {"a" => 1, "b" => 2, "$or" => [{"c" => 3}, {"d" => nil}]}
expect(subscriber).to receive(:pop_command).with("123456").and_return(command)
expect(Bugsnag).to receive(:leave_breadcrumb).with(
"Mongo query #{event_name}",
Expand All @@ -167,7 +167,28 @@ def require(path)
:request_id => "123456",
:duration => "123.456",
:collection => "collection_name_command",
:filter => '{"a":"?","b":"?","$or":[{"c":"?"},{"d":"?"}]}'
:filter => '{"a":"?","b":"?","$or":[{"c":"?"},{"d":null}]}'
},
"process",
:auto
)
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
Expand All @@ -188,11 +209,17 @@ def require(path)
expect(subscriber.send(:sanitize_filter_value, 523, 0)).to eq('?')
expect(subscriber.send(:sanitize_filter_value, "string", 0)).to eq('?')
expect(subscriber.send(:sanitize_filter_value, true, 0)).to eq('?')
expect(subscriber.send(:sanitize_filter_value, nil, 0)).to eq('?')
expect(subscriber.send(:sanitize_filter_value, nil, 0)).to eq(nil)
end

it "is recursive and iterative for array values" do
expect(subscriber.send(:sanitize_filter_value, [1, [2, [3]]], 0)).to eq(['?', ['?', ['?']]])
expect(subscriber.send(:sanitize_filter_value, [1, [2, [3], nil]], 0)).to eq(['?', ['?', ['?'], nil]])
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
Expand Down