1
1
from unittest import TestCase , mock
2
2
3
3
from opentelemetry .instrumentation .kafka .utils import (
4
+ _create_consumer_span ,
4
5
_get_span_name ,
5
6
_kafka_getter ,
6
7
_kafka_setter ,
@@ -75,22 +76,18 @@ def test_wrap_send(
75
76
)
76
77
self .assertEqual (retval , original_send_callback .return_value )
77
78
78
- @mock .patch ("opentelemetry.trace.set_span_in_context" )
79
79
@mock .patch ("opentelemetry.propagate.extract" )
80
- @mock .patch ("opentelemetry.context.attach" )
81
- @mock .patch ("opentelemetry.instrumentation.kafka.utils._enrich_span" )
80
+ @mock .patch (
81
+ "opentelemetry.instrumentation.kafka.utils._create_consumer_span"
82
+ )
82
83
@mock .patch (
83
84
"opentelemetry.instrumentation.kafka.utils.KafkaPropertiesExtractor.extract_bootstrap_servers"
84
85
)
85
- @mock .patch ("opentelemetry.context.detach" )
86
86
def test_wrap_next (
87
87
self ,
88
- detach : mock .MagicMock ,
89
88
extract_bootstrap_servers : mock .MagicMock ,
90
- enrich_span : mock .MagicMock ,
91
- attach : mock .MagicMock ,
89
+ _create_consumer_span : mock .MagicMock ,
92
90
extract : mock .MagicMock ,
93
- set_span_in_context : mock .MagicMock ,
94
91
) -> None :
95
92
tracer = mock .MagicMock ()
96
93
consume_hook = mock .MagicMock ()
@@ -109,19 +106,62 @@ def test_wrap_next(
109
106
* self .args , ** self .kwargs
110
107
)
111
108
self .assertEqual (record , original_next_callback .return_value )
112
- expected_span_name = _get_span_name ("receive" , record .topic )
113
109
114
110
extract .assert_called_once_with (record .headers , getter = _kafka_getter )
115
111
context = extract .return_value
112
+
113
+ _create_consumer_span .assert_called_once_with (
114
+ tracer ,
115
+ consume_hook ,
116
+ record ,
117
+ context ,
118
+ bootstrap_servers ,
119
+ self .args ,
120
+ self .kwargs ,
121
+ )
122
+
123
+ @mock .patch ("opentelemetry.trace.set_span_in_context" )
124
+ @mock .patch ("opentelemetry.context.attach" )
125
+ @mock .patch ("opentelemetry.instrumentation.kafka.utils._enrich_span" )
126
+ @mock .patch ("opentelemetry.context.detach" )
127
+ def test_create_consumer_span (
128
+ self ,
129
+ detach : mock .MagicMock ,
130
+ enrich_span : mock .MagicMock ,
131
+ attach : mock .MagicMock ,
132
+ set_span_in_context : mock .MagicMock ,
133
+ ) -> None :
134
+ tracer = mock .MagicMock ()
135
+ consume_hook = mock .MagicMock ()
136
+ bootstrap_servers = mock .MagicMock ()
137
+ extracted_context = mock .MagicMock ()
138
+ record = mock .MagicMock ()
139
+
140
+ _create_consumer_span (
141
+ tracer ,
142
+ consume_hook ,
143
+ record ,
144
+ extracted_context ,
145
+ bootstrap_servers ,
146
+ self .args ,
147
+ self .kwargs ,
148
+ )
149
+
150
+ expected_span_name = _get_span_name ("receive" , record .topic )
151
+
116
152
tracer .start_as_current_span .assert_called_once_with (
117
- expected_span_name , context = context , kind = SpanKind .CONSUMER
153
+ expected_span_name ,
154
+ context = extracted_context ,
155
+ kind = SpanKind .CONSUMER ,
118
156
)
119
157
span = tracer .start_as_current_span .return_value .__enter__ ()
120
- set_span_in_context .assert_called_once_with (span , context )
158
+ set_span_in_context .assert_called_once_with (span , extracted_context )
121
159
attach .assert_called_once_with (set_span_in_context .return_value )
122
160
123
161
enrich_span .assert_called_once_with (
124
162
span , bootstrap_servers , record .topic , record .partition
125
163
)
126
- consume_hook .assert_called_once_with (span , self .args , self .kwargs )
164
+ consume_hook .assert_called_once_with (
165
+ span , record , self .args , self .kwargs
166
+ )
127
167
detach .assert_called_once_with (attach .return_value )
0 commit comments