forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_aio_client_interceptor.py
345 lines (278 loc) · 11.8 KB
/
test_aio_client_interceptor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
try:
from unittest import IsolatedAsyncioTestCase
except ImportError:
# unittest.IsolatedAsyncioTestCase was introduced in Python 3.8. It's use
# simplifies the following tests. Without it, the amount of test code
# increases significantly, with most of the additional code handling
# the asyncio set up.
from unittest import TestCase
class IsolatedAsyncioTestCase(TestCase):
def run(self, result=None):
self.skipTest(
"This test requires Python 3.8 for unittest.IsolatedAsyncioTestCase"
)
import grpc
import pytest
import opentelemetry.instrumentation.grpc
from opentelemetry import trace
from opentelemetry.instrumentation.grpc import (
GrpcAioInstrumentorClient,
aio_client_interceptors,
)
from opentelemetry.instrumentation.grpc._aio_client import (
UnaryUnaryAioClientInterceptor,
)
from opentelemetry.instrumentation.utils import suppress_instrumentation
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.mock_textmap import MockTextMapPropagator
from opentelemetry.test.test_base import TestBase
from ._aio_client import (
bidirectional_streaming_method,
client_streaming_method,
server_streaming_method,
simple_method,
)
from ._server import create_test_server
from .protobuf import test_server_pb2_grpc # pylint: disable=no-name-in-module
class RecordingInterceptor(grpc.aio.UnaryUnaryClientInterceptor):
recorded_details = None
async def intercept_unary_unary(
self, continuation, client_call_details, request
):
self.recorded_details = client_call_details
return await continuation(client_call_details, request)
@pytest.mark.asyncio
class TestAioClientInterceptor(TestBase, IsolatedAsyncioTestCase):
def setUp(self):
super().setUp()
self.server = create_test_server(25565)
self.server.start()
interceptors = aio_client_interceptors()
self._channel = grpc.aio.insecure_channel(
"localhost:25565", interceptors=interceptors
)
self._stub = test_server_pb2_grpc.GRPCTestServerStub(self._channel)
def tearDown(self):
super().tearDown()
self.server.stop(1000)
async def asyncTearDown(self):
await self._channel.close()
async def test_instrument(self):
instrumentor = GrpcAioInstrumentorClient()
try:
instrumentor.instrument()
channel = grpc.aio.insecure_channel("localhost:25565")
stub = test_server_pb2_grpc.GRPCTestServerStub(channel)
response = await simple_method(stub)
assert response.response_data == "data"
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
finally:
instrumentor.uninstrument()
async def test_uninstrument(self):
instrumentor = GrpcAioInstrumentorClient()
instrumentor.instrument()
instrumentor.uninstrument()
channel = grpc.aio.insecure_channel("localhost:25565")
stub = test_server_pb2_grpc.GRPCTestServerStub(channel)
response = await simple_method(stub)
assert response.response_data == "data"
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
async def test_unary_unary(self):
response = await simple_method(self._stub)
assert response.response_data == "data"
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.name, "/GRPCTestServer/SimpleMethod")
self.assertIs(span.kind, trace.SpanKind.CLIENT)
# Check version and name in span's instrumentation info
self.assertEqualSpanInstrumentationInfo(
span, opentelemetry.instrumentation.grpc
)
self.assertSpanHasAttributes(
span,
{
SpanAttributes.RPC_METHOD: "SimpleMethod",
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
0
],
},
)
async def test_unary_stream(self):
async for response in server_streaming_method(self._stub):
self.assertEqual(response.response_data, "data")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.name, "/GRPCTestServer/ServerStreamingMethod")
self.assertIs(span.kind, trace.SpanKind.CLIENT)
# Check version and name in span's instrumentation info
self.assertEqualSpanInstrumentationInfo(
span, opentelemetry.instrumentation.grpc
)
self.assertSpanHasAttributes(
span,
{
SpanAttributes.RPC_METHOD: "ServerStreamingMethod",
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
0
],
},
)
async def test_stream_unary(self):
response = await client_streaming_method(self._stub)
assert response.response_data == "data"
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.name, "/GRPCTestServer/ClientStreamingMethod")
self.assertIs(span.kind, trace.SpanKind.CLIENT)
# Check version and name in span's instrumentation info
self.assertEqualSpanInstrumentationInfo(
span, opentelemetry.instrumentation.grpc
)
self.assertSpanHasAttributes(
span,
{
SpanAttributes.RPC_METHOD: "ClientStreamingMethod",
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
0
],
},
)
async def test_stream_stream(self):
async for response in bidirectional_streaming_method(self._stub):
self.assertEqual(response.response_data, "data")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(
span.name, "/GRPCTestServer/BidirectionalStreamingMethod"
)
self.assertIs(span.kind, trace.SpanKind.CLIENT)
# Check version and name in span's instrumentation info
self.assertEqualSpanInstrumentationInfo(
span, opentelemetry.instrumentation.grpc
)
self.assertSpanHasAttributes(
span,
{
SpanAttributes.RPC_METHOD: "BidirectionalStreamingMethod",
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
0
],
},
)
async def test_error_simple(self):
with self.assertRaises(grpc.RpcError):
await simple_method(self._stub, error=True)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertIs(
span.status.status_code,
trace.StatusCode.ERROR,
)
async def test_error_unary_stream(self):
with self.assertRaises(grpc.RpcError):
async for _ in server_streaming_method(self._stub, error=True):
pass
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertIs(
span.status.status_code,
trace.StatusCode.ERROR,
)
async def test_error_stream_unary(self):
with self.assertRaises(grpc.RpcError):
await client_streaming_method(self._stub, error=True)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertIs(
span.status.status_code,
trace.StatusCode.ERROR,
)
async def test_error_stream_stream(self):
with self.assertRaises(grpc.RpcError):
async for _ in bidirectional_streaming_method(
self._stub, error=True
):
pass
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertIs(
span.status.status_code,
trace.StatusCode.ERROR,
)
# pylint:disable=no-self-use
async def test_client_interceptor_trace_context_propagation(self):
"""ensure that client interceptor correctly inject trace context into all outgoing requests."""
previous_propagator = get_global_textmap()
try:
set_global_textmap(MockTextMapPropagator())
interceptor = UnaryUnaryAioClientInterceptor(trace.NoOpTracer())
recording_interceptor = RecordingInterceptor()
interceptors = [interceptor, recording_interceptor]
channel = grpc.aio.insecure_channel(
"localhost:25565", interceptors=interceptors
)
stub = test_server_pb2_grpc.GRPCTestServerStub(channel)
await simple_method(stub)
metadata = recording_interceptor.recorded_details.metadata
assert len(metadata) == 3
assert metadata.get_all("key") == ["value"]
assert metadata.get_all("mock-traceid") == ["0"]
assert metadata.get_all("mock-spanid") == ["0"]
finally:
set_global_textmap(previous_propagator)
async def test_unary_unary_with_suppress_key(self):
with suppress_instrumentation():
response = await simple_method(self._stub)
assert response.response_data == "data"
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
async def test_unary_stream_with_suppress_key(self):
with suppress_instrumentation():
async for response in server_streaming_method(self._stub):
self.assertEqual(response.response_data, "data")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
async def test_stream_unary_with_suppress_key(self):
with suppress_instrumentation():
response = await client_streaming_method(self._stub)
assert response.response_data == "data"
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
async def test_stream_stream_with_suppress_key(self):
with suppress_instrumentation():
async for response in bidirectional_streaming_method(self._stub):
self.assertEqual(response.response_data, "data")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)