Skip to content

Commit 1c820ea

Browse files
SQLAlchemy instrumentor populates span after sqlcomment creation, not before (#2937)
1 parent 53b8714 commit 1c820ea

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
([#2976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2976))
1818
- Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap`
1919
([#2996](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2996))
20+
- `opentelemetry-instrumentation-sqlalchemy` Add sqlcomment to `db.statement` attribute
21+
([#2937](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2937))
22+
- `opentelemetry-instrumentation-dbapi` Add sqlcomment to `db.statement` attribute
23+
([#2935](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2935))
2024

2125
### Fixed
2226

@@ -44,8 +48,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4448
([#2635](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2635))
4549
- `opentelemetry-instrumentation` Add support for string based dotted module paths in unwrap
4650
([#2919](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2919))
47-
- `opentelemetry-instrumentation-dbapi` Add sqlcomment to `db.statement` attribute
48-
([#2935](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2935))
4951

5052
### Fixed
5153

instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -219,28 +219,31 @@ def _before_cur_exec(
219219
)
220220
with trace.use_span(span, end_on_exit=False):
221221
if span.is_recording():
222+
if self.enable_commenter:
223+
commenter_data = {
224+
"db_driver": conn.engine.driver,
225+
# Driver/framework centric information.
226+
"db_framework": f"sqlalchemy:{sqlalchemy.__version__}",
227+
}
228+
229+
if self.commenter_options.get(
230+
"opentelemetry_values", True
231+
):
232+
commenter_data.update(**_get_opentelemetry_values())
233+
234+
# Filter down to just the requested attributes.
235+
commenter_data = {
236+
k: v
237+
for k, v in commenter_data.items()
238+
if self.commenter_options.get(k, True)
239+
}
240+
241+
statement = _add_sql_comment(statement, **commenter_data)
242+
222243
span.set_attribute(SpanAttributes.DB_STATEMENT, statement)
223244
span.set_attribute(SpanAttributes.DB_SYSTEM, self.vendor)
224245
for key, value in attrs.items():
225246
span.set_attribute(key, value)
226-
if self.enable_commenter:
227-
commenter_data = {
228-
"db_driver": conn.engine.driver,
229-
# Driver/framework centric information.
230-
"db_framework": f"sqlalchemy:{sqlalchemy.__version__}",
231-
}
232-
233-
if self.commenter_options.get("opentelemetry_values", True):
234-
commenter_data.update(**_get_opentelemetry_values())
235-
236-
# Filter down to just the requested attributes.
237-
commenter_data = {
238-
k: v
239-
for k, v in commenter_data.items()
240-
if self.commenter_options.get(k, True)
241-
}
242-
243-
statement = _add_sql_comment(statement, **commenter_data)
244247

245248
context._otel_span = span
246249

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

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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 (
@@ -21,6 +22,7 @@
2122

2223
from opentelemetry import context
2324
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
25+
from opentelemetry.semconv.trace import SpanAttributes
2426
from opentelemetry.test.test_base import TestBase
2527

2628

@@ -59,6 +61,38 @@ def test_sqlcommenter_enabled(self):
5961
r"SELECT 1 /\*db_driver='(.*)',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
6062
)
6163

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

0 commit comments

Comments
 (0)