28
28
)
29
29
from opentelemetry .trace import SpanKind
30
30
31
+ SEND_RETURN_VALUE = None
32
+
33
+
34
+ async def original_send (
35
+ topic ,
36
+ value = None ,
37
+ key = None ,
38
+ partition = None ,
39
+ timestamp_ms = None ,
40
+ headers = None ,
41
+ ):
42
+ return SEND_RETURN_VALUE
43
+
31
44
32
45
class TestUtils (IsolatedAsyncioTestCase ):
33
46
def setUp (self ) -> None :
@@ -109,6 +122,36 @@ async def test_wrap_send_with_topic_as_kwarg(
109
122
extract_bootstrap_servers ,
110
123
)
111
124
125
+ @mock .patch (
126
+ "opentelemetry.instrumentation.aiokafka.utils._extract_bootstrap_servers"
127
+ )
128
+ @mock .patch (
129
+ "opentelemetry.instrumentation.aiokafka.utils._extract_send_partition"
130
+ )
131
+ @mock .patch (
132
+ "opentelemetry.instrumentation.aiokafka.utils._enrich_send_span"
133
+ )
134
+ @mock .patch ("opentelemetry.trace.set_span_in_context" )
135
+ @mock .patch ("opentelemetry.propagate.inject" )
136
+ async def test_wrap_send_with_headers_as_args (
137
+ self ,
138
+ inject : mock .MagicMock ,
139
+ set_span_in_context : mock .MagicMock ,
140
+ enrich_span : mock .MagicMock ,
141
+ extract_send_partition : mock .AsyncMock ,
142
+ extract_bootstrap_servers : mock .MagicMock ,
143
+ ) -> None :
144
+ # like send_and_wait
145
+ self .args = [self .topic_name , None , None , None , None , None ]
146
+ self .kwargs = {}
147
+ await self .wrap_send_helper (
148
+ inject ,
149
+ set_span_in_context ,
150
+ enrich_span ,
151
+ extract_send_partition ,
152
+ extract_bootstrap_servers ,
153
+ )
154
+
112
155
async def wrap_send_helper (
113
156
self ,
114
157
inject : mock .MagicMock ,
@@ -120,6 +163,8 @@ async def wrap_send_helper(
120
163
tracer = mock .MagicMock ()
121
164
produce_hook = mock .AsyncMock ()
122
165
original_send_callback = mock .AsyncMock ()
166
+ original_send_callback .side_effect = original_send
167
+ original_send_callback .return_value = SEND_RETURN_VALUE
123
168
kafka_producer = mock .MagicMock ()
124
169
expected_span_name = _get_span_name ("send" , self .topic_name )
125
170
@@ -131,9 +176,7 @@ async def wrap_send_helper(
131
176
extract_bootstrap_servers .assert_called_once_with (
132
177
kafka_producer .client
133
178
)
134
- extract_send_partition .assert_awaited_once_with (
135
- kafka_producer , self .args , self .kwargs
136
- )
179
+ extract_send_partition .assert_awaited_once ()
137
180
tracer .start_as_current_span .assert_called_once_with (
138
181
expected_span_name , kind = SpanKind .PRODUCER
139
182
)
@@ -154,11 +197,9 @@ async def wrap_send_helper(
154
197
self .headers , context = context , setter = _aiokafka_setter
155
198
)
156
199
157
- produce_hook .assert_awaited_once_with ( span , self . args , self . kwargs )
200
+ produce_hook .assert_awaited_once ( )
158
201
159
- original_send_callback .assert_awaited_once_with (
160
- * self .args , ** self .kwargs
161
- )
202
+ original_send_callback .assert_awaited_once ()
162
203
self .assertEqual (retval , original_send_callback .return_value )
163
204
164
205
@mock .patch ("opentelemetry.propagate.extract" )
0 commit comments