@@ -28,30 +28,29 @@ def initialize(otel_tracer, parent_tracer)
28
28
end
29
29
30
30
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 (
34
32
operation_span_name ( name , operation ) ,
35
33
attributes : span_attributes ( name , operation ) ,
36
- with_parent : parent_context ,
37
34
kind : :client
38
- )
39
- :: OpenTelemetry :: Trace . with_span ( span ) do | _s , c |
35
+ ) do | span , _context |
36
+ operation_context . tracer = @parent_tracer
40
37
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
42
41
end
43
42
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
48
43
ensure
49
- span &.finish
50
44
operation_context . tracer = nil
51
45
end
52
46
53
47
private
54
48
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.
55
54
def span_attributes ( name , operation )
56
55
{
57
56
'db.system' => 'mongodb' ,
@@ -63,49 +62,11 @@ def span_attributes(name, operation)
63
62
} . compact
64
63
end
65
64
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.
96
66
#
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.
109
70
def operation_span_name ( name , operation )
110
71
if operation . coll_name
111
72
"#{ name } #{ operation . db_name } .#{ operation . coll_name } "
0 commit comments