@@ -21,6 +21,15 @@ module OpenTelemetry
2121 #
2222 # @api private
2323 class CommandTracer
24+ include Mongo ::Monitoring ::Event ::Secure
25+
26+ # Commands for which a span MUST NOT be created. The OpenTelemetry spec
27+ # requires drivers to skip command spans for sensitive commands listed in
28+ # the Command Logging and Monitoring spec. We additionally skip hello /
29+ # legacy hello in all forms — these are handshake/heartbeat traffic and
30+ # would only add noise to traces.
31+ HELLO_COMMANDS = %w[ hello ismaster isMaster ] . freeze
32+
2433 # Initializes a new CommandTracer.
2534 #
2635 # @param otel_tracer [ OpenTelemetry::Trace::Tracer ] the OpenTelemetry tracer.
@@ -57,6 +66,8 @@ def start_span(message, operation_context, connection); end
5766 # @return [ Object ] the result of the command.
5867 # rubocop:disable Lint/RescueException
5968 def trace_command ( message , _operation_context , connection )
69+ return yield if skip_tracing? ( message )
70+
6071 # Commands should always be nested under their operation span, not directly under
6172 # the transaction span. Don't pass with_parent to use automatic parent resolution
6273 # from the currently active span (the operation span).
@@ -76,6 +87,22 @@ def trace_command(message, _operation_context, connection)
7687
7788 private
7889
90+ # Determines whether the command must not be traced. Sensitive auth
91+ # commands carry credentials in their payloads (SCRAM proofs, cleartext
92+ # passwords, etc.) and the OpenTelemetry spec requires drivers to skip
93+ # command spans for them. Hello / legacy hello are also skipped to keep
94+ # handshake traffic out of traces.
95+ #
96+ # @param message [ Mongo::Protocol::Message ] the command message.
97+ #
98+ # @return [ Boolean ] true when no command span should be created.
99+ def skip_tracing? ( message )
100+ name = command_name ( message )
101+ return true if HELLO_COMMANDS . include? ( name )
102+
103+ sensitive? ( command_name : name , document : message . documents . first )
104+ end
105+
79106 # Creates a span for a command.
80107 #
81108 # @param message [ Mongo::Protocol::Message ] the command message.
0 commit comments