[SqlClient] Add db.response.returned_rows - #4625
Conversation
Add experimental opt-in support for `db.response.returned_rows`. Resolves open-telemetry#3898.
Address feedback to handle per-connection statistics not per query.
Add more tests to extend patch coverage.
Add PR number.
Remove redundant `using`.
Restore using, but only for `netX.0`.
There was a problem hiding this comment.
Pull request overview
Adds experimental, opt-in support in OpenTelemetry.Instrumentation.SqlClient to emit the db.response.returned_rows span attribute (aligned to semconv v1.42.0), with documentation and test coverage updates.
Changes:
- Add
db.response.returned_rowssemantic convention constant and implement SqlClient listener logic to populate it when enabled. - Introduce an experimental env-var toggle (
OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_RECORD_RETURNED_ROWS) and corresponding option wiring/snapshotting. - Update SqlClient semantic conventions version references to v1.42.0 and add unit/integration tests covering the new behavior.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlClientTraceInstrumentationOptionsTests.cs | Adds unit tests for default behavior, env-var parsing, and returned-rows tagging scenarios. |
| test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlClientIntegrationTests.cs | Adds Linux Docker integration test validating returned-rows tagging end-to-end when enabled via env var. |
| test/OpenTelemetry.Instrumentation.SqlClient.Tests/MockCommandExecutor.cs | Extends mock diagnostic payloads to include Statistics for row-count scenarios. |
| test/OpenTelemetry.Instrumentation.SqlClient.Tests/FakeDbConnection.cs | Adds a lightweight fake IDbConnection for testing baseline/connection-statistics edge cases. |
| test/OpenTelemetry.Instrumentation.SqlClient.Tests/FakeDbCommand.cs | Adds a lightweight fake IDbCommand for testing returned-rows logic without real SqlClient. |
| src/Shared/SemanticConventions.cs | Adds AttributeDbResponseReturnedRows constant used by instrumentation/tests. |
| src/OpenTelemetry.Instrumentation.SqlClient/SqlClientTraceInstrumentationOptions.cs | Adds env-var parsing and internal option flag for enabling returned-rows tagging. |
| src/OpenTelemetry.Instrumentation.SqlClient/SqlClientInstrumentation.cs | Includes the new option in the options snapshot/aggregation logic. |
| src/OpenTelemetry.Instrumentation.SqlClient/README.md | Documents the experimental returned-rows feature and updates semconv links to v1.42.0. |
| src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlTelemetryHelper.cs | Updates the semantic conventions version to 1.42.0. |
| src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlClientDiagnosticListener.cs | Implements statistics baseline capture + delta calculation + span tagging for returned rows. |
| src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md | Adds unreleased entries for semconv bump and returned-rows feature. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Fix bug with delegate dispatch to get the statistics.
Bump to v1.43.0.
- Use a single `AsyncLocal` for the counts. - Add missing check for `Activity,IsAllDataRequested`.
Pull request dashboard statusWaiting on maintainers · refreshed 2026-08-14 16:20 UTC Merge when ready. Status above doesn't look right?
|
|
Deleted the associated branch by mistake 🤦♂️ |
- Fix issues with tracking using `AsyncLocal<T>`. - Fix broken semantic convention links. - Accept Visual Studio refactoring suggestions in touched files.
Remove redundant `IDisposable` implementation.
Bump to the latest version.
| > The span ends when the command finishes executing. For `ExecuteReader` this | ||
| > happens **before** the returned rows are read by the application, so the | ||
| > attribute reflects the rows observed at that point and may be `0` for queries | ||
| > whose results are consumed after the command returns. The value is most | ||
| > meaningful for `ExecuteNonQuery` (rows affected) commands. |
There was a problem hiding this comment.
Does it make sense to attempt to only apply the attribute when we think it will actually be meaningful? That is, don't apply it for ExecuteReader?
There was a problem hiding this comment.
Made some changes based on this feedback and others.
Better handle commands that don't return meaningful row counts.
- Do not include inserts, updates and deletes in returned row counts. - Add note to enable statistics for correct behaviour.
Combine note paragraphs.
Fixes #3898
Changes
db.response.returned_rows.db.client.operation.durationmetric reporting an incorrect duration for commands which are not recorded as spans.Merge requirement checklist
CHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)