Skip to content

Commit 81a2f0a

Browse files
authored
RUBY-1955 Log connection ids in command logger (#1552)
* RUBY-1955 Log connection ids in command logger * RUBY-1955 Also log request id to be able to easily correlate started & succeeded/failed entries * RUBY-1955 Pending connections also need ids
1 parent 7c0ec22 commit 81a2f0a

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

lib/mongo/monitoring/command_log_subscriber.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def initialize(options = {})
5454
# @since 2.1.0
5555
def started(event)
5656
if logger.debug?
57-
log_debug("#{prefix(event)} | STARTED | #{format_command(event.command)}")
57+
log_debug("#{prefix(event, connection_id: event.connection_id)} | " +
58+
"STARTED | #{format_command(event.command)}")
5859
end
5960
end
6061

@@ -96,8 +97,9 @@ def format_command(args)
9697
end
9798
end
9899

99-
def prefix(event)
100-
"#{event.address.to_s} | #{event.database_name}.#{event.command_name}"
100+
def prefix(event, connection_id: nil)
101+
"[#{event.request_id}] #{event.address.to_s}#{connection_id && " ##{connection_id}"} | " +
102+
"#{event.database_name}.#{event.command_name}"
101103
end
102104

103105
def truncate(command)

lib/mongo/monitoring/event/command_started.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class CommandStarted < Mongo::Event::Base
4545
# @api private
4646
attr_reader :socket_object_id
4747

48+
# @return [ Integer ] The ID for the connection over which the command
49+
# is sent.
50+
#
51+
# @api private
52+
attr_reader :connection_id
53+
4854
# Create the new event.
4955
#
5056
# @example Create the event.
@@ -59,7 +65,7 @@ class CommandStarted < Mongo::Event::Base
5965
# @since 2.1.0
6066
# @api private
6167
def initialize(command_name, database_name, address, request_id,
62-
operation_id, command, socket_object_id = nil
68+
operation_id, command, socket_object_id: nil, connection_id: nil
6369
)
6470
@command_name = command_name.to_s
6571
@database_name = database_name
@@ -68,6 +74,7 @@ def initialize(command_name, database_name, address, request_id,
6874
@operation_id = operation_id
6975
@command = redacted(command_name, command)
7076
@socket_object_id = socket_object_id
77+
@connection_id = connection_id
7178
end
7279

7380
# Create the event from a wire protocol message payload.
@@ -83,15 +90,18 @@ def initialize(command_name, database_name, address, request_id,
8390
#
8491
# @since 2.1.0
8592
# @api private
86-
def self.generate(address, operation_id, payload, socket_object_id = nil)
93+
def self.generate(address, operation_id, payload,
94+
socket_object_id: nil, connection_id: nil
95+
)
8796
new(
8897
payload[:command_name],
8998
payload[:database_name],
9099
address,
91100
payload[:request_id],
92101
operation_id,
93102
payload[:command],
94-
socket_object_id,
103+
socket_object_id: socket_object_id,
104+
connection_id: connection_id,
95105
)
96106
end
97107

lib/mongo/monitoring/publishable.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ def publish_cmap_event(event)
4444

4545
private
4646

47-
def command_started(address, operation_id, payload, socket_object_id = nil)
47+
def command_started(address, operation_id, payload,
48+
socket_object_id: nil, connection_id: nil
49+
)
4850
monitoring.started(
4951
Monitoring::COMMAND,
5052
Event::CommandStarted.generate(address, operation_id, payload,
51-
socket_object_id)
53+
socket_object_id: socket_object_id, connection_id: connection_id)
5254
)
5355
end
5456

lib/mongo/server/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def do_connect
186186

187187
begin
188188
handshake!(socket)
189-
pending_connection = PendingConnection.new(socket, @server, monitoring, options)
189+
pending_connection = PendingConnection.new(socket, @server, monitoring, options.merge(id: id))
190190
authenticate!(pending_connection)
191191
rescue Exception
192192
socket.close

lib/mongo/server/connection_base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def deliver(message)
9696
buffer = serialize(message)
9797
ensure_connected do |socket|
9898
operation_id = Monitoring.next_operation_id
99-
command_started(address, operation_id, message.payload, socket.object_id)
99+
command_started(address, operation_id, message.payload,
100+
socket_object_id: socket.object_id, connection_id: id)
100101
start = Time.now
101102
result = nil
102103
begin

lib/mongo/server/pending_connection.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ def initialize(socket, server, monitoring, options = {})
2626
@options = options
2727
@server = server
2828
@monitoring = monitoring
29+
@id = options[:id]
2930
end
3031

32+
# @return [ Integer ] The ID for the connection. This is the same ID
33+
# as that of the regular Connection object for which this
34+
# PendingConnection instance was created.
35+
attr_reader :id
36+
3137
def ensure_connected
3238
yield @socket
3339
end

0 commit comments

Comments
 (0)