Skip to content

feat(pymongo): Add span streaming support - #6253

Merged
ericapisani merged 5 commits into
masterfrom
py-2349-migrate-pymongo
May 12, 2026
Merged

feat(pymongo): Add span streaming support#6253
ericapisani merged 5 commits into
masterfrom
py-2349-migrate-pymongo

Conversation

@ericapisani

@ericapisaniericapisani commented May 11, 2026

Copy link
Copy Markdown
Member

Add span streaming support to the PyMongo integration. The legacy code path is preserved unchanged for non-streaming clients.

Fixes PY-2349
Fixes#6047

Add span streaming support to the PyMongo integration, mirroring the
pattern used in the asyncpg integration. When trace_lifecycle=stream is
enabled, spans are emitted using the new StreamedSpan API with OTel-style
attributes. The legacy code path is preserved for non-streaming clients.
Fixes PY-2349
Fixes#6047
@linear-code

Copy link
Copy Markdown

PY-2349

@ericapisani

Copy link
Copy Markdown
MemberAuthor

bugbot run

@ericapisani

Copy link
Copy Markdown
MemberAuthor

@sentry review

@github-actions

github-actionsBot commented May 11, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

146 passed | Total: 146 | Pass Rate: 100% | Execution Time: 22.83s

📊 Comparison with Base Branch

MetricChange
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 5.36%. Project has 14368 uncovered lines.
✅ Project coverage is 34.92%. Comparing base (base) to head (head).

Files with missing lines (1)
FilePatch %Lines
pymongo.py9.79%⚠️ 129 Missing
Coverage diff
@@ Coverage Diff @@## main #PR +/-##
==========================================
+ Coverage 30.12% 34.92% +4.8%
==========================================
Files 190 190 —
Lines 21907 22079 +172
Branches 7818 7424 -394
==========================================
+ Hits 6598 7711 +1113- Misses 15309 14368 -941- Partials 695 807 +112

Generated by Codecov Action

Comment threadsentry_sdk/integrations/pymongo.py

data: "Dict[str, Any]" = {"operation_ids": {}}
data["operation_ids"]["operation"] = event.operation_id
data["operation_ids"]["request"] = event.request_id

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are also kept for legacy reasons, are being removed for streamed spans.

@ericapisani
ericapisani marked this pull request as ready for review May 11, 2026 18:46
@ericapisani
ericapisani requested a review from a team as a code ownerMay 11, 2026 18:46

@sentrivanasentrivana left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, but we're using some deprecated attributes here, I've highlighted the ones I noticed but please have another look at the rest we're setting on the span first path as well 🙏🏻 Approving to not block.

Comment threadsentry_sdk/integrations/pymongo.py Outdated
Comment on lines +238 to +239
if type(span) is StreamedSpan:
span.status = SpanStatus.OK

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK is the default value for StreamedSpans, so technically we don't need to set it here

Comment threadsentry_sdk/integrations/pymongo.py Outdated
Comment on lines 96 to 97

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this is deprecated and superseded by SPANDATA.DB_NAMESPACE, so we should use that in span first

Comment threadsentry_sdk/integrations/pymongo.py Outdated
"db.name": event.database_name,
SPANDATA.DB_SYSTEM: "mongodb",
SPANDATA.DB_DRIVER_NAME: "pymongo",
SPANDATA.DB_OPERATION: event.command_name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SPANDATA.DB_OPERATION: event.command_name,
SPANDATA.DB_OPERATION_NAME: event.command_name,

https://getsentry.github.io/sentry-conventions/attributes/db/#db-operation

Comment threadsentry_sdk/integrations/pymongo.py Outdated
if has_span_streaming_enabled(client.options):
span_first_data = {
"db.name": event.database_name,
SPANDATA.DB_SYSTEM: "mongodb",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SPANDATA.DB_SYSTEM: "mongodb",
SPANDATA.DB_SYSTEM_NAME: "mongodb",



@pytest.mark.parametrize("with_pii", [False, True])
def test_transactions_span_streaming(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to not use the term transaction in the span streaming tests as there are no transactions anymore. Maybe just span or segment if we want to emphasize that a span is root-level.

Looks like we're mentioning transactions in this test name and then also when we're creating spans by hand in this test and others we're calling them test_transaction

if db_name is not None:
data[SPANDATA.DB_NAME] = db_name

server_address = event.connection_id[0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Accessing a non-indexable event.connection_id in _get_db_data raises a TypeError that is silently caught, causing the entire pymongo span to be dropped.
Severity: MEDIUM

Suggested Fix

Wrap the access to event.connection_id within a try...except TypeError block. This will prevent the unhandled exception from halting execution, allowing the span to be created even if peer address data cannot be extracted.

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: sentry_sdk/integrations/pymongo.py#L98
Potential issue: The function `_get_db_data` directly accesses `event.connection_id` to
retrieve database connection details. If `event.connection_id` is not an indexable type
(e.g., an integer), a `TypeError` is raised. This exception is caught and suppressed by
the `capture_internal_exceptions` context manager, which then prematurely exits the
`with` block. As a consequence, the associated Sentry span is never created, and the
entire pymongo event is silently dropped. This is a regression from previous behavior
where this error was handled, and a span was still created.

Did we get this right? 👍 / 👎 to inform future reviews.

Comment threadsentry_sdk/integrations/pymongo.py Outdated
Comment threadsentry_sdk/integrations/pymongo.py Outdated
Comment threadsentry_sdk/integrations/pymongo.py

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 30ba6da. Configure here.

Comment threadsentry_sdk/integrations/pymongo.py
@ericapisani
ericapisani merged commit edaa6d6 into masterMay 12, 2026
156 checks passed
@ericapisani
ericapisani deleted the py-2349-migrate-pymongo branch May 12, 2026 16:05
mgaligniana pushed a commit to mgaligniana/sentry-python that referenced this pull request Aug 9, 2026
Add span streaming support to the PyMongo integration. The legacy code
path is preserved unchanged for non-streaming clients.
Fixes PY-2349
Fixesgetsentry#6047
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate pymongo to span first

2 participants

@ericapisani@sentrivana