Skip to content

Commit b4d9a68

Browse files
wip: transactions
1 parent f3f785a commit b4d9a68

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

lib/mongo/session.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,8 @@ def start_transaction(options = nil)
625625
@state = STARTING_TRANSACTION_STATE
626626
@already_committed = false
627627

628+
tracer.start_transaction_span(self)
629+
628630
# This method has no explicit return value.
629631
# We could return nil here but true indicates to the user that the
630632
# operation succeeded. This is intended for interactive use.
@@ -707,6 +709,7 @@ def commit_transaction(options=nil)
707709
end
708710
end
709711
ensure
712+
tracer.finish_transaction_span(self)
710713
@state = TRANSACTION_COMMITTED_STATE
711714
@committing_transaction = false
712715
end
@@ -781,6 +784,7 @@ def abort_transaction(options = nil)
781784
@state = TRANSACTION_ABORTED_STATE
782785
raise
783786
ensure
787+
tracer.finish_transaction_span(self)
784788
@aborting_transaction = false
785789
end
786790

lib/mongo/tracing/open_telemetry/command_tracer.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class CommandTracer
2424
extend Forwardable
2525

2626
def_delegators :@parent_tracer,
27-
:cursor_context_map,
2827
:parent_context_for,
2928
:transaction_context_map,
3029
:transaction_map_key
@@ -36,7 +35,7 @@ def initialize(otel_tracer, parent_tracer, query_text_max_length: 0)
3635
end
3736

3837
def trace_command(message, operation_context, connection)
39-
parent_context = parent_context_for(operation_context, cursor_id(message))
38+
parent_context = parent_context_for(operation_context)
4039
span = @otel_tracer.start_span(
4140
query_summary(message),
4241
attributes: span_attributes(message, connection),

lib/mongo/tracing/open_telemetry/operation_tracer.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class OperationTracer
2424
extend Forwardable
2525

2626
def_delegators :@parent_tracer,
27-
:cursor_context_map,
2827
:parent_context_for,
2928
:transaction_context_map,
3029
:transaction_map_key
@@ -35,7 +34,7 @@ def initialize(otel_tracer, parent_tracer)
3534
end
3635

3736
def trace_operation(operation, operation_context, op_name: nil)
38-
parent_context = parent_context_for(operation_context, operation.cursor_id)
37+
parent_context = parent_context_for(operation_context)
3938
span = @otel_tracer.start_span(
4039
operation_span_name(operation, op_name),
4140
attributes: span_attributes(operation, op_name),
@@ -55,6 +54,32 @@ def trace_operation(operation, operation_context, op_name: nil)
5554
span&.finish
5655
end
5756

57+
def start_transaction_span(session)
58+
raise ArgumentError, 'Session must have a transaction' unless session.in_transaction?
59+
key = transaction_map_key(session)
60+
raise ArgumentError, 'Transaction already started' if transaction_context_map[key]
61+
62+
span = @otel_tracer.start_span(
63+
'transaction',
64+
attributes: {
65+
'db.system' => 'mongodb',
66+
'db.operation.name' => 'transaction',
67+
},
68+
kind: :client
69+
)
70+
transaction_context_map[key] = ::OpenTelemetry::Trace.context_with_span(span)
71+
end
72+
73+
def finish_transaction_span(session)
74+
raise ArgumentError, 'Session must have a transaction' unless session.in_transaction?
75+
key = transaction_map_key(session)
76+
context = transaction_context_map.delete(key)
77+
return unless context
78+
79+
span = ::OpenTelemetry::Trace.current_span(context)
80+
span&.finish
81+
end
82+
5883
private
5984

6085
def operation_name(operation, op_name = nil)
@@ -79,14 +104,7 @@ def span_attributes(operation, op_name)
79104
def process_cursor_context(result, cursor_id, context, span)
80105
return unless result.is_a?(Cursor)
81106

82-
if result.id.zero?
83-
# If the cursor is closed, remove it from the context map.
84-
cursor_context_map.delete(cursor_id)
85-
elsif result.id && cursor_id.nil?
86-
# New cursor created, store its context.
87-
cursor_context_map[result.id] = context
88-
span.set_attribute('db.mongodb.cursor_id', result.id)
89-
end
107+
span.set_attribute('db.mongodb.cursor_id', result.id) if result.id.positive?
90108
end
91109

92110
def collection_name(operation)

lib/mongo/tracing/open_telemetry/tracer.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,23 @@ def trace_operation(operation, operation_context, op_name: nil, &block)
6161
@operation_tracer.trace_operation(operation, operation_context, op_name: op_name, &block)
6262
end
6363

64-
def trace_command(message, operation_context, connection, &block)
65-
return yield unless enabled?
66-
67-
@command_tracer.trace_command(message, operation_context, connection, &block)
64+
def start_transaction_span(session)
65+
@operation_tracer.start_transaction_span(session)
6866
end
6967

70-
def cursor_context_map
71-
@cursor_context_map ||= {}
68+
def finish_transaction_span(session)
69+
@operation_tracer.finish_transaction_span(session)
7270
end
7371

74-
def cursor_map_key(session, cursor_id)
75-
return if cursor_id.nil? || session.nil?
72+
def trace_command(message, operation_context, connection, &block)
73+
return yield unless enabled?
7674

77-
"#{session.session_id['id'].to_uuid}-#{cursor_id}"
75+
@command_tracer.trace_command(message, operation_context, connection, &block)
7876
end
7977

80-
def parent_context_for(operation_context, cursor_id)
78+
def parent_context_for(operation_context)
8179
if (key = transaction_map_key(operation_context.session))
8280
transaction_context_map[key]
83-
elsif (_key = cursor_map_key(operation_context.session, cursor_id))
84-
# We return nil here unless we decide how to nest cursor operations.
85-
nil
86-
# cursor_context_map[key]
8781
end
8882
end
8983

@@ -92,9 +86,10 @@ def transaction_context_map
9286
end
9387

9488
def transaction_map_key(session)
89+
byebug
9590
return if session.nil? || session.implicit? || !session.in_transaction?
9691

97-
"#{session.session_id['id'].to_uuid}-#{session.txn_num}"
92+
session.session_id['id'].to_uuid.to_s
9893
end
9994

10095
private

0 commit comments

Comments
 (0)