fix(db_statement_summary): bound INSERT summary length like the SELECT path#2084
fix(db_statement_summary): bound INSERT summary length like the SELECT path#2084chuenchen309 wants to merge 3 commits into
Conversation
…T path summarize_query is meant to keep span messages within MAX_QUERY_MESSAGE_LENGTH (80). The SELECT branch enforces this with a final truncate(summary, MAX_QUERY_MESSAGE_LENGTH), but the INSERT branch returned its f-string directly. Since the table name is not truncated on its own, a long (e.g. schema-qualified) table produced summaries well over the limit (112 chars in the added test), unlike SELECT and the fallback path. Wrap the INSERT result in the same final truncate so it stays bounded. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🔗 Linked repositories identifiedCodeRabbit considers these linked repositories for cross-repo context during reviews:
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughINSERT query summaries now apply the shared 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@logfire/_internal/db_statement_summary.py`:
- Around line 82-85: The INSERT summary truncation in truncate must reserve one
character for the ellipsis so the final result never exceeds
MAX_QUERY_MESSAGE_LENGTH. Update logfire/_internal/db_statement_summary.py lines
82-85 accordingly, then revise tests/test_db_statement_summary.py lines 158-168
to assert len(summary) <= MAX_QUERY_MESSAGE_LENGTH and update the expected
literal for the corrected output.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 8232872b-3d75-4136-88b1-5479f41f1aee
📒 Files selected for processing (2)
logfire/_internal/db_statement_summary.pytests/test_db_statement_summary.py
🔗 Linked repositories identified
CodeRabbit considers these linked repositories for cross-repo context during reviews:
pydantic/logfire(manual)pydantic/pydantic-ai(manual)pydantic/platform(auto-detected)pydantic/pydantic(auto-detected)
| return truncate( | ||
| f'INSERT INTO {table} {truncate(columns, 25)} VALUES {truncate(values, 25)}', | ||
| MAX_QUERY_MESSAGE_LENGTH, | ||
| ) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
The INSERT length-bound contract still has an off-by-one gap.
truncate(..., 80) currently produces 81 characters when truncating, while the regression test does not assert the configured bound directly.
logfire/_internal/db_statement_summary.py#L82-L85: fixtruncateto reserve one character for…, ensuring the final result is no longer than 80.tests/test_db_statement_summary.py#L158-L168: assertlen(summary) <= MAX_QUERY_MESSAGE_LENGTHand update the expected literal after correcting the helper.
🧰 Tools
🪛 Ruff (0.15.21)
[error] 83-83: Possible SQL injection vector through string-based query construction
(S608)
📍 Affects 2 files
logfire/_internal/db_statement_summary.py#L82-L85(this comment)tests/test_db_statement_summary.py#L158-L168
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@logfire/_internal/db_statement_summary.py` around lines 82 - 85, The INSERT
summary truncation in truncate must reserve one character for the ellipsis so
the final result never exceeds MAX_QUERY_MESSAGE_LENGTH. Update
logfire/_internal/db_statement_summary.py lines 82-85 accordingly, then revise
tests/test_db_statement_summary.py lines 158-168 to assert len(summary) <=
MAX_QUERY_MESSAGE_LENGTH and update the expected literal for the corrected
output.
There was a problem hiding this comment.
Added an explicit len(result) <= MAX_QUERY_MESSAGE_LENGTH + 1 assertion to the test to make the bound self-documenting.
On the + 1: the 81-vs-80 is a pre-existing property of truncate() itself — it returns 2 * (length // 2) + 1 characters for an even length, so truncate(s, 80) yields up to 81. That behavior is shared by the SELECT path, which already ends in truncate(summary, MAX_QUERY_MESSAGE_LENGTH). The goal of this PR is only to make the INSERT branch honor the same bound the SELECT branch already does; changing truncate() to reserve a slot for the ellipsis would alter the output of every existing SELECT summary and break the current exact-match tests (test_long_select, test_one_join_long, etc.), so it is out of scope here. Happy to tighten truncate() in a separate PR if that is wanted.
(The Ruff S608 "SQL injection" hint is a false positive — this f-string builds a truncated display summary for a span message, it is never executed as SQL.)
There was a problem hiding this comment.
CodeRabbit chat interactions are restricted to organization members for this repository. Ask an organization member to interact with CodeRabbit, or set chat.allow_non_org_members: true in your configuration.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Good catch that Making the INSERT result strictly |
…itly Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Putting a number on the "out of scope" claim I made above, since it was an assertion and this is checkable: Applying the suggested That's the reason this PR leaves Happy to send that as its own PR if the team wants the bound to be exact — it'd need the expected literals in those 9 tests updated too. |
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
summarize_querykeeps DB-statement span messages withinMAX_QUERY_MESSAGE_LENGTH(80). The SELECT path enforces this by ending intruncate(summary, MAX_QUERY_MESSAGE_LENGTH), and the fallback path is bounded too — but theINSERTbranch returned its f-string directly:columns/valuesare truncated, buttableis not, so a long (e.g. schema-qualified) table name pushes the summary past the limit.Reproduction
The equivalent
SELECTwith the same long table stays bounded (~74 chars).Fix
Wrap the INSERT result in the same final
truncate(..., MAX_QUERY_MESSAGE_LENGTH)that the SELECT path already applies, so every branch stays bounded.Tests
Added
test_insert_long_table; the existingtest_insert(short table) is unchanged. Fulltests/test_db_statement_summary.pypasses (24), andruff format/check+pyrightare clean on the touched files.