Skip to content

Commit 962a3ae

Browse files
authored
fix #991 audit asyncpg instrumentation with nooptracerprovider (#3144)
1 parent 41e670a commit 962a3ae

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from asyncpg import Connection, Record, cursor
66
from wrapt import ObjectProxy
77

8+
from opentelemetry import trace as trace_api
89
from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor
910
from opentelemetry.test.test_base import TestBase
1011

@@ -105,3 +106,36 @@ async def exec_mock(*args, **kwargs):
105106
spans = self.memory_exporter.get_finished_spans()
106107
self.assertEqual(len(spans), 2)
107108
self.assertEqual([span.status.is_ok for span in spans], [True, True])
109+
110+
def test_no_op_tracer_provider(self):
111+
AsyncPGInstrumentor().uninstrument()
112+
AsyncPGInstrumentor().instrument(
113+
tracer_provider=trace_api.NoOpTracerProvider()
114+
)
115+
116+
# Mock out all interaction with postgres
117+
async def bind_mock(*args, **kwargs):
118+
return []
119+
120+
async def exec_mock(*args, **kwargs):
121+
return [], None, True
122+
123+
conn = mock.Mock()
124+
conn.is_closed = lambda: False
125+
126+
conn._protocol = mock.Mock()
127+
conn._protocol.bind = bind_mock
128+
conn._protocol.execute = exec_mock
129+
conn._protocol.bind_execute = exec_mock
130+
conn._protocol.close_portal = bind_mock
131+
132+
state = mock.Mock()
133+
state.closed = False
134+
135+
# init the cursor and fetch a single record
136+
crs = cursor.Cursor(conn, "SELECT * FROM test", state, [], Record)
137+
asyncio.run(crs._init(1))
138+
asyncio.run(crs.fetch(1))
139+
140+
spans = self.memory_exporter.get_finished_spans()
141+
self.assertEqual(len(spans), 0)

0 commit comments

Comments
 (0)