Connect trace context with database commands
This guide describes how to extend database telemetry with trace context propagation and query parameter capture using OpenTelemetry for .NET.
Check Compatibility and Requirements
Before enabling trace context propagation or query parameter tracing, review the required runtime, configuration, and database conditions. These prerequisites ensure that OpenTelemetry features can operate correctly and safely in your environment.
Runtime Compatibility
Trace context propagation and query parameter tracing are supported only under specific runtime conditions. The following constraints apply at the .NET runtime level:
-
Only supported on .NET runtimes.
-
Requires use of
CommandType.Textfor database commands. -
Applies exclusively to applications using the
System.Data.SqlClientorMicrosoft.Data.SqlClientlibraries. See Requirements for .NET instrumentation for full compatibility details.
Environment Variables
To configure trace context propagation in .NET applications, use the following environment variable:
| Variable Name | Default Value | Description |
|---|---|---|
OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_CONTEXT_PROPAGATION | false | Enables trace context propagation. |
true to activate the corresponding feature.Enable Trace Context Propagation
To propagate trace context to the database, set the following environment variable:
OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_CONTEXT_PROPAGATION=true
This uses the SET CONTEXT_INFO command to attach traceparent information to the current connection. This results in an additional round-trip to the database.
SET CONTEXT_INFO, and enabling this feature introduces an additional round-trip per connection, which may affect performance.