Skip to content

Commit 1924187

Browse files
authored
RUBY-1933 Add debug level logging around initial DNS seed list query (mongodb#2904)
* RUBY-1933 Log when attempting to resolve a srv hostname * method comments
1 parent 4b9b347 commit 1924187

File tree

3 files changed

+90
-29
lines changed

3 files changed

+90
-29
lines changed

lib/mongo/client.rb

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -502,35 +502,15 @@ def hash
502502
def initialize(addresses_or_uri, options = nil)
503503
options = options ? options.dup : {}
504504

505-
srv_uri = nil
506-
if addresses_or_uri.is_a?(::String)
507-
uri = URI.get(addresses_or_uri, options)
508-
if uri.is_a?(URI::SRVProtocol)
509-
# If the URI is an SRV URI, note this so that we can start
510-
# SRV polling if the topology is a sharded cluster.
511-
srv_uri = uri
512-
end
513-
addresses = uri.servers
514-
uri_options = uri.client_options.dup
515-
# Special handing for :write and :write_concern: allow client Ruby
516-
# options to override URI options, even when the Ruby option uses the
517-
# deprecated :write key and the URI option uses the current
518-
# :write_concern key
519-
if options[:write]
520-
uri_options.delete(:write_concern)
521-
end
522-
options = uri_options.merge(options)
523-
@srv_records = uri.srv_records
524-
else
525-
addresses = addresses_or_uri
526-
addresses.each do |addr|
527-
if addr =~ /\Amongodb(\+srv)?:\/\//i
528-
raise ArgumentError, "Host '#{addr}' should not contain protocol. Did you mean to not use an array?"
529-
end
530-
end
505+
processed = process_addresses(addresses_or_uri, options)
531506

532-
@srv_records = nil
533-
end
507+
uri = processed[:uri]
508+
addresses = processed[:addresses]
509+
options = processed[:options]
510+
511+
# If the URI is an SRV URI, note this so that we can start
512+
# SRV polling if the topology is a sharded cluster.
513+
srv_uri = uri if uri.is_a?(URI::SRVProtocol)
534514

535515
options = self.class.canonicalize_ruby_options(options)
536516

@@ -1217,6 +1197,73 @@ def timeout_sec
12171197

12181198
private
12191199

1200+
# Attempts to parse the given list of addresses, using the provided options.
1201+
#
1202+
# @param [ String | Array<String> ] addresses the list of addresses
1203+
# @param [ Hash ] options the options that may drive how the list is
1204+
# processed.
1205+
#
1206+
# @return [ Hash<:uri, :addresses, :options> ] the results of processing the
1207+
# list of addresses.
1208+
def process_addresses(addresses, options)
1209+
if addresses.is_a?(String)
1210+
process_addresses_string(addresses, options)
1211+
else
1212+
process_addresses_array(addresses, options)
1213+
end
1214+
end
1215+
1216+
# Attempts to parse the given list of addresses, using the provided options.
1217+
#
1218+
# @param [ String ] addresses the list of addresses
1219+
# @param [ Hash ] options the options that may drive how the list is
1220+
# processed.
1221+
#
1222+
# @return [ Hash<:uri, :addresses, :options> ] the results of processing the
1223+
# list of addresses.
1224+
def process_addresses_string(addresses, options)
1225+
{}.tap do |processed|
1226+
processed[:uri] = uri = URI.get(addresses, options)
1227+
processed[:addresses] = uri.servers
1228+
1229+
uri_options = uri.client_options.dup
1230+
# Special handing for :write and :write_concern: allow client Ruby
1231+
# options to override URI options, even when the Ruby option uses the
1232+
# deprecated :write key and the URI option uses the current
1233+
# :write_concern key
1234+
if options[:write]
1235+
uri_options.delete(:write_concern)
1236+
end
1237+
1238+
processed[:options] = uri_options.merge(options)
1239+
1240+
@srv_records = uri.srv_records
1241+
end
1242+
end
1243+
1244+
# Attempts to parse the given list of addresses, using the provided options.
1245+
#
1246+
# @param [ Array<String> ] addresses the list of addresses
1247+
# @param [ Hash ] options the options that may drive how the list is
1248+
# processed.
1249+
#
1250+
# @return [ Hash<:uri, :addresses, :options> ] the results of processing the
1251+
# list of addresses.
1252+
def process_addresses_array(addresses, options)
1253+
{}.tap do |processed|
1254+
processed[:addresses] = addresses
1255+
processed[:options] = options
1256+
1257+
addresses.each do |addr|
1258+
if addr =~ /\Amongodb(\+srv)?:\/\//i
1259+
raise ArgumentError, "Host '#{addr}' should not contain protocol. Did you mean to not use an array?"
1260+
end
1261+
end
1262+
1263+
@srv_records = nil
1264+
end
1265+
end
1266+
12201267
# Create a new encrypter object using the client's auto encryption options
12211268
def build_encrypter
12221269
@encrypter = Crypt::AutoEncrypter.new(

lib/mongo/uri/srv_protocol.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ def parse!(remaining)
147147
validate_srv_hostname(hostname)
148148
@query_hostname = hostname
149149

150+
log_debug "attempting to resolve #{hostname}"
151+
150152
@srv_result = resolver.get_records(hostname, uri_options[:srv_service_name], uri_options[:srv_max_hosts])
151153
if srv_result.empty?
152154
raise Error::NoSRVRecords.new(NO_SRV_RECORDS % hostname)

spec/mongo/uri/srv_protocol_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# rubocop:todo all
33

44
require 'lite_spec_helper'
5+
require 'support/recording_logger'
56

67
describe Mongo::URI::SRVProtocol do
78
require_external_connectivity
@@ -21,6 +22,18 @@
2122
end
2223
end
2324

25+
describe 'logging' do
26+
let(:logger) { RecordingLogger.new }
27+
let(:uri) { described_class.new(string, logger: logger) }
28+
let(:host) { 'test5.test.build.10gen.cc' }
29+
let(:string) { "#{scheme}#{host}" }
30+
31+
it 'logs when resolving the address' do
32+
expect { uri }.not_to raise_error
33+
expect(logger.contents).to include("attempting to resolve #{host}")
34+
end
35+
end
36+
2437
describe 'invalid uris' do
2538

2639
context 'when there is more than one hostname' do
@@ -228,7 +241,6 @@
228241
end
229242

230243
describe 'valid uris' do
231-
require_external_connectivity
232244

233245
describe 'invalid query results' do
234246

0 commit comments

Comments
 (0)