Skip to content

Commit eae217c

Browse files
wip
1 parent 6aae681 commit eae217c

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,6 @@ RSpec/ExampleLength:
116116

117117
RSpec/MessageSpies:
118118
EnforcedStyle: receive
119+
120+
RSpec/NamedSubject:
121+
Enabled: false

lib/mongo/tracing/open_telemetry.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
module Mongo
1818
module Tracing
19+
# This module contains OpenTelemetry tracing functionality for MongoDB operations.
1920
module OpenTelemetry
2021
end
2122
end

spec/mongo/tracing/open_telemetry/command_tracer_spec.rb

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,34 @@
22

33
require 'spec_helper'
44

5-
require 'opentelemetry'
5+
require 'opentelemetry-sdk'
66

7+
# rubocop:disable RSpec/VerifiedDoubles
78
describe Mongo::Tracing::OpenTelemetry::CommandTracer do
8-
let(:otel_tracer) { double('OpenTelemetry::Trace::Tracer') }
9-
let(:parent_tracer) { double('Mongo::Tracing::OpenTelemetry::Tracer') }
9+
let(:otel_tracer) { instance_double(OpenTelemetry::Trace::Tracer) }
10+
let(:parent_tracer) { instance_double(Mongo::Tracing::OpenTelemetry::Tracer) }
1011
let(:query_text_max_length) { 0 }
1112
let(:command_tracer) do
1213
described_class.new(otel_tracer, parent_tracer, query_text_max_length: query_text_max_length)
1314
end
14-
let(:lsid_value) { "55dcab94-2c82-445a-a7f2-5ce50213b753" }
15+
let(:lsid_value) { '55dcab94-2c82-445a-a7f2-5ce50213b753' }
1516

1617
let(:connection) do
17-
double('Mongo::Server::Connection',
18-
id: 123,
19-
address: double('Address', host: 'localhost', port: 27_017),
20-
transport: :tcp,
21-
server: double('Server',
22-
description: double('Description', server_connection_id: 456)))
18+
instance_double(Mongo::Server::Connection,
19+
id: 123,
20+
address: instance_double(Mongo::Address, host: 'localhost', port: 27_017),
21+
transport: :tcp,
22+
server: instance_double(Mongo::Server,
23+
description: instance_double(Mongo::Server::Description,
24+
server_connection_id: 456)))
2325
end
2426

2527
let(:message) do
26-
double('Mongo::Protocol::Message',
27-
documents: [ document ],
28-
payload: { 'command' => document })
28+
double(
29+
'message',
30+
documents: [ document ],
31+
payload: { 'command' => document }
32+
)
2933
end
3034

3135
let(:document) do
@@ -37,7 +41,7 @@
3741
}
3842
end
3943

40-
let(:operation_context) { double('Mongo::Operation::Context') }
44+
let(:operation_context) { instance_double(Mongo::Operation::Context) }
4145

4246
describe '#initialize' do
4347
it 'sets the otel_tracer' do
@@ -62,9 +66,9 @@
6266
end
6367

6468
describe '#trace_command' do
65-
let(:span) { double('OpenTelemetry::Trace::Span', finish: nil, set_attribute: nil) }
66-
let(:context) { double('OpenTelemetry::Context') }
67-
let(:result) { double('Result', has_cursor_id?: false, successful?: true) }
69+
let(:span) { instance_double(OpenTelemetry::Trace::Span, finish: nil, set_attribute: nil) }
70+
let(:context) { instance_double(Mongo::Operation::Context) }
71+
let(:result) { instance_double(Mongo::Operation::Result, has_cursor_id?: false, successful?: true) }
6872

6973
before do
7074
allow(otel_tracer).to receive(:start_span).and_return(span)
@@ -100,7 +104,7 @@
100104

101105
context 'when result has cursor_id' do
102106
let(:result) do
103-
double('Result', has_cursor_id?: true, cursor_id: 789, successful?: true)
107+
instance_double(Mongo::Operation::Result, has_cursor_id?: true, cursor_id: 789, successful?: true)
104108
end
105109

106110
it 'sets the cursor_id attribute' do
@@ -111,7 +115,7 @@
111115

112116
context 'when result has zero cursor_id' do
113117
let(:result) do
114-
double('Result', has_cursor_id?: true, cursor_id: 0, successful?: true)
118+
instance_double(Mongo::Operation::Result, has_cursor_id?: true, cursor_id: 0, successful?: true)
115119
end
116120

117121
it 'does not set the cursor_id attribute' do
@@ -251,7 +255,7 @@
251255
context 'with getMore command' do
252256
let(:document) do
253257
{
254-
'getMore' => double('BSON::Int64', value: 999),
258+
'getMore' => BSON::Int64.new(999),
255259
'collection' => 'users',
256260
'$db' => 'test_db'
257261
}
@@ -267,7 +271,7 @@
267271
{
268272
'find' => 'users',
269273
'$db' => 'test_db',
270-
'txnNumber' => double('BSON::Int64', value: 42)
274+
'txnNumber' => BSON::Int64.new(42)
271275
}
272276
end
273277

@@ -390,7 +394,7 @@
390394
subject { command_tracer.send(:cursor_id, message) }
391395

392396
context 'with getMore command' do
393-
let(:document) { { 'getMore' => double('BSON::Int64', value: 999) } }
397+
let(:document) { { 'getMore' => BSON::Int64.new(999) } }
394398

395399
it 'returns the cursor ID' do
396400
expect(subject).to eq(999)
@@ -430,7 +434,7 @@
430434
subject { command_tracer.send(:txn_number, message) }
431435

432436
context 'with txnNumber present' do
433-
let(:document) { { 'find' => 'users', 'txnNumber' => double('BSON::Int64', value: 42) } }
437+
let(:document) { { 'find' => 'users', 'txnNumber' => BSON::Int64.new(42) } }
434438

435439
it 'returns the transaction number' do
436440
expect(subject).to eq(42)
@@ -509,3 +513,4 @@
509513
end
510514
end
511515
end
516+
# rubocop:enable RSpec/VerifiedDoubles

spec/support/tracing.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def set_attribute(key, value)
2222
@attributes[key] = value
2323
end
2424

25+
# rubocop:disable Lint/UnusedMethodArgument
2526
def record_exception(exception, attributes: nil)
2627
set_attribute('exception.type', exception.class.to_s)
2728
set_attribute('exception.message', exception.message)
@@ -31,6 +32,7 @@ def record_exception(exception, attributes: nil)
3132
replace: '�')
3233
)
3334
end
35+
# rubocop:enable Lint/UnusedMethodArgument
3436

3537
def finish
3638
raise Tracing::Error, 'Span already finished' if @finished
@@ -79,9 +81,9 @@ def finish_span(span)
7981

8082
def span_hierarchy
8183
# Build a mapping of all spans by their object_id for quick lookup
82-
span_map = {}
84+
span_map = {}.compare_by_identity
8385
@spans.each do |span|
84-
span_map[span.object_id] = span
86+
span_map[span] = span
8587
end
8688

8789
# Build the hierarchy by attaching children to their parents
@@ -92,7 +94,7 @@ def span_hierarchy
9294
root_spans << span
9395
else
9496
# Find the parent span and add this span to its nested array
95-
parent = span_map[span.with_parent.object_id]
97+
parent = span_map[span.with_parent]
9698
unless parent
9799
raise Error, "Parent span not found for span #{span.name} (parent object_id: #{span.with_parent.object_id})"
98100
end
@@ -115,9 +117,6 @@ def resolve_parent(with_parent)
115117
when Tracing::Context
116118
# Extract span from our mock Context
117119
with_parent.span
118-
when Tracing::Span
119-
# Already a span
120-
with_parent
121120
when OpenTelemetry::Context
122121
# Extract span from OpenTelemetry::Context
123122
# The OpenTelemetry context stores the span using a specific key

0 commit comments

Comments
 (0)