Skip to content

Commit a445ef9

Browse files
authored
fix(sqlalchemy): Stand down before tracing has started (#362) (#365)
1 parent 73ba946 commit a445ef9

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

instana/instrumentation/sqlalchemy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def _set_error_tags(context, exception_string, scope_string):
7373
@event.listens_for(Engine, error_event, named=True)
7474
def receive_handle_db_error(**kw):
7575

76+
if get_active_tracer() is None:
77+
return
78+
7679
# support older db error event
7780
if error_event == "dbapi_error":
7881
context = kw.get('context')

tests/clients/test_sqlalchemy.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ..helpers import testenv
99
from instana.singletons import tracer
1010
from sqlalchemy.orm import sessionmaker
11+
from sqlalchemy.exc import OperationalError
1112
from sqlalchemy.ext.declarative import declarative_base
1213
from sqlalchemy import Column, Integer, String, create_engine
1314

@@ -181,3 +182,29 @@ def test_error_logging(self):
181182
self.assertIsNotNone(sql_span.stack)
182183
self.assertTrue(type(sql_span.stack) is list)
183184
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)

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
collect_ignore_glob.append("*test_grpc*")
2727
collect_ignore_glob.append("*test_boto3*")
2828
collect_ignore_glob.append("*test_stan_recorder*")
29+
collect_ignore_glob.append("*test_sqlalchemy*")
2930

3031
if "ASYNQP_TEST" not in os.environ:
3132
# if LooseVersion(sys.version) < LooseVersion('3.5.3') or LooseVersion(sys.version) >= LooseVersion('3.8.0'):

0 commit comments

Comments
 (0)