From 7bcc404068fdc2489c1e55acfa9a97e33b0a149e Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Wed, 18 Dec 2024 16:48:41 -0800 Subject: [PATCH] More --- .../instrumentation/sqlalchemy/engine.py | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py index e2ea00daa2..a3312bd77c 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py @@ -249,6 +249,13 @@ def _get_commenter_data(self, conn) -> dict: } return commenter_data + def _set_db_client_span_attributes(self, span, statement, attrs) -> None: + """Uses statement and attrs to set attributes of provided Otel span""" + span.set_attribute(SpanAttributes.DB_STATEMENT, statement) + span.set_attribute(SpanAttributes.DB_SYSTEM, self.vendor) + for key, value in attrs.items(): + span.set_attribute(key, value) + def _before_cur_exec( self, conn, cursor, statement, params, context, _executemany ): @@ -267,39 +274,27 @@ def _before_cur_exec( commenter_data = self._get_commenter_data(conn) if self.enable_attribute_commenter: - # sqlcomment in executed query and span attribute + # sqlcomment is added to executed query and db.statement span attribute statement = _add_sql_comment( statement, **commenter_data ) - span.set_attribute( - SpanAttributes.DB_STATEMENT, statement - ) - span.set_attribute( - SpanAttributes.DB_SYSTEM, self.vendor + self._set_db_client_span_attributes( + span, statement, attrs ) - for key, value in attrs.items(): - span.set_attribute(key, value) else: - # sqlcomment in executed query only - span.set_attribute( - SpanAttributes.DB_STATEMENT, statement - ) - span.set_attribute( - SpanAttributes.DB_SYSTEM, self.vendor + # sqlcomment is only added to executed query + # so db.statement is set before add_sql_comment + self._set_db_client_span_attributes( + span, statement, attrs ) - for key, value in attrs.items(): - span.set_attribute(key, value) statement = _add_sql_comment( statement, **commenter_data ) else: - # no sqlcomment - span.set_attribute(SpanAttributes.DB_STATEMENT, statement) - span.set_attribute(SpanAttributes.DB_SYSTEM, self.vendor) - for key, value in attrs.items(): - span.set_attribute(key, value) + # no sqlcomment anywhere + self._set_db_client_span_attributes(span, statement, attrs) context._otel_span = span