|
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 | from asyncpg import Connection, Record, cursor |
| 6 | +from asyncpg.prepared_stmt import PreparedStatement |
6 | 7 |
|
7 | 8 | try: |
8 | 9 | # wrapt 2.0.0+ |
|
11 | 12 | from wrapt import ObjectProxy as BaseObjectProxy |
12 | 13 |
|
13 | 14 | from opentelemetry import trace as trace_api |
14 | | -from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor |
| 15 | +from opentelemetry.instrumentation.asyncpg import ( |
| 16 | + _PREPARED_STMT_METHODS, |
| 17 | + AsyncPGInstrumentor, |
| 18 | +) |
15 | 19 | from opentelemetry.test.test_base import TestBase |
16 | 20 |
|
17 | 21 |
|
18 | 22 | class TestAsyncPGInstrumentation(TestBase): |
| 23 | + def tearDown(self): |
| 24 | + super().tearDown() |
| 25 | + AsyncPGInstrumentor().uninstrument() |
| 26 | + |
19 | 27 | def test_duplicated_instrumentation_can_be_uninstrumented(self): |
20 | 28 | AsyncPGInstrumentor().instrument() |
21 | 29 | AsyncPGInstrumentor().instrument() |
@@ -144,3 +152,122 @@ async def exec_mock(*args, **kwargs): |
144 | 152 |
|
145 | 153 | spans = self.memory_exporter.get_finished_spans() |
146 | 154 | self.assertEqual(len(spans), 0) |
| 155 | + |
| 156 | + def test_prepared_statement_instrumentation(self): |
| 157 | + methods = [ |
| 158 | + m for m in _PREPARED_STMT_METHODS if hasattr(PreparedStatement, m) |
| 159 | + ] |
| 160 | + |
| 161 | + for method_name in methods: |
| 162 | + with self.subTest(method=method_name, phase="before"): |
| 163 | + self.assertFalse( |
| 164 | + isinstance( |
| 165 | + getattr(PreparedStatement, method_name), |
| 166 | + BaseObjectProxy, |
| 167 | + ) |
| 168 | + ) |
| 169 | + |
| 170 | + AsyncPGInstrumentor().instrument() |
| 171 | + |
| 172 | + for method_name in methods: |
| 173 | + with self.subTest(method=method_name, phase="instrumented"): |
| 174 | + self.assertTrue( |
| 175 | + isinstance( |
| 176 | + getattr(PreparedStatement, method_name), |
| 177 | + BaseObjectProxy, |
| 178 | + ) |
| 179 | + ) |
| 180 | + |
| 181 | + AsyncPGInstrumentor().uninstrument() |
| 182 | + |
| 183 | + for method_name in methods: |
| 184 | + with self.subTest(method=method_name, phase="uninstrumented"): |
| 185 | + self.assertFalse( |
| 186 | + isinstance( |
| 187 | + getattr(PreparedStatement, method_name), |
| 188 | + BaseObjectProxy, |
| 189 | + ) |
| 190 | + ) |
| 191 | + |
| 192 | + @staticmethod |
| 193 | + def _make_prepared_stmt_conn(): |
| 194 | + async def bind_execute_mock(*args, **kwargs): |
| 195 | + return [], b"SELECT 1", True |
| 196 | + |
| 197 | + async def bind_execute_many_mock(*args, **kwargs): |
| 198 | + return None |
| 199 | + |
| 200 | + conn = mock.Mock() |
| 201 | + conn._pool_release_ctr = 0 |
| 202 | + conn.is_closed = lambda: False |
| 203 | + conn._protocol = mock.Mock() |
| 204 | + conn._protocol.bind_execute = bind_execute_mock |
| 205 | + conn._protocol.bind_execute_many = bind_execute_many_mock |
| 206 | + |
| 207 | + state = mock.Mock() |
| 208 | + state.closed = False |
| 209 | + return conn, state |
| 210 | + |
| 211 | + def test_prepared_statement_span(self): |
| 212 | + # Per-method: (query, call_args, expected_span_name) |
| 213 | + method_cases = { |
| 214 | + "fetch": ("SELECT * FROM users", (), "SELECT"), |
| 215 | + "fetchval": ("SELECT id FROM users WHERE id=$1", (1,), "SELECT"), |
| 216 | + "fetchrow": ("SELECT * FROM t WHERE v=$1", ("x",), "SELECT"), |
| 217 | + "executemany": ( |
| 218 | + "INSERT INTO t (v) VALUES ($1)", |
| 219 | + ([("a",), ("b",)],), |
| 220 | + "INSERT", |
| 221 | + ), |
| 222 | + "fetchmany": ("SELECT * FROM t", ([],), "SELECT"), |
| 223 | + } |
| 224 | + |
| 225 | + for method_name in _PREPARED_STMT_METHODS: |
| 226 | + if not hasattr(PreparedStatement, method_name): |
| 227 | + continue |
| 228 | + query, call_args, expected_name = method_cases[method_name] |
| 229 | + with self.subTest(method=method_name): |
| 230 | + self.memory_exporter.clear() |
| 231 | + conn, state = self._make_prepared_stmt_conn() |
| 232 | + apg = AsyncPGInstrumentor() |
| 233 | + apg.instrument(tracer_provider=self.tracer_provider) |
| 234 | + |
| 235 | + stmt = PreparedStatement(conn, query, state) |
| 236 | + asyncio.run(getattr(stmt, method_name)(*call_args)) |
| 237 | + |
| 238 | + spans = self.memory_exporter.get_finished_spans() |
| 239 | + self.assertEqual(len(spans), 1) |
| 240 | + self.assertEqual(spans[0].name, expected_name) |
| 241 | + self.assertTrue(spans[0].status.is_ok) |
| 242 | + self.assertEqual( |
| 243 | + spans[0].attributes.get("db.statement"), query |
| 244 | + ) |
| 245 | + self.assertEqual( |
| 246 | + spans[0].attributes.get("db.system"), "postgresql" |
| 247 | + ) |
| 248 | + |
| 249 | + apg.uninstrument() |
| 250 | + |
| 251 | + def test_prepared_statement_error_span(self): |
| 252 | + async def bind_execute_error(*args, **kwargs): |
| 253 | + raise RuntimeError("db error") |
| 254 | + |
| 255 | + conn = mock.Mock() |
| 256 | + conn._pool_release_ctr = 0 |
| 257 | + conn.is_closed = lambda: False |
| 258 | + conn._protocol = mock.Mock() |
| 259 | + conn._protocol.bind_execute = bind_execute_error |
| 260 | + |
| 261 | + state = mock.Mock() |
| 262 | + state.closed = False |
| 263 | + |
| 264 | + apg = AsyncPGInstrumentor() |
| 265 | + apg.instrument(tracer_provider=self.tracer_provider) |
| 266 | + |
| 267 | + stmt = PreparedStatement(conn, "SELECT 1", state) |
| 268 | + with self.assertRaises(RuntimeError): |
| 269 | + asyncio.run(stmt.fetch()) |
| 270 | + |
| 271 | + spans = self.memory_exporter.get_finished_spans() |
| 272 | + self.assertEqual(len(spans), 1) |
| 273 | + self.assertFalse(spans[0].status.is_ok) |
0 commit comments