Skip to content

Commit 5310ffb

Browse files
wip
1 parent f059514 commit 5310ffb

File tree

28 files changed

+1245
-337
lines changed

28 files changed

+1245
-337
lines changed

lib/mongo/collection.rb

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -412,21 +412,24 @@ def create(opts = {})
412412
client: client,
413413
session: session
414414
)
415-
maybe_create_qe_collections(opts[:encrypted_fields], client, session) do |encrypted_fields|
416-
Operation::Create.new(
417-
selector: operation,
418-
db_name: database.name,
419-
write_concern: write_concern,
420-
session: session,
421-
# Note that these are collection options, collation isn't
422-
# taken from options passed to the create method.
423-
collation: options[:collation] || options['collation'],
424-
encrypted_fields: encrypted_fields,
425-
validator: options[:validator],
426-
).execute(
427-
next_primary(nil, session),
428-
context: context
429-
)
415+
operation = Operation::Create.new(
416+
selector: operation,
417+
db_name: database.name,
418+
write_concern: write_concern,
419+
session: session,
420+
# Note that these are collection options, collation isn't
421+
# taken from options passed to the create method.
422+
collation: options[:collation] || options['collation'],
423+
validator: options[:validator],
424+
)
425+
tracer.trace_operation(operation, context, op_name: 'createCollection') do
426+
maybe_create_qe_collections(opts[:encrypted_fields], client, session) do |encrypted_fields|
427+
operation.encrypted_fields = encrypted_fields
428+
operation.execute(
429+
next_primary(nil, session),
430+
context: context
431+
)
432+
end
430433
end
431434
end
432435
end
@@ -455,25 +458,27 @@ def create(opts = {})
455458
# @since 2.0.0
456459
def drop(opts = {})
457460
client.with_session(opts) do |session|
458-
maybe_drop_emm_collections(opts[:encrypted_fields], client, session) do
459-
temp_write_concern = write_concern
460-
write_concern = if opts[:write_concern]
461-
WriteConcern.get(opts[:write_concern])
462-
else
463-
temp_write_concern
461+
context = Operation::Context.new(
462+
client: client,
463+
session: session,
464+
operation_timeouts: operation_timeouts(opts)
465+
)
466+
operation = Operation::Drop.new({
467+
selector: { :drop => name },
468+
db_name: database.name,
469+
write_concern: write_concern,
470+
session: session,
471+
})
472+
tracer.trace_operation(operation, context, op_name: 'dropCollection') do
473+
maybe_drop_emm_collections(opts[:encrypted_fields], client, session) do
474+
temp_write_concern = write_concern
475+
write_concern = if opts[:write_concern]
476+
WriteConcern.get(opts[:write_concern])
477+
else
478+
temp_write_concern
479+
end
480+
do_drop(operation, session, context)
464481
end
465-
context = Operation::Context.new(
466-
client: client,
467-
session: session,
468-
operation_timeouts: operation_timeouts(opts)
469-
)
470-
operation = Operation::Drop.new({
471-
selector: { :drop => name },
472-
db_name: database.name,
473-
write_concern: write_concern,
474-
session: session,
475-
})
476-
do_drop(operation, session, context)
477482
end
478483
end
479484
end

lib/mongo/collection/view/readable.rb

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,25 @@ def count(opts = {})
192192
session: session,
193193
operation_timeouts: operation_timeouts(opts)
194194
)
195-
read_with_retry(session, selector, context) do |server|
196-
Operation::Count.new(
197-
selector: cmd,
198-
db_name: database.name,
199-
options: {:limit => -1},
200-
read: read_pref,
201-
session: session,
202-
# For some reason collation was historically accepted as a
203-
# string key. Note that this isn't documented as valid usage.
204-
collation: opts[:collation] || opts['collation'] || collation,
205-
comment: opts[:comment],
206-
).execute(
207-
server,
208-
context: context
209-
)
210-
end.n.to_i
195+
operation = Operation::Count.new(
196+
selector: cmd,
197+
db_name: database.name,
198+
options: {:limit => -1},
199+
read: read_pref,
200+
session: session,
201+
# For some reason collation was historically accepted as a
202+
# string key. Note that this isn't documented as valid usage.
203+
collation: opts[:collation] || opts['collation'] || collation,
204+
comment: opts[:comment],
205+
)
206+
tracer.trace_operation(operation, context) do
207+
read_with_retry(session, selector, context) do |server|
208+
operation.execute(
209+
server,
210+
context: context
211+
)
212+
end.n.to_i
213+
end
211214
end
212215
end
213216

@@ -294,32 +297,35 @@ def estimated_document_count(opts = {})
294297
session: session,
295298
operation_timeouts: operation_timeouts(opts)
296299
)
297-
read_with_retry(session, selector, context) do |server|
298-
cmd = { count: collection.name }
299-
cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms]
300-
if read_concern
301-
cmd[:readConcern] = Options::Mapper.transform_values_to_strings(read_concern)
300+
cmd = { count: collection.name }
301+
cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms]
302+
if read_concern
303+
cmd[:readConcern] = Options::Mapper.transform_values_to_strings(read_concern)
304+
end
305+
operation = Operation::Count.new(
306+
selector: cmd,
307+
db_name: database.name,
308+
read: read_pref,
309+
session: session,
310+
comment: opts[:comment],
311+
)
312+
tracer.trace_operation(operation, context, op_name: 'estimatedDocumentCount') do
313+
read_with_retry(session, selector, context) do |server|
314+
result = operation.execute(server, context: context)
315+
result.n.to_i
316+
end
317+
rescue Error::OperationFailure::Family => exc
318+
if exc.code == 26
319+
# NamespaceNotFound
320+
# This should only happen with the aggregation pipeline path
321+
# (server 4.9+). Previous servers should return 0 for nonexistent
322+
# collections.
323+
0
324+
else
325+
raise
302326
end
303-
result = Operation::Count.new(
304-
selector: cmd,
305-
db_name: database.name,
306-
read: read_pref,
307-
session: session,
308-
comment: opts[:comment],
309-
).execute(server, context: context)
310-
result.n.to_i
311327
end
312328
end
313-
rescue Error::OperationFailure::Family => exc
314-
if exc.code == 26
315-
# NamespaceNotFound
316-
# This should only happen with the aggregation pipeline path
317-
# (server 4.9+). Previous servers should return 0 for nonexistent
318-
# collections.
319-
0
320-
else
321-
raise
322-
end
323329
end
324330

325331
# Get a list of distinct values for a specific field.
@@ -362,22 +368,25 @@ def distinct(field_name, opts = {})
362368
session: session,
363369
operation_timeouts: operation_timeouts(opts)
364370
)
365-
read_with_retry(session, selector, context) do |server|
366-
Operation::Distinct.new(
367-
selector: cmd,
368-
db_name: database.name,
369-
options: {:limit => -1},
370-
read: read_pref,
371-
session: session,
372-
comment: opts[:comment],
373-
# For some reason collation was historically accepted as a
374-
# string key. Note that this isn't documented as valid usage.
375-
collation: opts[:collation] || opts['collation'] || collation,
376-
).execute(
377-
server,
378-
context: context
379-
)
380-
end.first['values']
371+
operation = Operation::Distinct.new(
372+
selector: cmd,
373+
db_name: database.name,
374+
options: {:limit => -1},
375+
read: read_pref,
376+
session: session,
377+
comment: opts[:comment],
378+
# For some reason collation was historically accepted as a
379+
# string key. Note that this isn't documented as valid usage.
380+
collation: opts[:collation] || opts['collation'] || collation,
381+
)
382+
tracer.trace_operation(operation, context) do
383+
read_with_retry(session, selector, context) do |server|
384+
operation.execute(
385+
server,
386+
context: context
387+
)
388+
end.first['values']
389+
end
381390
end
382391
end
383392

lib/mongo/collection/view/writable.rb

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,24 @@ def find_one_and_update(document, opts = {})
211211
session: session,
212212
operation_timeouts: operation_timeouts(opts)
213213
)
214-
write_with_retry(write_concern, context: context) do |connection, txn_num, context|
215-
gte_4_4 = connection.server.description.server_version_gte?('4.4')
216-
if !gte_4_4 && opts[:hint] && write_concern && !write_concern.acknowledged?
217-
raise Error::UnsupportedOption.hint_error(unacknowledged_write: true)
214+
operation = Operation::WriteCommand.new(
215+
selector: cmd,
216+
db_name: database.name,
217+
write_concern: write_concern,
218+
session: session,
219+
)
220+
tracer.trace_operation(operation, context, op_name: 'findOneAndUpdate') do
221+
write_with_retry(write_concern, context: context) do |connection, txn_num, context|
222+
gte_4_4 = connection.server.description.server_version_gte?('4.4')
223+
if !gte_4_4 && opts[:hint] && write_concern && !write_concern.acknowledged?
224+
raise Error::UnsupportedOption.hint_error(unacknowledged_write: true)
225+
end
226+
operation.txn_num = txn_num
227+
operation.execute_with_connection(connection, context: context)
218228
end
219-
220-
Operation::WriteCommand.new(
221-
selector: cmd,
222-
db_name: database.name,
223-
write_concern: write_concern,
224-
session: session,
225-
txn_num: txn_num,
226-
).execute_with_connection(connection, context: context)
227-
end
228-
end.first&.fetch('value', nil)
229-
value unless value.nil? || value.empty?
229+
end.first&.fetch('value', nil)
230+
value unless value.nil? || value.empty?
231+
end
230232
end
231233

232234
# Remove documents from the collection.
@@ -275,22 +277,24 @@ def delete_many(opts = {})
275277
session: session,
276278
operation_timeouts: operation_timeouts(opts)
277279
)
278-
nro_write_with_retry(write_concern, context: context) do |connection, txn_num, context|
279-
gte_4_4 = connection.server.description.server_version_gte?('4.4')
280-
if !gte_4_4 && opts[:hint] && write_concern && !write_concern.acknowledged?
281-
raise Error::UnsupportedOption.hint_error(unacknowledged_write: true)
280+
operation = Operation::Delete.new(
281+
deletes: [ delete_doc ],
282+
db_name: collection.database.name,
283+
coll_name: collection.name,
284+
write_concern: write_concern,
285+
bypass_document_validation: !!opts[:bypass_document_validation],
286+
session: session,
287+
let: opts[:let],
288+
comment: opts[:comment],
289+
)
290+
tracer.trace_operation(operation, context, op_name: 'deleteMany') do
291+
nro_write_with_retry(write_concern, context: context) do |connection, txn_num, context|
292+
gte_4_4 = connection.server.description.server_version_gte?('4.4')
293+
if !gte_4_4 && opts[:hint] && write_concern && !write_concern.acknowledged?
294+
raise Error::UnsupportedOption.hint_error(unacknowledged_write: true)
295+
end
296+
operation.execute_with_connection(connection, context: context)
282297
end
283-
284-
Operation::Delete.new(
285-
deletes: [ delete_doc ],
286-
db_name: collection.database.name,
287-
coll_name: collection.name,
288-
write_concern: write_concern,
289-
bypass_document_validation: !!opts[:bypass_document_validation],
290-
session: session,
291-
let: opts[:let],
292-
comment: opts[:comment],
293-
).execute_with_connection(connection, context: context)
294298
end
295299
end
296300
end
@@ -580,23 +584,25 @@ def update_one(spec, opts = {})
580584
session: session,
581585
operation_timeouts: operation_timeouts(opts)
582586
)
583-
write_with_retry(write_concern, context: context) do |connection, txn_num, context|
584-
gte_4_2 = connection.server.description.server_version_gte?('4.2')
585-
if !gte_4_2 && opts[:hint] && write_concern && !write_concern.acknowledged?
586-
raise Error::UnsupportedOption.hint_error(unacknowledged_write: true)
587+
operation = Operation::Update.new(
588+
updates: [ update_doc ],
589+
db_name: collection.database.name,
590+
coll_name: collection.name,
591+
write_concern: write_concern,
592+
bypass_document_validation: !!opts[:bypass_document_validation],
593+
session: session,
594+
let: opts[:let],
595+
comment: opts[:comment],
596+
)
597+
tracer.trace_operation(operation, context) do
598+
write_with_retry(write_concern, context: context) do |connection, txn_num, context|
599+
gte_4_2 = connection.server.description.server_version_gte?('4.2')
600+
if !gte_4_2 && opts[:hint] && write_concern && !write_concern.acknowledged?
601+
raise Error::UnsupportedOption.hint_error(unacknowledged_write: true)
602+
end
603+
operation.txn_num = txn_num
604+
operation.execute_with_connection(connection, context: context)
587605
end
588-
589-
Operation::Update.new(
590-
updates: [ update_doc ],
591-
db_name: collection.database.name,
592-
coll_name: collection.name,
593-
write_concern: write_concern,
594-
bypass_document_validation: !!opts[:bypass_document_validation],
595-
session: session,
596-
txn_num: txn_num,
597-
let: opts[:let],
598-
comment: opts[:comment],
599-
).execute_with_connection(connection, context: context)
600606
end
601607
end
602608
end

lib/mongo/database.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ class Database
7474
:server_selector,
7575
:read_concern,
7676
:write_concern,
77-
:encrypted_fields_map
77+
:encrypted_fields_map,
78+
:tracer
7879

7980
# @return [ Mongo::Server ] Get the primary server from the cluster.
8081
def_delegators :cluster,

0 commit comments

Comments
 (0)