test(aiomysql): Add span streaming variants to aiomysql tests#6601
Conversation
Parametrize existing aiomysql tests with span_streaming to cover both static and stream trace lifecycle modes. Fixes PY-2523 Fixes #6539
| @contextmanager | ||
| def fake_record_sql_queries(*args, **kwargs): | ||
| def fake_record_sql_queries_streaming(*args, **kwargs): | ||
| with record_sql_queries(*args, **kwargs) as span: | ||
| pass | ||
| span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0) | ||
| span.timestamp = datetime.datetime(2024, 1, 1, microsecond=100001) | ||
| yield span | ||
|
|
||
| async with conn.cursor() as cur: | ||
| with mock.patch( | ||
| "sentry_sdk.integrations.aiomysql.record_sql_queries", | ||
| fake_record_sql_queries, | ||
| ): | ||
| await cur.execute( | ||
| "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')", | ||
| span._start_timestamp = datetime.datetime( | ||
| 2024, 1, 1, microsecond=0, tzinfo=datetime.timezone.utc | ||
| ) | ||
| yield span |
There was a problem hiding this comment.
Bug: The mock in test_query_source_if_duration_over_threshold for streaming spans fails to simulate a long duration, preventing correct validation of the slow query source annotation feature.
Severity: LOW
Suggested Fix
Update the mock in the streaming test case to correctly simulate a slow query. After yield span, set span._end_timestamp to a value that ensures the duration span._end_timestamp - span._start_timestamp is greater than the 100ms threshold, similar to how the non-streaming version of the test is implemented. This will ensure the test accurately validates the intended behavior.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: tests/integrations/aiomysql/test_aiomysql.py#L800-L806
Potential issue: In the streaming version of the test
`test_query_source_if_duration_over_threshold`, the mock setup for the span only sets
`_start_timestamp`. When the span's context manager exits, `_end_timestamp` is
calculated based on the elapsed monotonic time, which is near-zero within the test's
execution. This results in a span duration that is too short to pass the `slow_query`
check in the `add_query_source` function. Consequently, the test fails to properly
simulate a slow query, meaning it cannot validate that source code information is
correctly added for slow streaming database queries as intended.
Did we get this right? 👍 / 👎 to inform future reviews.
Codecov Results 📊✅ 90810 passed | ⏭️ 6129 skipped | Total: 96939 | Pass Rate: 93.68% | Execution Time: 320m 11s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 100.00%. Project has 2411 uncovered lines. Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 89.60% 89.84% +0.24%
==========================================
Files 192 192 —
Lines 23732 23732 —
Branches 8194 8194 —
==========================================
+ Hits 21265 21321 +56
- Misses 2467 2411 -56
- Partials 1357 1343 -14Generated by Codecov Action |
alexander-alderman-webb
left a comment
There was a problem hiding this comment.
Didn't look too in-depth since it's test code
Parametrize existing aiomysql tests with span_streaming to cover both
static and stream trace lifecycle modes.
Fixes PY-2523
Fixes #6539