Skip to content

Commit e3ddea9

Browse files
_add_sql_comment only if span.is_recording
1 parent c5a7534 commit e3ddea9

File tree

1 file changed

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

1 file changed

+21
-18
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,28 @@ def _before_cur_exec(
218218
kind=trace.SpanKind.CLIENT,
219219
)
220220
with trace.use_span(span, end_on_exit=False):
221-
if self.enable_commenter:
222-
commenter_data = {
223-
"db_driver": conn.engine.driver,
224-
# Driver/framework centric information.
225-
"db_framework": f"sqlalchemy:{sqlalchemy.__version__}",
226-
}
227-
228-
if self.commenter_options.get("opentelemetry_values", True):
229-
commenter_data.update(**_get_opentelemetry_values())
230-
231-
# Filter down to just the requested attributes.
232-
commenter_data = {
233-
k: v
234-
for k, v in commenter_data.items()
235-
if self.commenter_options.get(k, True)
236-
}
237-
238-
statement = _add_sql_comment(statement, **commenter_data)
239221
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+
240243
span.set_attribute(SpanAttributes.DB_STATEMENT, statement)
241244
span.set_attribute(SpanAttributes.DB_SYSTEM, self.vendor)
242245
for key, value in attrs.items():

0 commit comments

Comments
 (0)