|
8 | 8 | from ..helpers import testenv |
9 | 9 | from instana.singletons import tracer |
10 | 10 | from sqlalchemy.orm import sessionmaker |
| 11 | +from sqlalchemy.exc import OperationalError |
11 | 12 | from sqlalchemy.ext.declarative import declarative_base |
12 | 13 | from sqlalchemy import Column, Integer, String, create_engine |
13 | 14 |
|
@@ -181,3 +182,29 @@ def test_error_logging(self): |
181 | 182 | self.assertIsNotNone(sql_span.stack) |
182 | 183 | self.assertTrue(type(sql_span.stack) is list) |
183 | 184 | self.assertGreater(len(sql_span.stack), 0) |
| 185 | + |
| 186 | + def test_error_before_tracing(self): |
| 187 | + """Test the scenario, in which instana is loaded, |
| 188 | + but connection fails before tracing begins. |
| 189 | + This is typical in test container scenario, |
| 190 | + where it is "normal" to just start hammering a database container |
| 191 | + which is still starting and not ready to handle requests yet. |
| 192 | + In this scenario it is important that we get |
| 193 | + an sqlalachemy exception, and not something else |
| 194 | + like an AttributeError. Because testcontainer has a logic |
| 195 | + to retry in case of certain sqlalchemy exceptions but it |
| 196 | + can't handle an AttributeError.""" |
| 197 | + # https://github.com/instana/python-sensor/issues/362 |
| 198 | + |
| 199 | + self.assertIsNone(tracer.active_span) |
| 200 | + |
| 201 | + invalid_connection_url = 'postgresql://user1:pwd1@localhost:9999/mydb1' |
| 202 | + with self.assertRaisesRegex( |
| 203 | + OperationalError, |
| 204 | + r'\(psycopg2.OperationalError\) connection .* failed.*' |
| 205 | + ) as context_manager: |
| 206 | + engine = create_engine(invalid_connection_url) |
| 207 | + version, = engine.execute("select version()").fetchone() |
| 208 | + |
| 209 | + the_exception = context_manager.exception |
| 210 | + self.assertFalse(the_exception.connection_invalidated) |
0 commit comments