Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dbname as collecting parameter for PEP0249 #461

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions instana/instrumentation/pep0249.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ def _collect_kvs(self, span, sql):
try:
span.set_tag(ext.SPAN_KIND, 'exit')

if 'db' in self._connect_params[1]:
span.set_tag(ext.DATABASE_INSTANCE, self._connect_params[1]['db'])
elif 'database' in self._connect_params[1]:
span.set_tag(ext.DATABASE_INSTANCE, self._connect_params[1]['database'])
db_parameter_name = next((p for p in ('db', 'database', 'dbname') if p in self._connect_params[1]), None)
if db_parameter_name:
span.set_tag(ext.DATABASE_INSTANCE, self._connect_params[1][db_parameter_name])

span.set_tag(ext.DATABASE_STATEMENT, sql_sanitizer(sql))
span.set_tag(ext.DATABASE_USER, self._connect_params[1]['user'])
Expand Down
38 changes: 35 additions & 3 deletions tests/clients/test_psycopg2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@

class TestPsycoPG2(unittest.TestCase):
def setUp(self):
self.db = psycopg2.connect(host=testenv['postgresql_host'], port=testenv['postgresql_port'],
user=testenv['postgresql_user'], password=testenv['postgresql_pw'],
database=testenv['postgresql_db'])
deprecated_param_name = self.shortDescription() == 'test_deprecated_parameter_database'
kwargs = {
'host': testenv['postgresql_host'],
'port': testenv['postgresql_port'],
'user': testenv['postgresql_user'],
'password': testenv['postgresql_pw'],
'dbname' if not deprecated_param_name else 'database': testenv['postgresql_db'],
}
self.db = psycopg2.connect(**kwargs)

database_setup_query = """
DROP TABLE IF EXISTS users;
Expand Down Expand Up @@ -330,3 +336,29 @@ def test_cursor_ctx_mgr(self):
self.assertEqual(db_span.data["pg"]["stmt"], "SELECT * from users")
self.assertEqual(db_span.data["pg"]["host"], testenv["postgresql_host"])
self.assertEqual(db_span.data["pg"]["port"], testenv["postgresql_port"])

def test_deprecated_parameter_database(self):
"""test_deprecated_parameter_database"""

with tracer.start_active_span('test'):
self.cursor.execute("""SELECT * from users""")
affected_rows = self.cursor.rowcount
result = self.cursor.fetchone()
self.db.commit()

self.assertEqual(1, affected_rows)
self.assertEqual(6, len(result))

spans = self.recorder.queued_spans()
self.assertEqual(2, len(spans))

db_span, test_span = spans

self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertIsNone(db_span.ec)

self.assertEqual(db_span.n, "postgres")
self.assertEqual(db_span.data["pg"]["db"], testenv['postgresql_db'])
37 changes: 34 additions & 3 deletions tests/clients/test_pymysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@

class TestPyMySQL(unittest.TestCase):
def setUp(self):
self.db = pymysql.connect(host=testenv['mysql_host'], port=testenv['mysql_port'],
user=testenv['mysql_user'], passwd=testenv['mysql_pw'],
db=testenv['mysql_db'])
deprecated_param_name = self.shortDescription() == 'test_deprecated_parameter_db'
kwargs = {
'host': testenv['mysql_host'],
'port': testenv['mysql_port'],
'user': testenv['mysql_user'],
'passwd': testenv['mysql_pw'],
'database' if not deprecated_param_name else 'db': testenv['mysql_db'],
}
self.db = pymysql.connect(**kwargs)

database_setup_query = """
DROP TABLE IF EXISTS users; |
CREATE TABLE users(
Expand Down Expand Up @@ -286,3 +293,27 @@ def test_cursor_ctx_mgr(self):
self.assertEqual(db_span.data["mysql"]["stmt"], "SELECT * from users")
self.assertEqual(db_span.data["mysql"]["host"], testenv["mysql_host"])
self.assertEqual(db_span.data["mysql"]["port"], testenv["mysql_port"])

def test_deprecated_parameter_db(self):
"""test_deprecated_parameter_db"""

with tracer.start_active_span('test'):
affected_rows = self.cursor.execute("""SELECT * from users""")
result = self.cursor.fetchone()

self.assertEqual(1, affected_rows)
self.assertEqual(3, len(result))

spans = self.recorder.queued_spans()
self.assertEqual(2, len(spans))

db_span, test_span = spans

self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertIsNone(db_span.ec)

self.assertEqual(db_span.n, "mysql")
self.assertEqual(db_span.data["mysql"]["db"], testenv['mysql_db'])