Skip to content

[SqlClient] Add db.response.returned_rows - #4625

Open
martincostello wants to merge 20 commits into
open-telemetry:mainfrom
martincostello:gh-3898
Open

[SqlClient] Add db.response.returned_rows#4625
martincostello wants to merge 20 commits into
open-telemetry:mainfrom
martincostello:gh-3898

Conversation

@martincostello

@martincostello martincostello commented Jul 1, 2026

Copy link
Copy Markdown
Member

Fixes #3898

Changes

  • Add experimental opt-in support for db.response.returned_rows.
  • Fix the db.client.operation.duration metric reporting an incorrect duration for commands which are not recorded as spans.
  • Update Semantic Conventions version to v1.44.0.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

Add experimental opt-in support for `db.response.returned_rows`.

Resolves open-telemetry#3898.
Address feedback to handle per-connection statistics not per query.
@github-actions github-actions Bot added the comp:instrumentation.sqlclient Things related to OpenTelemetry.Instrumentation.SqlClient label Jul 1, 2026
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.43750% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 77.65%. Comparing base (4dfc4f8) to head (7727832).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...ient/Implementation/SqlClientDiagnosticListener.cs 98.18% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4625      +/-   ##
==========================================
+ Coverage   77.57%   77.65%   +0.08%     
==========================================
  Files         468      468              
  Lines       19920    19978      +58     
==========================================
+ Hits        15452    15514      +62     
+ Misses       4468     4464       -4     
Flag Coverage Δ
unittests-Contrib.Shared.Tests 90.26% <ø> (ø)
unittests-Exporter.Geneva 56.72% <ø> (+0.05%) ⬆️
unittests-Exporter.InfluxDB 97.96% <ø> (ø)
unittests-Exporter.OneCollector 95.60% <ø> (ø)
unittests-Extensions 90.61% <ø> (ø)
unittests-Extensions.Enrichment 100.00% <ø> (ø)
unittests-Extensions.Enrichment.AspNetCore 86.27% <ø> (ø)
unittests-Extensions.Enrichment.Http 94.33% <ø> (ø)
unittests-Instrumentation.AWS 85.35% <ø> (ø)
unittests-Instrumentation.AspNet 79.01% <ø> (ø)
unittests-Instrumentation.AspNetCore 87.20% <ø> (ø)
unittests-Instrumentation.Cassandra 92.30% <ø> (ø)
unittests-Instrumentation.ConfluentKafka 82.83% <ø> (ø)
unittests-Instrumentation.ElasticsearchClient 80.36% <ø> (ø)
unittests-Instrumentation.EntityFrameworkCore 81.02% <ø> (ø)
unittests-Instrumentation.EventCounters 75.45% <ø> (ø)
unittests-Instrumentation.GrpcCore 91.12% <ø> (ø)
unittests-Instrumentation.GrpcNetClient 78.09% <ø> (ø)
unittests-Instrumentation.Hangfire 88.88% <ø> (ø)
unittests-Instrumentation.Http 75.61% <ø> (ø)
unittests-Instrumentation.Kusto 92.42% <ø> (ø)
unittests-Instrumentation.Owin 89.02% <ø> (ø)
unittests-Instrumentation.Process 100.00% <ø> (ø)
unittests-Instrumentation.Quartz 77.77% <ø> (ø)
unittests-Instrumentation.Remoting 65.59% <ø> (ø)
unittests-Instrumentation.Runtime 100.00% <ø> (ø)
unittests-Instrumentation.ServiceFabricRemoting 39.91% <ø> (ø)
unittests-Instrumentation.SqlClient 85.17% <98.43%> (+1.80%) ⬆️
unittests-Instrumentation.StackExchangeRedis 94.98% <ø> (ø)
unittests-Instrumentation.Wcf 82.52% <ø> (-0.13%) ⬇️
unittests-OpAmp.Client 86.96% <ø> (+0.08%) ⬆️
unittests-PersistentStorage 69.50% <ø> (+0.29%) ⬆️
unittests-Resources.AWS 72.78% <ø> (ø)
unittests-Resources.Azure 84.18% <ø> (ø)
unittests-Resources.Container 80.76% <ø> (ø)
unittests-Resources.Gcp 80.70% <ø> (ø)
unittests-Resources.Host 72.34% <ø> (ø)
unittests-Resources.OperatingSystem 77.16% <ø> (ø)
unittests-Resources.Process 90.90% <ø> (ø)
unittests-Resources.ProcessRuntime 80.76% <ø> (ø)
unittests-Sampler.AWS 96.05% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ion.SqlClient/Implementation/SqlTelemetryHelper.cs 100.00% <100.00%> (ø)
...trumentation.SqlClient/SqlClientInstrumentation.cs 85.41% <100.00%> (+0.47%) ⬆️
....SqlClient/SqlClientTraceInstrumentationOptions.cs 100.00% <100.00%> (ø)
...ient/Implementation/SqlClientDiagnosticListener.cs 86.12% <98.18%> (+4.36%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add more tests to extend patch coverage.
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md Outdated
Remove redundant `using`.
Restore using, but only for `netX.0`.
@martincostello
martincostello marked this pull request as ready for review July 1, 2026 13:27
@martincostello
martincostello requested a review from a team as a code owner July 1, 2026 13:27
Copilot AI review requested due to automatic review settings July 1, 2026 13:27

Copilot AI 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.

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_rows semantic 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.

@martincostello
martincostello marked this pull request as draft July 8, 2026 08:28
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlTelemetryHelper.cs Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/README.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/README.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/README.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/README.md Outdated
@martincostello
martincostello marked this pull request as ready for review July 8, 2026 09:04
@martincostello martincostello added the keep-open Prevents issues and pull requests being closed as stale label Jul 8, 2026
- Use a single `AsyncLocal` for the counts.
- Add missing check for `Activity,IsAllDataRequested`.
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Jul 18, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on maintainers · refreshed 2026-08-14 16:20 UTC

Merge when ready.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@martincostello
martincostello deleted the gh-3898 branch July 31, 2026 20:40
@martincostello
martincostello restored the gh-3898 branch July 31, 2026 21:25
@martincostello

Copy link
Copy Markdown
Member Author

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.
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlTelemetryHelper.cs Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/README.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/README.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/README.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.SqlClient/README.md Outdated

@alanwest alanwest left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Non-blocking question...

Comment on lines +271 to +275
> 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Made some changes based on this feedback and others.

@martincostello
martincostello added this pull request to the merge queue Aug 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Aug 6, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:instrumentation.sqlclient Things related to OpenTelemetry.Instrumentation.SqlClient keep-open Prevents issues and pull requests being closed as stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature request] Add db.response.returned_rows attribute to SqlClient instrumentation

4 participants