Skip to content

Commit

Permalink
Breadcrumbs/Mongo integration: Minor test and efficieny improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec committed Jan 10, 2019
1 parent 3ccc0e8 commit be506d3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion features/fixtures/rails4/app/app/models/mongo_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ class MongoModel
include Mongoid::Document

field :string_field, type: String
field :numeric_field, type: Numeric
field :numeric_field, type: Integer
end
2 changes: 1 addition & 1 deletion features/fixtures/rails5/app/app/models/mongo_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ class MongoModel
include Mongoid::Document

field :string_field, type: String
field :numeric_field, type: Numeric
field :numeric_field, type: Integer
end
7 changes: 3 additions & 4 deletions lib/bugsnag/integrations/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ def leave_mongo_breadcrumb(event_name, event)
# Removes values from filter hashes, replacing them with '?'
#
# @param filter_hash [Hash] the filter hash for the mongo transaction
# @param depth [Numeric] the current filter depth
# @param depth [Integer] the current filter depth
#
# @return [Hash] the filtered hash
def sanitize_filter_hash(filter_hash, depth = 0)
filter_hash.each_with_object({}) do |args, output|
key, value = *args
filter_hash.each_with_object({}) do |(key, value), output|
output[key] = sanitize_filter_value(value, depth)
end
end
Expand All @@ -84,7 +83,7 @@ def sanitize_filter_hash(filter_hash, depth = 0)
# Transforms a value element into a useful, redacted, version
#
# @param value [Object] the filter value
# @param depth [Numeric] the current filter depth
# @param depth [Integer] the current filter depth
#
# @return [Array, Hash, String] the sanitized value
def sanitize_filter_value(value, depth)
Expand Down
36 changes: 9 additions & 27 deletions spec/integrations/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
module ::Mongo
module Monitoring
COMMAND = 'command'
class Global
module Global
end
end
end
Expand All @@ -22,15 +22,11 @@ def require(path)
end

it "should subscribe to the mongo monitoring service" do
expect(::Mongo::Monitoring::Global).to receive(:subscribe) do |*args|
expect(args[0]).to equal(::Mongo::Monitoring::COMMAND)
subscriber = args[1]
expect(subscriber.class).to equal(Bugsnag::MongoBreadcrumbSubscriber)
expect(subscriber.respond_to?(:started)).to be true
expect(subscriber.respond_to?(:succeeded)).to be true
expect(subscriber.respond_to?(:failed)).to be true
expect(::Mongo::Monitoring::Global).to receive(:subscribe) do |command, subscriber|
expect(command).to eq(::Mongo::Monitoring::COMMAND)
expect(subscriber).to be_an_instance_of(::Bugsnag::MongoBreadcrumbSubscriber)
end
require './lib/bugsnag/integrations/mongo'
load './lib/bugsnag/integrations/mongo.rb'
end

context "with the module loaded" do
Expand All @@ -50,7 +46,7 @@ def require(path)
end

describe "#succeeded" do
it "calls #leave_mongo_beradcrumb with the event_name and event" do
it "calls #leave_mongo_breadcrumb with the event_name and event" do
event = double
expect(subscriber).to receive(:leave_mongo_breadcrumb).with("succeeded", event)
subscriber.succeeded(event)
Expand Down Expand Up @@ -138,7 +134,7 @@ def require(path)
subscriber.send(:leave_mongo_breadcrumb, event_name, event)
end

it "adds the correct colleciton name for 'getMore' commands" do
it "adds the correct collection name for 'getMore' commands" do
allow(event).to receive(:command_name).and_return("getMore")
expect(subscriber).to receive(:pop_command).with("123456").and_return(command)
expect(Bugsnag).to receive(:leave_breadcrumb).with(
Expand Down Expand Up @@ -171,7 +167,7 @@ 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":"?"}]}'
},
"process",
:auto
Expand All @@ -183,18 +179,14 @@ def require(path)

describe "#sanitize_filter_hash" do
it "calls into #sanitize_filter_value with the value from each {k,v} pair" do
expect(subscriber).to receive(:sanitize_filter_value).with(1, 0).ordered.and_return('?')
expect(subscriber).to receive(:sanitize_filter_value).with(2, 0).ordered.and_return('?')
expect(subscriber.send(:sanitize_filter_hash, {:a => 1, :b => 2})).to eq({:a => '?', :b => '?'})
end

it "defaults the depth to 0" do
expect(subscriber).to receive(:sanitize_filter_value).with(1, 0).ordered.and_return('?')
subscriber.send(:sanitize_filter_hash, {:a => 1})
end

it "passes through a given depth" do
expect(subscriber).to receive(:sanitize_filter_value).with(1, 3).ordered.and_return('?')
subscriber.send(:sanitize_filter_hash, {:a => 1}, 3)
end
end
Expand All @@ -208,10 +200,6 @@ def require(path)
end

it "is recursive and iterative for array values" do
expect(subscriber).to receive(:sanitize_filter_value).with([1, 2, 3], 0).ordered.and_call_original
expect(subscriber).to receive(:sanitize_filter_value).with(1, 1).ordered.and_call_original
expect(subscriber).to receive(:sanitize_filter_value).with(2, 1).ordered.and_call_original
expect(subscriber).to receive(:sanitize_filter_value).with(3, 1).ordered.and_call_original
expect(subscriber.send(:sanitize_filter_value, [1, 2, 3], 0)).to eq(['?', '?', '?'])
end

Expand All @@ -221,12 +209,6 @@ def require(path)
end

it "increments the depth for each call" do
expect(subscriber).to receive(:sanitize_filter_value).with([1, [2, [3]]], 0).ordered.and_call_original
expect(subscriber).to receive(:sanitize_filter_value).with(1, 1).ordered.and_call_original
expect(subscriber).to receive(:sanitize_filter_value).with([2, [3]], 1).ordered.and_call_original
expect(subscriber).to receive(:sanitize_filter_value).with(2, 2).ordered.and_call_original
expect(subscriber).to receive(:sanitize_filter_value).with([3], 2).ordered.and_call_original
expect(subscriber).to receive(:sanitize_filter_value).with(3, 3).ordered.and_call_original
expect(subscriber.send(:sanitize_filter_value, [1, [2, [3]]], 0)).to eq(['?', ['?', ['?']]])
end

Expand Down Expand Up @@ -262,7 +244,7 @@ def require(path)
it "removes the command from the request_data" do
subscriber.send(:pop_command, request_id)
command_hash = Bugsnag.configuration.request_data[Bugsnag::MongoBreadcrumbSubscriber::MONGO_COMMAND_KEY]
expect(command_hash).not_to include(request_id => command)
expect(command_hash).not_to have_key(request_id)
end

it "returns nil if the request_id is not found" do
Expand Down

0 comments on commit be506d3

Please sign in to comment.