Skip to content

Commit

Permalink
tests: add tests for collection name, span name
Browse files Browse the repository at this point in the history
  • Loading branch information
keithZmudzinski committed Sep 12, 2024
1 parent b54bbca commit f2971f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def instrument_connection(
enable_commenter=enable_commenter,
commenter_options=commenter_options,
)
db_integration.get_connection_attributes(connection)
db_integration.get_connection_attributes(connection=connection)
return get_traced_connection_proxy(connection, db_integration)


Expand Down Expand Up @@ -285,10 +285,10 @@ def wrapped_connection(
):
"""Add object proxy to connection object."""
connection = connect_method(*args, **kwargs)
self.get_connection_attributes(kwargs, connection)
self.get_connection_attributes(connection=connection, kwargs=kwargs)
return get_traced_connection_proxy(connection, self)

def get_connection_attributes(self, kwargs, connection):
def get_connection_attributes(self, connection, kwargs={}):
# Populate span fields using kwargs and connection
for key, value in self.connection_attributes.items():
# First set from kwargs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from opentelemetry.instrumentation import dbapi
from opentelemetry.sdk import resources
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.db_attributes import DB_COLLECTION_NAME
from opentelemetry.test.test_base import TestBase


Expand Down Expand Up @@ -49,11 +50,12 @@ def test_span_succeeded(self):
mock_connect, {}, connection_props
)
cursor = mock_connection.cursor()
cursor.execute("Test query", ("param1Value", False))
expected_query = "Test query FROM test_table"
cursor.execute(expected_query, ("param1Value", False))
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertEqual(span.name, "Test")
self.assertEqual(span.name, "Test test_table")
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)

self.assertEqual(
Expand All @@ -63,7 +65,10 @@ def test_span_succeeded(self):
span.attributes[SpanAttributes.DB_NAME], "testdatabase"
)
self.assertEqual(
span.attributes[SpanAttributes.DB_STATEMENT], "Test query"
span.attributes[SpanAttributes.DB_STATEMENT], expected_query
)
self.assertEqual(
span.attributes[DB_COLLECTION_NAME], "test_table"
)
self.assertFalse("db.statement.parameters" in span.attributes)
self.assertEqual(span.attributes[SpanAttributes.DB_USER], "testuser")
Expand Down Expand Up @@ -91,14 +96,16 @@ def test_span_name(self):
cursor.execute("/* leading comment */ query")
cursor.execute("/* leading comment */ query /* trailing comment */")
cursor.execute("query /* trailing comment */")
cursor.execute("SELECT * FROM test_table")
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 6)
self.assertEqual(len(spans_list), 7)
self.assertEqual(spans_list[0].name, "Test")
self.assertEqual(spans_list[1].name, "multi")
self.assertEqual(spans_list[2].name, "tab")
self.assertEqual(spans_list[3].name, "query")
self.assertEqual(spans_list[4].name, "query")
self.assertEqual(spans_list[5].name, "query")
self.assertEqual(spans_list[6].name, "SELECT test_table")

def test_span_succeeded_with_capture_of_statement_parameters(self):
connection_props = {
Expand Down

0 comments on commit f2971f4

Please sign in to comment.