Skip to content

Conversation

@Elyahou
Copy link
Contributor

@Elyahou Elyahou commented Jan 7, 2026

Description

Add commenter_for_all_spans option to SQLAlchemy and DBAPI instrumentations, enabling SQLCommenter to add SQL comments (including traceparent) for not sampled spans.

Motivation: By default, SQLCommenter only adds comments when span.is_recording() is True. This means not sampled spans (e.g., when using a sampling rate < 100%) don't get SQL comments. However, some users want traceparent propagated in SQL comments for all requests regardless of sampling decision, enabling database-side correlation with distributed traces.

Changes:

  • Added commenter_for_all_spans parameter (default: False) to both SQLAlchemy and DBAPI instrumentations
  • When enabled, SQL comments are added even for non-recording but valid spans
  • Span attributes are still only set when span.is_recording() is True (existing behavior preserved)

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Added unit tests for both instrumentations:

@Elyahou Elyahou requested a review from a team as a code owner January 7, 2026 21:56
@tammy-baylis-swi tammy-baylis-swi moved this to Ready for review in @xrmx's Python PR digest Jan 8, 2026
and (span.is_recording() or self._commenter_for_all_spans)
)

if should_comment:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to make these nested if-else blocks (here and in sqlalchemy) more concise? If the original code gets chopped up a bit then it's fine as long as the tests continue to pass 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I refactored it a little bit, let me know if it is better

@tammy-baylis-swi
Copy link
Contributor

This is looking good and I really appreciate the docs updates and comments so far. Just left one minor suggestion.

@Elyahou Elyahou changed the title feat: add commenter_for_nonrecording_spans option to enable SQLCommenter for non-recording spans feat: add commenter_for_all_spans option to enable SQLCommenter for non-recording spans Jan 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

2 participants