@@ -28,30 +28,29 @@ def initialize(otel_tracer, parent_tracer)
2828 end
2929
3030 def trace_operation ( name , operation , operation_context )
31- parent_context = parent_context_for ( operation_context , operation . cursor_id )
32- operation_context . tracer = @parent_tracer
33- span = @otel_tracer . start_span (
31+ @otel_tracer . in_span (
3432 operation_span_name ( name , operation ) ,
3533 attributes : span_attributes ( name , operation ) ,
36- with_parent : parent_context ,
3734 kind : :client
38- )
39- :: OpenTelemetry :: Trace . with_span ( span ) do | _s , c |
35+ ) do | span , _context |
36+ operation_context . tracer = @parent_tracer
4037 yield . tap do |result |
41- process_cursor_context ( result , operation . cursor_id , c )
38+ if result . is_a? ( Cursor ) && result . id . positive?
39+ span . set_attribute ( 'db.mongodb.cursor_id' , result . id )
40+ end
4241 end
4342 end
44- rescue Exception => e
45- span &.record_exception ( e )
46- span &.status = ::OpenTelemetry ::Trace ::Status . error ( "Unhandled exception of type: #{ e . class } " )
47- raise e
4843 ensure
49- span &.finish
5044 operation_context . tracer = nil
5145 end
5246
5347 private
5448
49+ # Returns a hash of attributes for the OpenTelemetry span for the operation.
50+ #
51+ # @param name [String] The name of the operation.
52+ # @param operation [Operation] The operation being traced.
53+ # @return [Hash] A hash of attributes for the span.
5554 def span_attributes ( name , operation )
5655 {
5756 'db.system' => 'mongodb' ,
@@ -63,49 +62,11 @@ def span_attributes(name, operation)
6362 } . compact
6463 end
6564
66- def parent_context_for ( operation_context , cursor_id )
67- if ( key = transaction_map_key ( operation_context . session ) )
68- transaction_context_map [ key ]
69- elsif cursor_id
70- cursor_context_map [ cursor_id ]
71- end
72- end
73-
74- # This map is used to store OpenTelemetry context for cursor_id.
75- # This allows to group all operations related to a cursor under the same context.
76- #
77- # # @return [Hash] a map of cursor_id to OpenTelemetry context.
78- def cursor_context_map
79- @cursor_context_map ||= { }
80- end
81-
82- def process_cursor_context ( result , cursor_id , context )
83- return unless result . is_a? ( Cursor )
84-
85- if result . id . zero?
86- # If the cursor is closed, remove it from the context map.
87- cursor_context_map . delete ( cursor_id )
88- elsif result . id && cursor_id . nil?
89- # New cursor created, store its context.
90- cursor_context_map [ result . id ] = context
91- end
92- end
93-
94- # This map is used to store OpenTelemetry context for transaction.
95- # This allows to group all operations related to a transaction under the same context.
65+ # Returns the name of the span for the operation.
9666 #
97- # @return [Hash] a map of transaction_id to OpenTelemetry context.
98- def transaction_context_map
99- @transaction_context_map ||= { }
100- end
101-
102- # @param session [Mongo::Session] the session for which to get the transaction map key.
103- def transaction_map_key ( session )
104- return if session . nil? || session . implicit? || !session . in_transaction?
105-
106- "#{ session . id } -#{ session . txn_num } "
107- end
108-
67+ # @param name [String] The name of the operation.
68+ # @param operation [Operation] The operation being traced.
69+ # # @return [String] The name of the span.
10970 def operation_span_name ( name , operation )
11071 if operation . coll_name
11172 "#{ name } #{ operation . db_name } .#{ operation . coll_name } "
0 commit comments