Skip to content

Feature | Surface batch information on the SqlClient diagnostic events #4545

Description

@PetarJerinic

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area\Open TelemetryUse for issues related to OTel: Tracing / MetricsUp-for-Grabs 🙌Issues that are ready to be picked up for anyone interested. Please self-assign and remove the label

    Projects

    Status
    In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions