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:
SqlCommand exposes no public member revealing batch state. The operation count exists only in the private _RPCList field.
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
Is your feature request related to a problem? Please describe.
A
SqlBatchis invisible toDiagnosticSourcesubscribers, 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 privateSqlCommandfield,_batchCommand, whichSetupBatchCommandExecute()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.SqlClientthis meansdb.operation.batch.sizecan't be emitted.Describe the solution you'd like
Expose the batch on the command diagnostic payloads in
Microsoft.Data.SqlClient.Diagnostics.Example:
Suggested classes where this would be present:
SqlClientCommandBefore,SqlClientCommandAfterandSqlClientCommandError.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
_RPCListfield, but the idea was rejected since any future change could break the functionality inOpenTelemetry.Instrumentation.SqlClient.Additional context
OpenTelemetry.Instrumentation.SqlClientcan be seen here (open-telemetry/opentelemetry-dotnet-contrib#2240)