fix(aws-lambda): Attach user info when streaming spans - #7429
Conversation
Codecov Results 📊✅ 130608 passed | ❌ 1 failed | ⏭️ 7126 skipped | Total: 137735 | Pass Rate: 94.83% | Execution Time: 477m 59s 📊 Comparison with Base Branch
➕ New Tests (1)View new tests
❌ Failed Tests
|
| File | Patch % | Lines |
|---|---|---|
| sentry_sdk/integrations/aws_lambda.py | 3.13% |
Coverage diff
@@ Coverage Diff @@
## master #PR +/-##
==========================================
- Coverage 90.26% 90.21% -0.05%
==========================================
Files 193 193 —
Lines 25729 25741 +12
Branches 9496 9510 +14
==========================================
+ Hits 23221 23220 -1
- Misses 2508 2521 +13
- Partials 1434 1436 +2Generated by Codecov Action
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit ffd1721. Configure here.
| def _get_user_from_event(aws_event: "dict[str, Any]") -> "dict[str, Any]": | ||
| identity = aws_event.get("requestContext", {}).get("identity") | ||
| if identity is None: | ||
| return {} | ||
|
|
||
| user_info: "dict[str, Any]" = {} | ||
|
|
||
| user_arn = identity.get("userArn") | ||
| if user_arn is not None: | ||
| user_info["id"] = user_arn | ||
|
|
||
| ip = identity.get("sourceIp") | ||
| if ip is not None: | ||
| user_info["ip_address"] = ip | ||
|
|
||
| return user_info |
There was a problem hiding this comment.
_get_user_from_event crashes when requestContext is None or non-dict
Guard requestContext and identity with isinstance(..., dict) before calling .get(); dict.get("requestContext", {}) still returns None when the key is present, and this helper now runs on the request path outside capture_internal_exceptions.
Evidence
_get_user_from_event()doesaws_event.get("requestContext", {}).get("identity"), which raisesAttributeErrorifrequestContextis explicitlyNoneor otherwise non-dict.- After the
identity is Nonecheck, it callsidentity.get(...)with no dict check, so a non-dict identity also raises. - The new streaming path calls this helper via
scope.set_user(...)outside the nearbycapture_internal_exceptions()block, so the exception can fail the Lambda invocation. - The same handler already special-cases non-dict
headersfor this reason, but user extraction does not.
Identified by Warden · code-review · QYJ-NUN

Add user info onto streamed spans since they don't go through the event processor.