forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_aiopg_integration.py
508 lines (413 loc) · 17.5 KB
/
test_aiopg_integration.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# 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.
import asyncio
import logging
from unittest import mock
from unittest.mock import MagicMock
import aiopg
from aiopg.utils import _ContextManager, _PoolAcquireContextManager
import opentelemetry.instrumentation.aiopg
from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor, wrappers
from opentelemetry.instrumentation.aiopg.aiopg_integration import (
AiopgIntegration,
)
from opentelemetry.sdk import resources
from opentelemetry.test.test_base import TestBase
def async_call(coro):
loop = asyncio.get_event_loop()
return loop.run_until_complete(coro)
class TestAiopgInstrumentor(TestBase):
def setUp(self):
super().setUp()
self.origin_aiopg_connect = aiopg.connect
self.origin_aiopg_create_pool = aiopg.create_pool
aiopg.connect = mock_connect
aiopg.create_pool = mock_create_pool
def tearDown(self):
super().tearDown()
aiopg.connect = self.origin_aiopg_connect
aiopg.create_pool = self.origin_aiopg_create_pool
with self.disable_logging():
AiopgInstrumentor().uninstrument()
def test_instrumentor_connect(self):
AiopgInstrumentor().instrument()
cnx = async_call(aiopg.connect(database="test"))
cursor = async_call(cnx.cursor())
query = "SELECT * FROM test"
async_call(cursor.execute(query))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
# Check version and name in span's instrumentation info
self.check_span_instrumentation_info(
span, opentelemetry.instrumentation.aiopg
)
# check that no spans are generated after uninstrument
AiopgInstrumentor().uninstrument()
cnx = async_call(aiopg.connect(database="test"))
cursor = async_call(cnx.cursor())
query = "SELECT * FROM test"
cursor.execute(query)
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
def test_instrumentor_create_pool(self):
AiopgInstrumentor().instrument()
pool = async_call(aiopg.create_pool(database="test"))
cnx = async_call(pool.acquire())
cursor = async_call(cnx.cursor())
query = "SELECT * FROM test"
async_call(cursor.execute(query))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
# Check version and name in span's instrumentation info
self.check_span_instrumentation_info(
span, opentelemetry.instrumentation.aiopg
)
# check that no spans are generated after uninstrument
AiopgInstrumentor().uninstrument()
pool = async_call(aiopg.create_pool(database="test"))
cnx = async_call(pool.acquire())
cursor = async_call(cnx.cursor())
query = "SELECT * FROM test"
cursor.execute(query)
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
def test_custom_tracer_provider_connect(self):
resource = resources.Resource.create({})
result = self.create_tracer_provider(resource=resource)
tracer_provider, exporter = result
AiopgInstrumentor().instrument(tracer_provider=tracer_provider)
cnx = async_call(aiopg.connect(database="test"))
cursor = async_call(cnx.cursor())
query = "SELECT * FROM test"
async_call(cursor.execute(query))
spans_list = exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertIs(span.resource, resource)
def test_custom_tracer_provider_create_pool(self):
resource = resources.Resource.create({})
result = self.create_tracer_provider(resource=resource)
tracer_provider, exporter = result
AiopgInstrumentor().instrument(tracer_provider=tracer_provider)
pool = async_call(aiopg.create_pool(database="test"))
cnx = async_call(pool.acquire())
cursor = async_call(cnx.cursor())
query = "SELECT * FROM test"
async_call(cursor.execute(query))
spans_list = exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertIs(span.resource, resource)
def test_instrument_connection(self):
cnx = async_call(aiopg.connect(database="test"))
query = "SELECT * FROM test"
cursor = async_call(cnx.cursor())
async_call(cursor.execute(query))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 0)
cnx = AiopgInstrumentor().instrument_connection(cnx)
cursor = async_call(cnx.cursor())
async_call(cursor.execute(query))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
def test_uninstrument_connection(self):
AiopgInstrumentor().instrument()
cnx = async_call(aiopg.connect(database="test"))
query = "SELECT * FROM test"
cursor = async_call(cnx.cursor())
async_call(cursor.execute(query))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
cnx = AiopgInstrumentor().uninstrument_connection(cnx)
cursor = async_call(cnx.cursor())
async_call(cursor.execute(query))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
class TestAiopgIntegration(TestBase):
def setUp(self):
super().setUp()
self.tracer = self.tracer_provider.get_tracer(__name__)
def test_span_succeeded(self):
connection_props = {
"database": "testdatabase",
"server_host": "testhost",
"server_port": 123,
"user": "testuser",
}
connection_attributes = {
"database": "database",
"port": "server_port",
"host": "server_host",
"user": "user",
}
db_integration = AiopgIntegration(
self.tracer,
"testcomponent",
"testtype",
connection_attributes,
capture_parameters=True,
)
mock_connection = async_call(
db_integration.wrapped_connection(
mock_connect, {}, connection_props
)
)
cursor = async_call(mock_connection.cursor())
async_call(cursor.execute("Test query", ("param1Value", False)))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertEqual(span.name, "testcomponent.testdatabase")
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
self.assertEqual(span.attributes["component"], "testcomponent")
self.assertEqual(span.attributes["db.type"], "testtype")
self.assertEqual(span.attributes["db.instance"], "testdatabase")
self.assertEqual(span.attributes["db.statement"], "Test query")
self.assertEqual(
span.attributes["db.statement.parameters"],
"('param1Value', False)",
)
self.assertEqual(span.attributes["db.user"], "testuser")
self.assertEqual(span.attributes["net.peer.name"], "testhost")
self.assertEqual(span.attributes["net.peer.port"], 123)
self.assertIs(
span.status.status_code, trace_api.status.StatusCode.UNSET,
)
def test_span_not_recording(self):
connection_props = {
"database": "testdatabase",
"server_host": "testhost",
"server_port": 123,
"user": "testuser",
}
connection_attributes = {
"database": "database",
"port": "server_port",
"host": "server_host",
"user": "user",
}
mock_tracer = mock.Mock()
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
db_integration = AiopgIntegration(
mock_tracer, "testcomponent", "testtype", connection_attributes
)
mock_connection = async_call(
db_integration.wrapped_connection(
mock_connect, {}, connection_props
)
)
cursor = async_call(mock_connection.cursor())
async_call(cursor.execute("Test query", ("param1Value", False)))
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)
def test_span_failed(self):
db_integration = AiopgIntegration(self.tracer, "testcomponent")
mock_connection = async_call(
db_integration.wrapped_connection(mock_connect, {}, {})
)
cursor = async_call(mock_connection.cursor())
with self.assertRaises(Exception):
async_call(cursor.execute("Test query", throw_exception=True))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertEqual(span.attributes["db.statement"], "Test query")
self.assertIs(
span.status.status_code, trace_api.status.StatusCode.ERROR,
)
self.assertEqual(span.status.description, "Test Exception")
def test_executemany(self):
db_integration = AiopgIntegration(self.tracer, "testcomponent")
mock_connection = async_call(
db_integration.wrapped_connection(mock_connect, {}, {})
)
cursor = async_call(mock_connection.cursor())
async_call(cursor.executemany("Test query"))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertEqual(span.attributes["db.statement"], "Test query")
def test_callproc(self):
db_integration = AiopgIntegration(self.tracer, "testcomponent")
mock_connection = async_call(
db_integration.wrapped_connection(mock_connect, {}, {})
)
cursor = async_call(mock_connection.cursor())
async_call(cursor.callproc("Test stored procedure"))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertEqual(
span.attributes["db.statement"], "Test stored procedure"
)
def test_wrap_connect(self):
aiopg_mock = AiopgMock()
with mock.patch("aiopg.connect", aiopg_mock.connect):
wrappers.wrap_connect(self.tracer, "-")
connection = async_call(aiopg.connect())
self.assertEqual(aiopg_mock.connect_call_count, 1)
self.assertIsInstance(connection.__wrapped__, mock.Mock)
def test_unwrap_connect(self):
wrappers.wrap_connect(self.tracer, "-")
aiopg_mock = AiopgMock()
with mock.patch("aiopg.connect", aiopg_mock.connect):
connection = async_call(aiopg.connect())
self.assertEqual(aiopg_mock.connect_call_count, 1)
wrappers.unwrap_connect()
connection = async_call(aiopg.connect())
self.assertEqual(aiopg_mock.connect_call_count, 2)
self.assertIsInstance(connection, mock.Mock)
def test_wrap_create_pool(self):
async def check_connection(pool):
async with pool.acquire() as connection:
self.assertEqual(aiopg_mock.create_pool_call_count, 1)
self.assertIsInstance(
connection.__wrapped__, AiopgConnectionMock
)
aiopg_mock = AiopgMock()
with mock.patch("aiopg.create_pool", aiopg_mock.create_pool):
wrappers.wrap_create_pool(self.tracer, "-")
pool = async_call(aiopg.create_pool())
async_call(check_connection(pool))
def test_unwrap_create_pool(self):
async def check_connection(pool):
async with pool.acquire() as connection:
self.assertEqual(aiopg_mock.create_pool_call_count, 2)
self.assertIsInstance(connection, AiopgConnectionMock)
aiopg_mock = AiopgMock()
with mock.patch("aiopg.create_pool", aiopg_mock.create_pool):
wrappers.wrap_create_pool(self.tracer, "-")
pool = async_call(aiopg.create_pool())
self.assertEqual(aiopg_mock.create_pool_call_count, 1)
wrappers.unwrap_create_pool()
pool = async_call(aiopg.create_pool())
async_call(check_connection(pool))
def test_instrument_connection(self):
connection = mock.Mock()
# Avoid get_attributes failing because can't concatenate mock
connection.database = "-"
connection2 = wrappers.instrument_connection(
self.tracer, connection, "-"
)
self.assertIs(connection2.__wrapped__, connection)
def test_uninstrument_connection(self):
connection = mock.Mock()
# Set connection.database to avoid a failure because mock can't
# be concatenated
connection.database = "-"
connection2 = wrappers.instrument_connection(
self.tracer, connection, "-"
)
self.assertIs(connection2.__wrapped__, connection)
connection3 = wrappers.uninstrument_connection(connection2)
self.assertIs(connection3, connection)
with self.assertLogs(level=logging.WARNING):
connection4 = wrappers.uninstrument_connection(connection)
self.assertIs(connection4, connection)
# pylint: disable=unused-argument
async def mock_connect(*args, **kwargs):
database = kwargs.get("database")
server_host = kwargs.get("server_host")
server_port = kwargs.get("server_port")
user = kwargs.get("user")
return MockConnection(database, server_port, server_host, user)
# pylint: disable=unused-argument
async def mock_create_pool(*args, **kwargs):
database = kwargs.get("database")
server_host = kwargs.get("server_host")
server_port = kwargs.get("server_port")
user = kwargs.get("user")
return MockPool(database, server_port, server_host, user)
class MockPool:
def __init__(self, database, server_port, server_host, user):
self.database = database
self.server_port = server_port
self.server_host = server_host
self.user = user
async def release(self, conn):
return conn
def acquire(self):
"""Acquire free connection from the pool."""
coro = self._acquire()
return _PoolAcquireContextManager(coro, self)
async def _acquire(self):
connect = await mock_connect(
self.database, self.server_port, self.server_host, self.user
)
return connect
class MockPsycopg2Connection:
def __init__(self, database, server_port, server_host, user):
self.database = database
self.server_port = server_port
self.server_host = server_host
self.user = user
class MockConnection:
def __init__(self, database, server_port, server_host, user):
self._conn = MockPsycopg2Connection(
database, server_port, server_host, user
)
# pylint: disable=no-self-use
def cursor(self):
coro = self._cursor()
return _ContextManager(coro)
async def _cursor(self):
return MockCursor()
def close(self):
pass
class MockCursor:
# pylint: disable=unused-argument, no-self-use
async def execute(self, query, params=None, throw_exception=False):
if throw_exception:
raise Exception("Test Exception")
# pylint: disable=unused-argument, no-self-use
async def executemany(self, query, params=None, throw_exception=False):
if throw_exception:
raise Exception("Test Exception")
# pylint: disable=unused-argument, no-self-use
async def callproc(self, query, params=None, throw_exception=False):
if throw_exception:
raise Exception("Test Exception")
class AiopgConnectionMock:
_conn = MagicMock()
async def __aexit__(self, exc_type, exc_val, exc_tb):
pass
async def __aenter__(self):
return MagicMock()
class AiopgPoolMock:
async def release(self, conn):
return conn
def acquire(self):
coro = self._acquire()
return _PoolAcquireContextManager(coro, self)
async def _acquire(self):
return AiopgConnectionMock()
class AiopgMock:
def __init__(self):
self.connect_call_count = 0
self.create_pool_call_count = 0
async def connect(self, *args, **kwargs):
self.connect_call_count += 1
return MagicMock()
async def create_pool(self, *args, **kwargs):
self.create_pool_call_count += 1
return AiopgPoolMock()