Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c305a8e

Browse files
committedJan 8, 2021
Updated dbapi and psycopg2 instrumentations.
Changes: - Update dbapi instrumentation to use the SQL statement name as the span instead of the entire SQL query. - Renamed TracedCursor with CursorTracing. The class was not a valid Cursor so the name was confusing. - Updated CursorTracing's (previously TracedCursor) traced_execution method to accept the cursor instance as the first argument. This is required as for some dbapi implementations, we need a reference to the cursor in order to correctly format the SQL query. - Updated psycopg2 instrumentation to leverage dbapi's `cursor_factory` mechanism instead of wrapping the cursor with wrapt. This results in a simpler instrumentation without monkey patching objects at runtime and allows psycopg2's type registration system to work. This should make it possible to use psycopg2 instrumentation when using the JSONB feature or with frameworks like Django.
1 parent 8abed07 commit c305a8e

File tree

14 files changed

+271
-91
lines changed

14 files changed

+271
-91
lines changed
 

Diff for: ‎.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ lib64
2222
__pycache__
2323
venv*/
2424
.venv*/
25-
opentelemetry-python-core*/
25+
/opentelemetry-python-core*/
2626
/opentelemetry-python-core
2727

2828
# Installer logs

Diff for: ‎.pylintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ disable=missing-docstring,
7878
protected-access, # temp-pylint-upgrade
7979
super-init-not-called, # temp-pylint-upgrade
8080
invalid-overridden-method, # temp-pylint-upgrade
81-
missing-module-docstring, # temp-pylint-upgrad, # temp-pylint-upgradee
81+
missing-module-docstring, # temp-pylint-upgrade
8282

8383
# Enable the message, report, category or checker with the given id(s). You can
8484
# either give multiple identifier separated by comma (,) or put this option

Diff for: ‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5151
([#259](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/259))
5252
- `opentelemetry-exporter-datadog` Fix unintentional type change of span trace flags
5353
([#261](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/261))
54+
- `opentelemetry-instrumentation-dbapi`, `TracedCursor` replaced by `CursorTracer`
55+
([#246](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/246))
56+
- `opentelemetry-instrumentation-psycopg2`, Added support for psycopg2 registered types.
57+
([#246](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/246))
58+
- `opentelemetry-instrumentation-dbapi`, `opentelemetry-instrumentation-psycopg2`, `opentelemetry-instrumentation-mysql`, `opentelemetry-instrumentation-pymysql`, `opentelemetry-instrumentation-aiopg` Use SQL command name as the span operation name instead of the entire query.
59+
([#246](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/246))
5460

5561
## [0.16b1](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.16b1) - 2020-11-26
5662

Diff for: ‎instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from aiopg.utils import _ContextManager, _PoolAcquireContextManager
55

66
from opentelemetry.instrumentation.dbapi import (
7+
CursorTracer,
78
DatabaseApiIntegration,
8-
TracedCursor,
99
)
1010
from opentelemetry.trace import SpanKind
1111
from opentelemetry.trace.status import Status, StatusCode
@@ -94,25 +94,29 @@ async def _acquire(self):
9494
return TracedPoolProxy(pool, *args, **kwargs)
9595

9696

97-
class AsyncTracedCursor(TracedCursor):
97+
class AsyncCursorTracer(CursorTracer):
9898
async def traced_execution(
9999
self,
100+
cursor,
100101
query_method: typing.Callable[..., typing.Any],
101102
*args: typing.Tuple[typing.Any, typing.Any],
102103
**kwargs: typing.Dict[typing.Any, typing.Any]
103104
):
104105
name = ""
105-
if len(args) > 0 and args[0]:
106-
name = args[0]
107-
elif self._db_api_integration.database:
108-
name = self._db_api_integration.database
109-
else:
110-
name = self._db_api_integration.name
106+
if args:
107+
name = self.get_operation_name(cursor, args)
108+
109+
if not name:
110+
name = (
111+
self._db_api_integration.database
112+
if self._db_api_integration.database
113+
else self._db_api_integration.name
114+
)
111115

112116
with self._db_api_integration.get_tracer().start_as_current_span(
113117
name, kind=SpanKind.CLIENT
114118
) as span:
115-
self._populate_span(span, *args)
119+
self._populate_span(span, cursor, *args)
116120
try:
117121
result = await query_method(*args, **kwargs)
118122
return result
@@ -123,7 +127,7 @@ async def traced_execution(
123127

124128

125129
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):
126-
_traced_cursor = AsyncTracedCursor(db_api_integration)
130+
_traced_cursor = AsyncCursorTracer(db_api_integration)
127131

128132
# pylint: disable=abstract-method
129133
class AsyncTracedCursorProxy(AsyncProxyObject):
@@ -134,19 +138,19 @@ def __init__(self, cursor, *args, **kwargs):
134138

135139
async def execute(self, *args, **kwargs):
136140
result = await _traced_cursor.traced_execution(
137-
self.__wrapped__.execute, *args, **kwargs
141+
self, self.__wrapped__.execute, *args, **kwargs
138142
)
139143
return result
140144

141145
async def executemany(self, *args, **kwargs):
142146
result = await _traced_cursor.traced_execution(
143-
self.__wrapped__.executemany, *args, **kwargs
147+
self, self.__wrapped__.executemany, *args, **kwargs
144148
)
145149
return result
146150

147151
async def callproc(self, *args, **kwargs):
148152
result = await _traced_cursor.traced_execution(
149-
self.__wrapped__.callproc, *args, **kwargs
153+
self, self.__wrapped__.callproc, *args, **kwargs
150154
)
151155
return result
152156

Diff for: ‎instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def test_span_succeeded(self):
215215
spans_list = self.memory_exporter.get_finished_spans()
216216
self.assertEqual(len(spans_list), 1)
217217
span = spans_list[0]
218-
self.assertEqual(span.name, "Test query")
218+
self.assertEqual(span.name, "Test")
219219
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
220220

221221
self.assertEqual(span.attributes["component"], "testcomponent")

Diff for: ‎instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

+42-19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def trace_integration(
6060
connection_attributes: typing.Dict = None,
6161
tracer_provider: typing.Optional[TracerProvider] = None,
6262
capture_parameters: bool = False,
63+
db_api_integration_factory=None,
6364
):
6465
"""Integrate with DB API library.
6566
https://www.python.org/dev/peps/pep-0249/
@@ -86,6 +87,7 @@ def trace_integration(
8687
version=__version__,
8788
tracer_provider=tracer_provider,
8889
capture_parameters=capture_parameters,
90+
db_api_integration_factory=db_api_integration_factory,
8991
)
9092

9193

@@ -99,6 +101,7 @@ def wrap_connect(
99101
version: str = "",
100102
tracer_provider: typing.Optional[TracerProvider] = None,
101103
capture_parameters: bool = False,
104+
db_api_integration_factory=None,
102105
):
103106
"""Integrate with DB API library.
104107
https://www.python.org/dev/peps/pep-0249/
@@ -115,6 +118,9 @@ def wrap_connect(
115118
capture_parameters: Configure if db.statement.parameters should be captured.
116119
117120
"""
121+
db_api_integration_factory = (
122+
db_api_integration_factory or DatabaseApiIntegration
123+
)
118124

119125
# pylint: disable=unused-argument
120126
def wrap_connect_(
@@ -123,7 +129,7 @@ def wrap_connect_(
123129
args: typing.Tuple[typing.Any, typing.Any],
124130
kwargs: typing.Dict[typing.Any, typing.Any],
125131
):
126-
db_integration = DatabaseApiIntegration(
132+
db_integration = db_api_integration_factory(
127133
name,
128134
database_component,
129135
database_type=database_type,
@@ -314,16 +320,19 @@ def __exit__(self, *args, **kwargs):
314320
return TracedConnectionProxy(connection, *args, **kwargs)
315321

316322

317-
class TracedCursor:
323+
class CursorTracer:
318324
def __init__(self, db_api_integration: DatabaseApiIntegration):
319325
self._db_api_integration = db_api_integration
320326

321327
def _populate_span(
322-
self, span: trace_api.Span, *args: typing.Tuple[typing.Any, typing.Any]
328+
self,
329+
span: trace_api.Span,
330+
cursor,
331+
*args: typing.Tuple[typing.Any, typing.Any]
323332
):
324333
if not span.is_recording():
325334
return
326-
statement = args[0] if args else ""
335+
statement = self.get_statement(cursor, args)
327336
span.set_attribute(
328337
"component", self._db_api_integration.database_component
329338
)
@@ -342,24 +351,38 @@ def _populate_span(
342351
if self._db_api_integration.capture_parameters and len(args) > 1:
343352
span.set_attribute("db.statement.parameters", str(args[1]))
344353

354+
def get_operation_name(self, cursor, args): # pylint: disable=no-self-use
355+
if args and isinstance(args[0], str):
356+
return args[0].split(" ")[0]
357+
return ""
358+
359+
def get_statement(self, cursor, args): # pylint: disable=no-self-use
360+
if not args:
361+
return ""
362+
statement = args[0]
363+
if isinstance(statement, bytes):
364+
return statement.decode("utf8", "replace")
365+
return statement
366+
345367
def traced_execution(
346368
self,
369+
cursor,
347370
query_method: typing.Callable[..., typing.Any],
348371
*args: typing.Tuple[typing.Any, typing.Any],
349372
**kwargs: typing.Dict[typing.Any, typing.Any]
350373
):
351-
name = ""
352-
if args:
353-
name = args[0]
354-
elif self._db_api_integration.database:
355-
name = self._db_api_integration.database
356-
else:
357-
name = self._db_api_integration.name
374+
name = self.get_operation_name(cursor, args)
375+
if not name:
376+
name = (
377+
self._db_api_integration.database
378+
if self._db_api_integration.database
379+
else self._db_api_integration.name
380+
)
358381

359382
with self._db_api_integration.get_tracer().start_as_current_span(
360383
name, kind=SpanKind.CLIENT
361384
) as span:
362-
self._populate_span(span, *args)
385+
self._populate_span(span, cursor, *args)
363386
try:
364387
result = query_method(*args, **kwargs)
365388
return result
@@ -370,7 +393,7 @@ def traced_execution(
370393

371394

372395
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):
373-
_traced_cursor = TracedCursor(db_api_integration)
396+
_cursor_tracer = CursorTracer(db_api_integration)
374397

375398
# pylint: disable=abstract-method
376399
class TracedCursorProxy(wrapt.ObjectProxy):
@@ -380,18 +403,18 @@ def __init__(self, cursor, *args, **kwargs):
380403
wrapt.ObjectProxy.__init__(self, cursor)
381404

382405
def execute(self, *args, **kwargs):
383-
return _traced_cursor.traced_execution(
384-
self.__wrapped__.execute, *args, **kwargs
406+
return _cursor_tracer.traced_execution(
407+
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs
385408
)
386409

387410
def executemany(self, *args, **kwargs):
388-
return _traced_cursor.traced_execution(
389-
self.__wrapped__.executemany, *args, **kwargs
411+
return _cursor_tracer.traced_execution(
412+
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs
390413
)
391414

392415
def callproc(self, *args, **kwargs):
393-
return _traced_cursor.traced_execution(
394-
self.__wrapped__.callproc, *args, **kwargs
416+
return _cursor_tracer.traced_execution(
417+
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs
395418
)
396419

397420
def __enter__(self):

Diff for: ‎instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_span_succeeded(self):
5050
spans_list = self.memory_exporter.get_finished_spans()
5151
self.assertEqual(len(spans_list), 1)
5252
span = spans_list[0]
53-
self.assertEqual(span.name, "Test query")
53+
self.assertEqual(span.name, "Test")
5454
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
5555

5656
self.assertEqual(span.attributes["component"], "testcomponent")
@@ -93,7 +93,7 @@ def test_span_succeeded_with_capture_of_statement_parameters(self):
9393
spans_list = self.memory_exporter.get_finished_spans()
9494
self.assertEqual(len(spans_list), 1)
9595
span = spans_list[0]
96-
self.assertEqual(span.name, "Test query")
96+
self.assertEqual(span.name, "Test")
9797
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
9898

9999
self.assertEqual(span.attributes["component"], "testcomponent")

Diff for: ‎instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py

+93-27
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@
3939
---
4040
"""
4141

42+
import typing
43+
4244
import psycopg2
45+
from psycopg2.extensions import (
46+
cursor as pg_cursor, # pylint: disable=no-name-in-module
47+
)
48+
from psycopg2.sql import Composed # pylint: disable=no-name-in-module
4349

4450
from opentelemetry.instrumentation import dbapi
4551
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
4652
from opentelemetry.instrumentation.psycopg2.version import __version__
47-
from opentelemetry.trace import get_tracer
53+
54+
OTEL_CURSOR_FACTORY_KEY = "_otel_orig_cursor_factory"
4855

4956

5057
class Psycopg2Instrumentor(BaseInstrumentor):
@@ -62,7 +69,6 @@ def _instrument(self, **kwargs):
6269
"""Integrate with PostgreSQL Psycopg library.
6370
Psycopg: http://initd.org/psycopg/
6471
"""
65-
6672
tracer_provider = kwargs.get("tracer_provider")
6773

6874
dbapi.wrap_connect(
@@ -74,39 +80,99 @@ def _instrument(self, **kwargs):
7480
self._CONNECTION_ATTRIBUTES,
7581
version=__version__,
7682
tracer_provider=tracer_provider,
83+
db_api_integration_factory=DatabaseApiIntegration,
7784
)
7885

7986
def _uninstrument(self, **kwargs):
8087
""""Disable Psycopg2 instrumentation"""
8188
dbapi.unwrap_connect(psycopg2, "connect")
8289

83-
# pylint:disable=no-self-use
84-
def instrument_connection(self, connection):
85-
"""Enable instrumentation in a Psycopg2 connection.
86-
87-
Args:
88-
connection: The connection to instrument.
90+
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
91+
def instrument_connection(self, connection): # pylint: disable=no-self-use
92+
setattr(connection, OTEL_CURSOR_FACTORY_KEY, connection.cursor_factory)
93+
connection.cursor_factory = _new_cursor_factory()
94+
return connection
95+
96+
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
97+
def uninstrument_connection(
98+
self, connection
99+
): # pylint: disable=no-self-use
100+
connection.cursor_factory = getattr(
101+
connection, OTEL_CURSOR_FACTORY_KEY, None
102+
)
103+
return connection
104+
105+
106+
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
107+
class DatabaseApiIntegration(dbapi.DatabaseApiIntegration):
108+
def wrapped_connection(
109+
self,
110+
connect_method: typing.Callable[..., typing.Any],
111+
args: typing.Tuple[typing.Any, typing.Any],
112+
kwargs: typing.Dict[typing.Any, typing.Any],
113+
):
114+
"""Add object proxy to connection object."""
115+
base_cursor_factory = kwargs.pop("cursor_factory", None)
116+
new_factory_kwargs = {"db_api": self}
117+
if base_cursor_factory:
118+
new_factory_kwargs["base_factory"] = base_cursor_factory
119+
kwargs["cursor_factory"] = _new_cursor_factory(**new_factory_kwargs)
120+
connection = connect_method(*args, **kwargs)
121+
self.get_connection_attributes(connection)
122+
return connection
123+
124+
125+
class CursorTracer(dbapi.CursorTracer):
126+
def get_operation_name(self, cursor, args):
127+
if not args:
128+
return ""
129+
130+
statement = args[0]
131+
if isinstance(statement, Composed):
132+
statement = statement.as_string(cursor)
133+
134+
if isinstance(statement, str):
135+
return statement.split(" ")[0]
136+
137+
return ""
138+
139+
def get_statement(self, cursor, args):
140+
if not args:
141+
return ""
142+
143+
statement = args[0]
144+
if isinstance(statement, Composed):
145+
statement = statement.as_string(cursor)
146+
return statement
147+
148+
149+
def _new_cursor_factory(db_api=None, base_factory=None):
150+
if not db_api:
151+
db_api = DatabaseApiIntegration(
152+
__name__,
153+
Psycopg2Instrumentor._DATABASE_COMPONENT,
154+
database_type=Psycopg2Instrumentor._DATABASE_TYPE,
155+
connection_attributes=Psycopg2Instrumentor._CONNECTION_ATTRIBUTES,
156+
version=__version__,
157+
)
89158

90-
Returns:
91-
An instrumented connection.
92-
"""
93-
tracer = get_tracer(__name__, __version__)
159+
base_factory = base_factory or pg_cursor
160+
_cursor_tracer = CursorTracer(db_api)
94161

95-
return dbapi.instrument_connection(
96-
tracer,
97-
connection,
98-
self._DATABASE_COMPONENT,
99-
self._DATABASE_TYPE,
100-
self._CONNECTION_ATTRIBUTES,
101-
)
162+
class TracedCursorFactory(base_factory):
163+
def execute(self, *args, **kwargs):
164+
return _cursor_tracer.traced_execution(
165+
self, super().execute, *args, **kwargs
166+
)
102167

103-
def uninstrument_connection(self, connection):
104-
"""Disable instrumentation in a Psycopg2 connection.
168+
def executemany(self, *args, **kwargs):
169+
return _cursor_tracer.traced_execution(
170+
self, super().executemany, *args, **kwargs
171+
)
105172

106-
Args:
107-
connection: The connection to uninstrument.
173+
def callproc(self, *args, **kwargs):
174+
return _cursor_tracer.traced_execution(
175+
self, super().callproc, *args, **kwargs
176+
)
108177

109-
Returns:
110-
An uninstrumented connection.
111-
"""
112-
return dbapi.uninstrument_connection(connection)
178+
return TracedCursorFactory

Diff for: ‎instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py

+91-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import types
1516
from unittest import mock
1617

1718
import psycopg2
@@ -22,15 +23,99 @@
2223
from opentelemetry.test.test_base import TestBase
2324

2425

26+
class MockCursor:
27+
28+
execute = mock.MagicMock(spec=types.MethodType)
29+
execute.__name__ = "execute"
30+
31+
executemany = mock.MagicMock(spec=types.MethodType)
32+
executemany.__name__ = "executemany"
33+
34+
callproc = mock.MagicMock(spec=types.MethodType)
35+
callproc.__name__ = "callproc"
36+
37+
rowcount = "SomeRowCount"
38+
39+
def __init__(self, *args, **kwargs):
40+
pass
41+
42+
def __enter__(self):
43+
return self
44+
45+
def __exit__(self, *args):
46+
return self
47+
48+
49+
class MockConnection:
50+
51+
commit = mock.MagicMock(spec=types.MethodType)
52+
commit.__name__ = "commit"
53+
54+
rollback = mock.MagicMock(spec=types.MethodType)
55+
rollback.__name__ = "rollback"
56+
57+
def __init__(self, *args, **kwargs):
58+
self.cursor_factory = kwargs.pop("cursor_factory", None)
59+
60+
def cursor(self):
61+
if self.cursor_factory:
62+
return self.cursor_factory(self)
63+
return MockCursor()
64+
65+
def get_dsn_parameters(self): # pylint: disable=no-self-use
66+
return dict(dbname="test")
67+
68+
2569
class TestPostgresqlIntegration(TestBase):
70+
def setUp(self):
71+
self.cursor_mock = mock.patch(
72+
"opentelemetry.instrumentation.psycopg2.pg_cursor", MockCursor
73+
)
74+
self.connection_mock = mock.patch("psycopg2.connect", MockConnection)
75+
76+
self.cursor_mock.start()
77+
self.connection_mock.start()
78+
2679
def tearDown(self):
2780
super().tearDown()
81+
self.memory_exporter.clear()
82+
self.cursor_mock.stop()
83+
self.connection_mock.stop()
2884
with self.disable_logging():
2985
Psycopg2Instrumentor().uninstrument()
3086

31-
@mock.patch("psycopg2.connect")
3287
# pylint: disable=unused-argument
33-
def test_instrumentor(self, mock_connect):
88+
def test_instrumentor(self):
89+
Psycopg2Instrumentor().instrument()
90+
91+
cnx = psycopg2.connect(database="test")
92+
93+
cursor = cnx.cursor()
94+
95+
query = "SELECT * FROM test"
96+
cursor.execute(query)
97+
98+
spans_list = self.memory_exporter.get_finished_spans()
99+
self.assertEqual(len(spans_list), 1)
100+
span = spans_list[0]
101+
102+
# Check version and name in span's instrumentation info
103+
self.check_span_instrumentation_info(
104+
span, opentelemetry.instrumentation.psycopg2
105+
)
106+
107+
# check that no spans are generated after uninstrument
108+
Psycopg2Instrumentor().uninstrument()
109+
110+
cnx = psycopg2.connect(database="test")
111+
cursor = cnx.cursor()
112+
query = "SELECT * FROM test"
113+
cursor.execute(query)
114+
115+
spans_list = self.memory_exporter.get_finished_spans()
116+
self.assertEqual(len(spans_list), 1)
117+
118+
def test_composed(self):
34119
Psycopg2Instrumentor().instrument()
35120

36121
cnx = psycopg2.connect(database="test")
@@ -60,9 +145,8 @@ def test_instrumentor(self, mock_connect):
60145
spans_list = self.memory_exporter.get_finished_spans()
61146
self.assertEqual(len(spans_list), 1)
62147

63-
@mock.patch("psycopg2.connect")
64148
# pylint: disable=unused-argument
65-
def test_not_recording(self, mock_connect):
149+
def test_not_recording(self):
66150
mock_tracer = mock.Mock()
67151
mock_span = mock.Mock()
68152
mock_span.is_recording.return_value = False
@@ -83,9 +167,8 @@ def test_not_recording(self, mock_connect):
83167

84168
Psycopg2Instrumentor().uninstrument()
85169

86-
@mock.patch("psycopg2.connect")
87170
# pylint: disable=unused-argument
88-
def test_custom_tracer_provider(self, mock_connect):
171+
def test_custom_tracer_provider(self):
89172
resource = resources.Resource.create({})
90173
result = self.create_tracer_provider(resource=resource)
91174
tracer_provider, exporter = result
@@ -103,9 +186,8 @@ def test_custom_tracer_provider(self, mock_connect):
103186

104187
self.assertIs(span.resource, resource)
105188

106-
@mock.patch("psycopg2.connect")
107189
# pylint: disable=unused-argument
108-
def test_instrument_connection(self, mock_connect):
190+
def test_instrument_connection(self):
109191
cnx = psycopg2.connect(database="test")
110192
query = "SELECT * FROM test"
111193
cursor = cnx.cursor()
@@ -121,9 +203,8 @@ def test_instrument_connection(self, mock_connect):
121203
spans_list = self.memory_exporter.get_finished_spans()
122204
self.assertEqual(len(spans_list), 1)
123205

124-
@mock.patch("psycopg2.connect")
125206
# pylint: disable=unused-argument
126-
def test_uninstrument_connection(self, mock_connect):
207+
def test_uninstrument_connection(self):
127208
Psycopg2Instrumentor().instrument()
128209
cnx = psycopg2.connect(database="test")
129210
query = "SELECT * FROM test"

Diff for: ‎instrumentation/opentelemetry-instrumentation-sqlite3/tests/test_sqlite3.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ def test_execute(self):
6060
stmt = "CREATE TABLE IF NOT EXISTS test (id integer)"
6161
with self._tracer.start_as_current_span("rootSpan"):
6262
self._cursor.execute(stmt)
63-
self.validate_spans(stmt)
63+
self.validate_spans("CREATE")
6464

6565
def test_executemany(self):
6666
"""Should create a child span for executemany"""
6767
stmt = "INSERT INTO test (id) VALUES (?)"
6868
with self._tracer.start_as_current_span("rootSpan"):
6969
data = [("1",), ("2",), ("3",)]
7070
self._cursor.executemany(stmt, data)
71-
self.validate_spans(stmt)
71+
self.validate_spans("INSERT")
7272

7373
def test_callproc(self):
7474
"""Should create a child span for callproc"""

Diff for: ‎tests/opentelemetry-docker-tests/tests/mysql/test_mysql_functional.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_execute(self):
8181
stmt = "CREATE TABLE IF NOT EXISTS test (id INT)"
8282
with self._tracer.start_as_current_span("rootSpan"):
8383
self._cursor.execute(stmt)
84-
self.validate_spans(stmt)
84+
self.validate_spans("CREATE")
8585

8686
def test_execute_with_connection_context_manager(self):
8787
"""Should create a child span for execute with connection context"""
@@ -90,23 +90,23 @@ def test_execute_with_connection_context_manager(self):
9090
with self._connection as conn:
9191
cursor = conn.cursor()
9292
cursor.execute(stmt)
93-
self.validate_spans(stmt)
93+
self.validate_spans("CREATE")
9494

9595
def test_execute_with_cursor_context_manager(self):
9696
"""Should create a child span for execute with cursor context"""
9797
stmt = "CREATE TABLE IF NOT EXISTS test (id INT)"
9898
with self._tracer.start_as_current_span("rootSpan"):
9999
with self._connection.cursor() as cursor:
100100
cursor.execute(stmt)
101-
self.validate_spans(stmt)
101+
self.validate_spans("CREATE")
102102

103103
def test_executemany(self):
104104
"""Should create a child span for executemany"""
105105
stmt = "INSERT INTO test (id) VALUES (%s)"
106106
with self._tracer.start_as_current_span("rootSpan"):
107107
data = (("1",), ("2",), ("3",))
108108
self._cursor.executemany(stmt, data)
109-
self.validate_spans(stmt)
109+
self.validate_spans("INSERT")
110110

111111
def test_callproc(self):
112112
"""Should create a child span for callproc"""

Diff for: ‎tests/opentelemetry-docker-tests/tests/postgres/test_aiopg_functional.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_execute(self):
8989
stmt = "CREATE TABLE IF NOT EXISTS test (id integer)"
9090
with self._tracer.start_as_current_span("rootSpan"):
9191
async_call(self._cursor.execute(stmt))
92-
self.validate_spans(stmt)
92+
self.validate_spans("CREATE")
9393

9494
def test_executemany(self):
9595
"""Should create a child span for executemany"""
@@ -98,7 +98,7 @@ def test_executemany(self):
9898
with self._tracer.start_as_current_span("rootSpan"):
9999
data = (("1",), ("2",), ("3",))
100100
async_call(self._cursor.executemany(stmt, data))
101-
self.validate_spans(stmt)
101+
self.validate_spans("INSERT")
102102

103103
def test_callproc(self):
104104
"""Should create a child span for callproc"""
@@ -167,7 +167,7 @@ def test_execute(self):
167167
stmt = "CREATE TABLE IF NOT EXISTS test (id integer)"
168168
with self._tracer.start_as_current_span("rootSpan"):
169169
async_call(self._cursor.execute(stmt))
170-
self.validate_spans(stmt)
170+
self.validate_spans("CREATE")
171171

172172
def test_executemany(self):
173173
"""Should create a child span for executemany"""
@@ -176,7 +176,7 @@ def test_executemany(self):
176176
with self._tracer.start_as_current_span("rootSpan"):
177177
data = (("1",), ("2",), ("3",))
178178
async_call(self._cursor.executemany(stmt, data))
179-
self.validate_spans(stmt)
179+
self.validate_spans("INSERT")
180180

181181
def test_callproc(self):
182182
"""Should create a child span for callproc"""

Diff for: ‎tests/opentelemetry-docker-tests/tests/postgres/test_psycopg_functional.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_execute(self):
8181
stmt = "CREATE TABLE IF NOT EXISTS test (id integer)"
8282
with self._tracer.start_as_current_span("rootSpan"):
8383
self._cursor.execute(stmt)
84-
self.validate_spans(stmt)
84+
self.validate_spans("CREATE")
8585

8686
def test_execute_with_connection_context_manager(self):
8787
"""Should create a child span for execute with connection context"""
@@ -90,15 +90,15 @@ def test_execute_with_connection_context_manager(self):
9090
with self._connection as conn:
9191
cursor = conn.cursor()
9292
cursor.execute(stmt)
93-
self.validate_spans(stmt)
93+
self.validate_spans("CREATE")
9494

9595
def test_execute_with_cursor_context_manager(self):
9696
"""Should create a child span for execute with cursor context"""
9797
stmt = "CREATE TABLE IF NOT EXISTS test (id INT)"
9898
with self._tracer.start_as_current_span("rootSpan"):
9999
with self._connection.cursor() as cursor:
100100
cursor.execute(stmt)
101-
self.validate_spans(stmt)
101+
self.validate_spans("CREATE")
102102
self.assertTrue(cursor.closed)
103103

104104
def test_executemany(self):
@@ -107,7 +107,7 @@ def test_executemany(self):
107107
with self._tracer.start_as_current_span("rootSpan"):
108108
data = (("1",), ("2",), ("3",))
109109
self._cursor.executemany(stmt, data)
110-
self.validate_spans(stmt)
110+
self.validate_spans("INSERT")
111111

112112
def test_callproc(self):
113113
"""Should create a child span for callproc"""

Diff for: ‎tests/opentelemetry-docker-tests/tests/pymysql/test_pymysql_functional.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,23 @@ def test_execute(self):
7878
stmt = "CREATE TABLE IF NOT EXISTS test (id INT)"
7979
with self._tracer.start_as_current_span("rootSpan"):
8080
self._cursor.execute(stmt)
81-
self.validate_spans(stmt)
81+
self.validate_spans("CREATE")
8282

8383
def test_execute_with_cursor_context_manager(self):
8484
"""Should create a child span for execute with cursor context"""
8585
stmt = "CREATE TABLE IF NOT EXISTS test (id INT)"
8686
with self._tracer.start_as_current_span("rootSpan"):
8787
with self._connection.cursor() as cursor:
8888
cursor.execute(stmt)
89-
self.validate_spans(stmt)
89+
self.validate_spans("CREATE")
9090

9191
def test_executemany(self):
9292
"""Should create a child span for executemany"""
9393
stmt = "INSERT INTO test (id) VALUES (%s)"
9494
with self._tracer.start_as_current_span("rootSpan"):
9595
data = (("1",), ("2",), ("3",))
9696
self._cursor.executemany(stmt, data)
97-
self.validate_spans(stmt)
97+
self.validate_spans("INSERT")
9898

9999
def test_callproc(self):
100100
"""Should create a child span for callproc"""

0 commit comments

Comments
 (0)
Please sign in to comment.