From da16d8184dbbd63ff9f1cd63e84b266031eee8d4 Mon Sep 17 00:00:00 2001 From: RiyaChaturvedi37 Date: Sun, 24 May 2026 20:41:10 +0530 Subject: [PATCH 1/2] feat(sqlcommenter): add comment_position option to control SQL comment placement --- .changelog/4490.added | 1 + .../instrumentation/sqlcommenter_utils.py | 20 ++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 .changelog/4490.added diff --git a/.changelog/4490.added b/.changelog/4490.added new file mode 100644 index 0000000000..a8e8e9b59a --- /dev/null +++ b/.changelog/4490.added @@ -0,0 +1 @@ +`opentelemetry-instrumentation`: Add `comment_position` option to `_add_sql_comment()` to support prepending sqlcommenter comment to the beginning of the query \ No newline at end of file diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py index e97fdf55ec..99c40fa541 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py @@ -5,17 +5,26 @@ from opentelemetry.instrumentation.utils import _url_quote -def _add_sql_comment(sql, **meta) -> str: +def _add_sql_comment(sql, comment_position="end", **meta) -> str: """ - Appends comments to the sql statement and returns it + Adds comments to the sql statement and returns it. + By default, the comment is appended to the end of the query. + Set comment_position="start" to prepend the comment instead. """ meta.update(**_add_framework_tags()) comment = _generate_sql_comment(**meta) + if not comment: + return sql sql = sql.rstrip() if sql.endswith(";"): - sql = sql[:-1] + comment + ";" + sql = sql[:-1] + end = ";" else: - sql = sql + comment + end = "" + if comment_position == "start": + sql = comment + " " + sql + end + else: + sql = sql + comment + end return sql @@ -25,10 +34,8 @@ def _generate_sql_comment(**meta) -> str: **meta kwargs. """ key_value_delimiter = "," - if not meta: # No entries added. return "" - # Sort the keywords to ensure that caching works and that testing is # deterministic. It eases visual inspection as well. return ( @@ -46,7 +53,6 @@ def _add_framework_tags() -> dict: """ Returns orm related tags if any set by the context """ - sqlcommenter_framework_values = ( context.get_value("SQLCOMMENTER_ORM_TAGS_AND_VALUES") if context.get_value("SQLCOMMENTER_ORM_TAGS_AND_VALUES") From 455589c34e5b45fe998e6d97ea4f6ed2728d5ff6 Mon Sep 17 00:00:00 2001 From: RiyaChaturvedi37 Date: Tue, 26 May 2026 12:17:19 +0530 Subject: [PATCH 2/2] fix: update changelog fragment to use new PR number 4628 --- .changelog/{4490.added => 4628.added} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changelog/{4490.added => 4628.added} (100%) diff --git a/.changelog/4490.added b/.changelog/4628.added similarity index 100% rename from .changelog/4490.added rename to .changelog/4628.added