Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
942d191
fix(sqlcommenter): prepend comment to beginning of query
RiyaChaturvedi37 Apr 26, 2026
f80b3e3
fix(sqlcommenter): update tests for prepended comment position
RiyaChaturvedi37 Apr 26, 2026
24fe73c
fix: update sqlcommenter tests for prepended comment
RiyaChaturvedi37 Apr 26, 2026
bce87f7
fix: normalize line endings to LF
RiyaChaturvedi37 Apr 27, 2026
4b2621e
fix(sqlcommenter): add comment_position option, default to end of query
RiyaChaturvedi37 May 1, 2026
1b2c466
Merge branch 'main' into fix/sqlcommenter-prepend-comment
RiyaChaturvedi37 May 1, 2026
bb88698
fix: normalize line endings and add final newline
RiyaChaturvedi37 May 1, 2026
dce66bc
fix(sqlcommenter): update tests for default end position
RiyaChaturvedi37 May 1, 2026
443cd77
fix(sqlcommenter): fix semicolon handling in tests
RiyaChaturvedi37 May 1, 2026
f450d13
fix(sqlcommenter): fix expected value in without-semicolon test
RiyaChaturvedi37 May 1, 2026
b1b30d7
Merge branch 'main' into fix/sqlcommenter-prepend-comment
xrmx May 5, 2026
932e0e2
Merge branch 'main' into fix/sqlcommenter-prepend-comment
tammy-baylis-swi May 5, 2026
087f077
Merge branch 'main' into fix/sqlcommenter-prepend-comment
RiyaChaturvedi37 May 8, 2026
234e6c4
fix(sqlcommenter): move changelog entry to root CHANGELOG.md with PR …
RiyaChaturvedi37 May 9, 2026
3720924
Merge branch 'main' into fix/sqlcommenter-prepend-comment
RiyaChaturvedi37 May 9, 2026
98c69b6
fix: use towncrier changelog fragment and simplify sqlcommenter_utils…
RiyaChaturvedi37 May 19, 2026
6e4b971
Merge branch 'main' into fix/sqlcommenter-prepend-comment
RiyaChaturvedi37 May 19, 2026
133529c
Merge branch 'main' into fix/sqlcommenter-prepend-comment
RiyaChaturvedi37 May 19, 2026
806279a
fix: restore CHANGELOG.md to upstream towncrier-managed version
RiyaChaturvedi37 May 20, 2026
c98c5fb
Merge branch 'main' into fix/sqlcommenter-prepend-comment
emdneto May 22, 2026
d016a11
Update CHANGELOG.md
emdneto May 22, 2026
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `sqlcommenter`: prepend SQL comment to beginning of query instead of appending to end
([#3583](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3583))
- Bump `pylint` to `4.0.5`
([#4244](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4244))
- `opentelemetry-instrumentation-sqlite3`: Add uninstrument, error status, suppress, and no-op tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from opentelemetry import context
from opentelemetry.instrumentation.utils import _url_quote


def _add_sql_comment(sql, **meta) -> str:
"""
Appends comments to the sql statement and returns it
"""
meta.update(**_add_framework_tags())
comment = _generate_sql_comment(**meta)
sql = sql.rstrip()
if sql.endswith(";"):
sql = sql[:-1] + comment + ";"
else:
sql = sql + comment
return sql


def _generate_sql_comment(**meta) -> str:
"""
Return a SQL comment with comma delimited key=value pairs created from
**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 (
" /*"
+ key_value_delimiter.join(
f"{_url_quote(key)}={_url_quote(value)!r}"
for key, value in sorted(meta.items())
if value is not None
)
+ "*/"
)


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")
else {}
)
return sqlcommenter_framework_values
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from opentelemetry import context
from opentelemetry.instrumentation.utils import _url_quote


def _add_sql_comment(sql, **meta) -> str:
"""
Prepends comments to the sql statement and returns it
"""
meta.update(**_add_framework_tags())
comment = _generate_sql_comment(**meta)
if not comment:
return sql
sql = sql.strip()
if sql.endswith(";"):
sql = comment + " " + sql[:-1] + ";"
else:
sql = comment + " " + sql
return sql


def _generate_sql_comment(**meta) -> str:
"""
Return a SQL comment with comma delimited key=value pairs created from
**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 (
" /*"
+ key_value_delimiter.join(
f"{_url_quote(key)}={_url_quote(value)!r}"
for key, value in sorted(meta.items())
if value is not None
)
+ "*/"
)


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")
else {}
)
return sqlcommenter_framework_values
4 changes: 2 additions & 2 deletions opentelemetry-instrumentation/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_add_sql_comments_with_semicolon(self):

self.assertEqual(
commented_sql_without_semicolon,
"Select 1 /*comment%%202='value%%203',comment_1='value%%201'*/;",
" /*comment%%202='value%%203',comment_1='value%%201'*/ Select 1",
)

def test_add_sql_comments_without_semicolon(self):
Expand All @@ -203,7 +203,7 @@ def test_add_sql_comments_without_semicolon(self):

self.assertEqual(
commented_sql_without_semicolon,
"Select 1 /*comment%%202='value%%203',comment_1='value%%201'*/",
" /*comment%%202='value%%203',comment_1='value%%201'*/ Select 1",
)

def test_add_sql_comments_without_comments(self):
Expand Down
Loading