Skip to content

Commit ad8948a

Browse files
Add test
1 parent 1d2e7c9 commit ad8948a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15+
import re
1516

1617
import pytest
1718
from sqlalchemy import create_engine
1819

1920
from opentelemetry import context
2021
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
22+
from opentelemetry.semconv.trace import SpanAttributes
2123
from opentelemetry.test.test_base import TestBase
2224

2325

@@ -56,6 +58,38 @@ def test_sqlcommenter_enabled(self):
5658
r"SELECT 1 /\*db_driver='(.*)',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
5759
)
5860

61+
def test_sqlcommenter_enabled_matches_db_statement_attribute(self):
62+
engine = create_engine("sqlite:///:memory:")
63+
SQLAlchemyInstrumentor().instrument(
64+
engine=engine,
65+
tracer_provider=self.tracer_provider,
66+
enable_commenter=True,
67+
commenter_options={"db_framework": False},
68+
)
69+
cnx = engine.connect()
70+
cnx.execute("SELECT 1;").fetchall()
71+
query_log = self.caplog.records[-2].getMessage()
72+
self.assertRegex(
73+
query_log,
74+
r"SELECT 1 /\*db_driver='(.*)',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
75+
)
76+
spans = self.memory_exporter.get_finished_spans()
77+
self.assertEqual(len(spans), 2)
78+
# first span is connection to db
79+
self.assertEqual(spans[0].name, "connect")
80+
# second span is query itself
81+
query_span = spans[1]
82+
self.assertRegex(
83+
query_span.attributes[SpanAttributes.DB_STATEMENT],
84+
r"SELECT 1 /\*db_driver='(.*)',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
85+
)
86+
cnx_span_id = re.search(r"[a-zA-Z0-9_]{16}", query_log).group()
87+
db_statement_span_id = re.search(
88+
r"[a-zA-Z0-9_]{16}",
89+
query_span.attributes[SpanAttributes.DB_STATEMENT],
90+
).group()
91+
self.assertEqual(cnx_span_id, db_statement_span_id)
92+
5993
def test_sqlcommenter_enabled_otel_values_false(self):
6094
engine = create_engine("sqlite:///:memory:")
6195
SQLAlchemyInstrumentor().instrument(

0 commit comments

Comments
 (0)