Skip to content

Commit 7bcc404

Browse files
More
1 parent 888059f commit 7bcc404

File tree

1 file changed

+16
-21
lines changed
  • instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy

1 file changed

+16
-21
lines changed

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ def _get_commenter_data(self, conn) -> dict:
249249
}
250250
return commenter_data
251251

252+
def _set_db_client_span_attributes(self, span, statement, attrs) -> None:
253+
"""Uses statement and attrs to set attributes of provided Otel span"""
254+
span.set_attribute(SpanAttributes.DB_STATEMENT, statement)
255+
span.set_attribute(SpanAttributes.DB_SYSTEM, self.vendor)
256+
for key, value in attrs.items():
257+
span.set_attribute(key, value)
258+
252259
def _before_cur_exec(
253260
self, conn, cursor, statement, params, context, _executemany
254261
):
@@ -267,39 +274,27 @@ def _before_cur_exec(
267274
commenter_data = self._get_commenter_data(conn)
268275

269276
if self.enable_attribute_commenter:
270-
# sqlcomment in executed query and span attribute
277+
# sqlcomment is added to executed query and db.statement span attribute
271278
statement = _add_sql_comment(
272279
statement, **commenter_data
273280
)
274-
span.set_attribute(
275-
SpanAttributes.DB_STATEMENT, statement
276-
)
277-
span.set_attribute(
278-
SpanAttributes.DB_SYSTEM, self.vendor
281+
self._set_db_client_span_attributes(
282+
span, statement, attrs
279283
)
280-
for key, value in attrs.items():
281-
span.set_attribute(key, value)
282284

283285
else:
284-
# sqlcomment in executed query only
285-
span.set_attribute(
286-
SpanAttributes.DB_STATEMENT, statement
287-
)
288-
span.set_attribute(
289-
SpanAttributes.DB_SYSTEM, self.vendor
286+
# sqlcomment is only added to executed query
287+
# so db.statement is set before add_sql_comment
288+
self._set_db_client_span_attributes(
289+
span, statement, attrs
290290
)
291-
for key, value in attrs.items():
292-
span.set_attribute(key, value)
293291
statement = _add_sql_comment(
294292
statement, **commenter_data
295293
)
296294

297295
else:
298-
# no sqlcomment
299-
span.set_attribute(SpanAttributes.DB_STATEMENT, statement)
300-
span.set_attribute(SpanAttributes.DB_SYSTEM, self.vendor)
301-
for key, value in attrs.items():
302-
span.set_attribute(key, value)
296+
# no sqlcomment anywhere
297+
self._set_db_client_span_attributes(span, statement, attrs)
303298

304299
context._otel_span = span
305300

0 commit comments

Comments
 (0)