Repository navigation
Description
Is your feature request related to a problem? Please describe.
A SqlBatch is invisible to DiagnosticSource subscribers, so nothing outside SqlClient can distinguish a batch from a single command or determine how many operations it contained.
SqlBatch.ExecuteNonQuery() delegates the batch to a single private SqlCommand field, _batchCommand, which SetupBatchCommandExecute() puts into batch-RPC mode (SqlBatch.cs#L89-L94, #L220-L240).
For the subscriber, it has the following consequences:
SqlCommandexposes no public member revealing batch state. The operation count exists only in the private_RPCListfield.
AddBatchCommandoverwritesCommandText(SqlCommand.Batch.cs#L25), making only the last statement available.
For OpenTelemetry.Instrumentation.SqlClient this means db.operation.batch.size can't be emitted.
Describe the solution you'd like
Expose the batch on the command diagnostic payloads in Microsoft.Data.SqlClient.Diagnostics.
Example:
public IReadOnlyList<SqlBatchCommand>? BatchCommands { get; }Suggested classes where this would be present: SqlClientCommandBefore, SqlClientCommandAfter and SqlClientCommandError.
Alternatively, only operation count could be exposed, but in that case only the last statement of the batch would be present in query text
Describe alternatives you've considered
Use reflection to read _RPCList field, but the idea was rejected since any future change could break the functionality in OpenTelemetry.Instrumentation.SqlClient.
Additional context
- Observed on Microsoft.Data.SqlClient 7.0.2
- The impact on
OpenTelemetry.Instrumentation.SqlClientcan be seen here (open-telemetry/opentelemetry-dotnet-contrib#2240) - This looks like the extension case from Emit OpenTelemetry tracing #2210 comment: Emit OpenTelemetry tracing #2210 (comment)
- Happy to create a PR myself if the team is open to it.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn progress
Activity
github-actions commentedon Aug 16, 2026
🔍 Triage Summary
Area\EngineeringAnalysis
This feature request asks for
SqlBatchexecution context (batch commands or batch size) to be surfaced in theDiagnosticSourcepayloads (SqlClientCommandBefore,SqlClientCommandAfter,SqlClientCommandError) withinMicrosoft.Data.SqlClient.Diagnostics. Currently,SqlBatchdelegates to a private internalSqlCommandin batch-RPC mode, and the diagnostic payload only reflects the last overwrittenCommandText, making it impossible for OpenTelemetry instrumentation (or otherDiagnosticSourcesubscribers) to emitdb.operation.batch.size. The proposed fix is a publicIReadOnlyList<SqlBatchCommand>? BatchCommandsproperty on the diagnostic payload classes. This is a P2 feature with a clear, well-scoped design and direct ecosystem impact (OpenTelemetry).Next Steps
BatchCommandsproperty (or at minimum aBatchCommandCountproperty) toSqlClientCommandBefore,SqlClientCommandAfter, andSqlClientCommandErrorinMicrosoft.Data.SqlClient.Diagnostics.SqlBatchexecution inSqlBatch.SetupBatchCommandExecute/ the diagnostic write paths.netcore/ref/andnetfx/ref/) to reflect the new public API surface.cheenamalhotra commentedon Aug 17, 2026
Thanks for the detailed writeup @PetarJerinic — the analysis matches what I see in the code.
SqlBatchexecutes through a privateSqlCommandin batch-RPC mode,AddBatchCommandoverwritesCommandText, and the batch commands only exist in the private_RPCList, so there is currently no supported way for aDiagnosticSourcesubscriber to see the batch.Here is the API shape I'd like to go with — essentially your proposal:
Proposed API
Add one property to
SqlClientCommandBefore,SqlClientCommandAfterandSqlClientCommandError:Why this shape
SqlBatchCommandis already public onnet462,net8.0andnet9.0, so the surface addition is minimal.db.operation.batch.sizefrom.Countand usable per-commanddb.query.text, which the count-only alternative would not.SqlBatchitself on the payload instead, but that leaks a disposable, user-owned object into the diagnostic payload (SqlBatch.BatchCommandsthrows once disposed), so we prefer the list.Implementation notes
SqlCommand.BatchCommands, populated inSqlBatch.SetupBatchCommandExecute()from a cachedReadOnlyCollection<SqlBatchCommand>wrapper overSqlBatch.Commands— allocated once perSqlBatch, and it prevents subscribers from downcasting and mutating the live list.SqlDiagnosticListener.WriteCommandBefore/WriteCommandAfter/WriteCommandErrorpass it through to the payloads.null, so there is no added cost on the common path.IReadOnlyList<KeyValuePair<string, object>>view, appended at the last index.Countgoes 6 → 7 (Before) and 7 → 8 (After/Error); existing indices 0–6 keep their current meaning.src/Microsoft.Data.SqlClient/ref/Microsoft.Data.SqlClient.Diagnostics.csand thedoc/snippets/.../SqlClientDiagnostic.xmldocs.ExecuteNonQuery/ExecuteNonQueryAsync,ExecuteReader/ExecuteReaderAsync,ExecuteScalar/ExecuteScalarAsync) and assertBatchCommandsisnullfor plainSqlCommandexecution.We're happy for you to send the PR — please target this API shape and we'll review. Thanks for offering!
PetarJerinic commentedon Aug 18, 2026
@cheenamalhotra thanks for the quick feedback and additional implementation notes. I am starting work on it and will create a PR in the next few days.