Skip to content

Commit

Permalink
Rearrange sqlcomment and span attr setup
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi committed Dec 19, 2024
1 parent 85c39a0 commit 888059f
Showing 1 changed file with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ def _operation_name(self, db_name, statement):
return self.vendor
return " ".join(parts)

def _get_commenter_data(self, conn) -> dict:
"""Calculate sqlcomment contents from conn and configured options"""
commenter_data = {
"db_driver": conn.engine.driver,
# Driver/framework centric information.
"db_framework": f"sqlalchemy:{sqlalchemy.__version__}",
}

if self.commenter_options.get("opentelemetry_values", True):
commenter_data.update(**_get_opentelemetry_values())

# Filter down to just the requested attributes.
commenter_data = {
k: v
for k, v in commenter_data.items()
if self.commenter_options.get(k, True)
}
return commenter_data

def _before_cur_exec(
self, conn, cursor, statement, params, context, _executemany
):
Expand All @@ -245,39 +264,42 @@ def _before_cur_exec(
with trace.use_span(span, end_on_exit=False):
if span.is_recording():
if self.enable_commenter:
commenter_data = {
"db_driver": conn.engine.driver,
# Driver/framework centric information.
"db_framework": f"sqlalchemy:{sqlalchemy.__version__}",
}

if self.commenter_options.get(
"opentelemetry_values", True
):
commenter_data.update(**_get_opentelemetry_values())

# Filter down to just the requested attributes.
commenter_data = {
k: v
for k, v in commenter_data.items()
if self.commenter_options.get(k, True)
}
commenter_data = self._get_commenter_data(conn)

if self.enable_attribute_commenter:
# sqlcomment in executed query and span attribute
statement = _add_sql_comment(
statement, **commenter_data
)
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)

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)
else:
# sqlcomment in executed query only
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)
statement = _add_sql_comment(
statement, **commenter_data
)

if (
self.enable_commenter
and not self.enable_attribute_commenter
):
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)

context._otel_span = span

Expand Down

0 comments on commit 888059f

Please sign in to comment.