Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/4628.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`opentelemetry-instrumentation`: Add `comment_position` option to `_add_sql_comment()` to support prepending sqlcommenter comment to the beginning of the query

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pardon my delayed review @RiyaChaturvedi37 and thank you for resuming this!

I had this comment on the other PR: Instead of a string, what if we had it as a case-insensitive boolean string check for false/true and the config was called comment_prepend=false? Since I don't think we'll support a "middle".

"""
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


Expand All @@ -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 (
Expand All @@ -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")
Expand Down
Loading