psycopg connection context manager drops X-Ray tracing wrapper, so cursor execute calls are not traced
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- aws, python
- Domain
- observability
Research direction
Start with the psycopg integration around patch() and XRayTracedConn, then reproduce the with conn as c: flow described in the issue. Done means the entered connection remains wrapped, its cursor is traced, and execute() produces the native database subsegment without manual instrumentation.
Written by the indexing model from the issue text.
Description
I am seeing a gap in the psycopg integration for aws-xray-sdk.
When patching psycopg succeeds, psycopg.connect() returns an XRayTracedConn proxy as expected. However, if the connection is used as a context manager with with conn as c:, the object returned from enter is the raw psycopg Connection, not the X-Ray wrapper. As a result, c.cursor(...) returns a raw psycopg Cursor, and execute() calls do not produce X-Ray subsegments.
Observed behavior
patch(("psycopg",), raise_errors=True) succeeds
psycopg.connect() returns aws_xray_sdk.ext.dbapi2.XRayTracedConn
entering the connection context manager unwraps the proxy
cursor() on that entered object returns psycopg.Cursor
execute() on the cursor is not traced
if I create an explicit subsegment manually, that subsegment appears in X-Ray, which confirms the request has a valid segment and the DB code path is running
Expected behavior
The psycopg X-Ray wrapper should preserve tracing across the connection context manager
with conn as c: should still return a wrapped connection, or otherwise ensure cursors created from the entered object are still traced
execute() should generate the native DB subsegment without needing manual instrumentation
Reproduction
Install aws-xray-sdk 2.15.0
Install psycopg 3.3.4 and psycopg-pool 3.2.7
Call patch(("psycopg",), raise_errors=True)
Call psycopg.connect(...)
Use the connection in a with block
Create a cursor and execute a query
Observe that the cursor is raw psycopg.Cursor and no DB subsegment is emitted
Environment details
aws-xray-sdk 2.15.0
psycopg 3.3.4
psycopg-pool 3.2.7
Python 3.12
Running in AWS Lambda
- Dominant language
- Python
- Stars
- 339
- Forks
- 148
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from aws/aws-xray-sdk-python
-
Difficulty 5/5 Over a week Newbie friendliness 1/100
aws/aws-xray-sdk-python#460 ·
-
Next releaseOpen
Difficulty 5/5 Over a week Newbie friendliness 20/100
aws/aws-xray-sdk-python#457 · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
aws/aws-xray-sdk-python#453 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
aws/aws-xray-sdk-python#452 · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
aws/aws-xray-sdk-python#451 · 2 comments · 1 reaction ·
All issues in aws/aws-xray-sdk-python
Similar issues
-
upstream update
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
conan-io/conan-center-index#31098 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
john-kurkowski/tldextract#382 ·
-
comp/tools duplicate P2 sweeper:risk-compatibility tool/mcp type/bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
NousResearch/hermes-agent#132042 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
deepset-ai/haystack#13092 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
feder-cr/invisible_playwright_mcp#1408 ·
Maintainers usually reply within 1 day