From 962a3aecdb3cd99bc0590303106be27c098a71f8 Mon Sep 17 00:00:00 2001 From: Will Li Date: Fri, 10 Jan 2025 06:51:23 +0800 Subject: [PATCH] fix #991 audit asyncpg instrumentation with nooptracerprovider (#3144) --- .../tests/test_asyncpg_wrapper.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py b/instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py index 7c88b9c005..0fc44d6a23 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py @@ -5,6 +5,7 @@ from asyncpg import Connection, Record, cursor from wrapt import ObjectProxy +from opentelemetry import trace as trace_api from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor from opentelemetry.test.test_base import TestBase @@ -105,3 +106,36 @@ async def exec_mock(*args, **kwargs): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 2) self.assertEqual([span.status.is_ok for span in spans], [True, True]) + + def test_no_op_tracer_provider(self): + AsyncPGInstrumentor().uninstrument() + AsyncPGInstrumentor().instrument( + tracer_provider=trace_api.NoOpTracerProvider() + ) + + # Mock out all interaction with postgres + async def bind_mock(*args, **kwargs): + return [] + + async def exec_mock(*args, **kwargs): + return [], None, True + + conn = mock.Mock() + conn.is_closed = lambda: False + + conn._protocol = mock.Mock() + conn._protocol.bind = bind_mock + conn._protocol.execute = exec_mock + conn._protocol.bind_execute = exec_mock + conn._protocol.close_portal = bind_mock + + state = mock.Mock() + state.closed = False + + # init the cursor and fetch a single record + crs = cursor.Cursor(conn, "SELECT * FROM test", state, [], Record) + asyncio.run(crs._init(1)) + asyncio.run(crs.fetch(1)) + + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 0)