Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentation
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbQueryParameters.get -> bool
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbQueryParameters.set -> void
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbStatementForStoredProcedure.get -> bool
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbStatementForStoredProcedure.set -> void
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbStatementForText.get -> bool
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbStatementForText.set -> void
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.EnrichWithIDbCommand.get -> System.Action<System.Diagnostics.Activity!, System.Data.IDbCommand!>?
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.EnrichWithIDbCommand.set -> void
OpenTelemetry.Trace.TracerProviderBuilderExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
* Add the `db.query.summary` attribute and use it for the trace span name when opted
into using the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable.
([#3022](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3022))
* The `db.statement` and `db.query.text` attributes added when `SetDbStatementForText`
is `true` are now sanitized when using specific SQL-like EFCore providers.
* The `db.statement` and `db.query.text` attributes are now sanitized when using
specific SQL-like EFCore providers.
([#3022](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3022))
* The `SetDbStatementForStoredProcedure` and `SetDbStatementForText` properties have
been removed. Behaviors related to this option are now always enabled.
([#3072](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3072))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* The `SetDbStatementForStoredProcedure` and `SetDbStatementForText` properties have
been removed. Behaviors related to this option are now always enabled.
([#3072](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3072))
* **Breaking change**: The `SetDbStatementForStoredProcedure` and
`SetDbStatementForText` properties have been removed. Behaviors related
to this option are now always enabled.
([#3072](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3072))


## 1.12.0-beta.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ internal EntityFrameworkInstrumentationOptions(IConfiguration configuration)
this.EmitNewAttributes = databaseSemanticConvention.HasFlag(DatabaseSemanticConvention.New);
}

/// <summary>
/// Gets or sets a value indicating whether or not the <see cref="EntityFrameworkInstrumentation"/> should add the names of <see cref="CommandType.StoredProcedure"/> commands as the <see cref="Trace.SemanticConventions.AttributeDbStatement"/> tag. Default value: True.
/// </summary>
public bool SetDbStatementForStoredProcedure { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether or not the <see cref="EntityFrameworkInstrumentation"/> should add the text of <see cref="CommandType.Text"/> commands as the <see cref="Trace.SemanticConventions.AttributeDbStatement"/> tag. Default value: False.
/// </summary>
public bool SetDbStatementForText { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity from the db command.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,24 @@ public override void OnEventWritten(string name, object? payload)
switch (commandType)
{
case CommandType.StoredProcedure:
if (this.options.SetDbStatementForStoredProcedure)
{
DatabaseSemanticConventionHelper.ApplyConventionsForStoredProcedure(
activity,
commandText,
this.options.EmitOldAttributes,
this.options.EmitNewAttributes);
}

DatabaseSemanticConventionHelper.ApplyConventionsForStoredProcedure(
activity,
commandText,
this.options.EmitOldAttributes,
this.options.EmitNewAttributes);
break;

case CommandType.Text:
if (this.options.SetDbStatementForText)
{
// Only SQL-like providers support sanitization as we are not
// able to sanitize arbitrary commands for other query dialects.
bool sanitizeQuery = IsSqlLikeProvider(providerName);

DatabaseSemanticConventionHelper.ApplyConventionsForQueryText(
activity,
commandText,
this.options.EmitOldAttributes,
this.options.EmitNewAttributes,
sanitizeQuery);
}

// Only SQL-like providers support sanitization as we are not
// able to sanitize arbitrary commands for other query dialects.
bool sanitizeQuery = IsSqlLikeProvider(providerName);

DatabaseSemanticConventionHelper.ApplyConventionsForQueryText(
activity,
commandText,
this.options.EmitOldAttributes,
this.options.EmitNewAttributes,
sanitizeQuery);
break;

case CommandType.TableDirect:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,6 @@ services.AddOpenTelemetry()
.AddConsoleExporter());
```

### SetDbStatementForText

`SetDbStatementForText` controls whether the `db.statement` attribute is
emitted. The behavior of `SetDbStatementForText` depends on the runtime used,
see below for more details.

Query text may contain sensitive data, so when `SetDbStatementForText` is
enabled the raw query text is sanitized by automatically replacing literal
values with a `?` character.

> [!NOTE]
> Query sanitization is only supported for the following SQL-like providers:
>
> * Firebird
> * GCP Spanner
> * IBM DB2
> * Microsoft SQL Server
> * MySQL
> * Oracle
> * PostgreSQL
> * SQLite
> * Teradata

### SetDbQueryParameters

`SetDbQueryParameters` controls whether `db.query.parameter.<key>` attributes
Expand All @@ -183,7 +160,7 @@ following configuration.
```csharp
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddEntityFrameworkCoreInstrumentation(
options => options.SetDbStatementForText = true)
options => options.SetDbQueryParameters = true)
.AddConsoleExporter()
.Build();
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#nullable enable
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity!, string!, object!>?
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Filter.get -> System.Func<object!, bool>?
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SetDbQueryParameters.get -> bool
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SetDbQueryParameters.set -> void
Comment on lines 9 to 10
Copy link
Member

@alanwest alanwest Sep 5, 2025

Choose a reason for hiding this comment

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

Sorry, but I missed the PR this was introduced in. The db.query.parameter.* attribute is not stable and still considered in development. This means it could be renamed or perhaps even removed altogether.

Because of this, I do not think we should expose this configuration publicly in prep for the stable release. I'd be ok gating it behind an experimental environment variable or something.

Copy link
Member

Choose a reason for hiding this comment

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

Good catch. Issue created.

OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SetDbStatementForText.get -> bool
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SetDbStatementForText.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SqlClientTraceInstrumentationOptions() -> void
OpenTelemetry.Metrics.SqlClientMeterProviderBuilderExtensions
OpenTelemetry.Trace.TracerProviderBuilderExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#nullable enable
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity!, string!, object!>?
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Filter.get -> System.Func<object!, bool>?
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.RecordException.set -> void
8 changes: 8 additions & 0 deletions src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
listener.
([#3041](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3041))

* The `SetDbStatementForText` property has been removed. Behaviors related to this
Copy link
Member

Choose a reason for hiding this comment

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

Breaking change note

option are now always enabled.
([#3072](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3072))

* The `Enrich`, `Filter` and `RecordException` properties has been removed from
target frameworks where they were non-functional.
([#3072](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3072))

## 1.12.0-beta.2

Released 2025-Jul-15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ public override void OnEventWritten(string name, object? payload)
{
try
{
#if NET
if (options.Filter?.Invoke(command) == false)
{
SqlClientInstrumentationEventSource.Log.CommandIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
return;
}
#endif
}
catch (Exception ex)
{
Expand Down Expand Up @@ -162,15 +164,11 @@ public override void OnEventWritten(string name, object? payload)
break;

case CommandType.Text:
if (options.SetDbStatementForText)
{
DatabaseSemanticConventionHelper.ApplyConventionsForQueryText(
activity,
commandText,
options.EmitOldAttributes,
options.EmitNewAttributes);
}

DatabaseSemanticConventionHelper.ApplyConventionsForQueryText(
activity,
commandText,
options.EmitOldAttributes,
options.EmitNewAttributes);
break;

case CommandType.TableDirect:
Expand All @@ -179,6 +177,7 @@ public override void OnEventWritten(string name, object? payload)
}
}

#if NET
try
{
options.Enrich?.Invoke(activity, "OnCustom", command);
Expand All @@ -187,6 +186,7 @@ public override void OnEventWritten(string name, object? payload)
{
SqlClientInstrumentationEventSource.Log.EnrichmentException(ex);
}
#endif
}
}

Expand Down Expand Up @@ -265,10 +265,12 @@ public override void OnEventWritten(string name, object? payload)

activity.SetStatus(ActivityStatusCode.Error, exception.Message);

#if NET
if (options.RecordException)
{
activity.AddException(exception);
}
#endif
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void OnBeginExecute(EventWrittenEventArgs eventData)
if (activity.IsAllDataRequested)
{
var commandText = (string)eventData.Payload[3];
if (!string.IsNullOrEmpty(commandText) && options.SetDbStatementForText)
if (!string.IsNullOrEmpty(commandText))
{
var sqlStatementInfo = SqlProcessor.GetSanitizedSql(commandText);
if (options.EmitOldAttributes)
Expand Down
82 changes: 6 additions & 76 deletions src/OpenTelemetry.Instrumentation.SqlClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,76 +121,6 @@ For an ASP.NET application, adding instrumentation is typically done in the
This instrumentation can be configured to change the default behavior by using
`SqlClientTraceInstrumentationOptions`.

### SetDbStatementForText

`SetDbStatementForText` controls whether the `db.statement` attribute is
emitted. The behavior of `SetDbStatementForText` depends on the runtime used,
see below for more details.

Query text may contain sensitive data, so when `SetDbStatementForText` is
enabled the raw query text is sanitized by automatically replacing literal
values with a `?` character.

#### .NET

On .NET, `SetDbStatementForText` controls whether or not
this instrumentation will set the
[`db.statement`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#call-level-attributes)
attribute to the `CommandText` of the `SqlCommand` being executed when the `CommandType`
is `CommandType.Text`. The
[`db.statement`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#call-level-attributes)
attribute is always captured for `CommandType.StoredProcedure` because the `SqlCommand.CommandText`
only contains the stored procedure name.

`SetDbStatementForText` is _false_ by default. When set to `true`, the
instrumentation will set
[`db.statement`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#call-level-attributes)
attribute to the text of the SQL command being executed.

To enable capturing of `SqlCommand.CommandText` for `CommandType.Text` use the
following configuration.

```csharp
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSqlClientInstrumentation(
options => options.SetDbStatementForText = true)
.AddConsoleExporter()
.Build();
```

#### .NET Framework

On .NET Framework, there is no way to determine the type of command being
executed, so the `SetDbStatementForText` property always controls whether
or not this instrumentation will set the
[`db.statement`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#call-level-attributes)
attribute to the `CommandText` of the `SqlCommand` being executed. The
`CommandText` could be the name of a stored procedure (when
`CommandType.StoredProcedure` is used) or the full text of a `CommandType.Text`
query.

`SetDbStatementForText` is _false_ by default. When set to `true`, the
instrumentation will set
[`db.statement`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#call-level-attributes)
attribute to the text of the SQL command being executed.

To enable capturing of `SqlCommand.CommandText` for `CommandType.Text` use the
following configuration.

```csharp
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSqlClientInstrumentation(
options => options.SetDbStatementForText = true)
.AddConsoleExporter()
.Build();
```

> [!NOTE]
> When using the built-in `System.Data.SqlClient`, only stored procedure command
names will be captured. To capture query text, other command text and
stored procedure command names, you need to use the `Microsoft.Data.SqlClient`
NuGet package (v1.1+).

### SetDbQueryParameters

> [!NOTE]
Expand All @@ -199,10 +129,10 @@ NuGet package (v1.1+).
`SetDbQueryParameters` controls whether `db.query.parameter.<key>` attributes
are emitted.

Query parameters may contain sensitive data, so only enable `SetDbStatementForText`
Query parameters may contain sensitive data, so only enable `SetDbQueryParameters`
if your queries and/or environment are appropriate for enabling this option.

`SetDbQueryParameters` is _false_ by default. When set to `true`, the
`SetDbQueryParameters` is `false` by default. When set to `true`, the
instrumentation will set
[`db.query.parameter.<key>`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#span-definition)
attributes for each of the query parameters associated with a database command.
Expand All @@ -213,15 +143,15 @@ following configuration.
```csharp
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSqlClientInstrumentation(
options => options.SetDbStatementForText = true)
options => options.SetDbQueryParameters = true)
.AddConsoleExporter()
.Build();
```

### Enrich

> [!NOTE]
> Enrich is supported on .NET and .NET Core runtimes only.
> Enrich is available on .NET runtimes only.

This option can be used to enrich the activity with additional information from
the raw `SqlCommand` object. The `Enrich` action is called only when
Expand Down Expand Up @@ -258,7 +188,7 @@ access to `SqlCommand` object.
### RecordException

> [!NOTE]
> RecordException is supported on .NET and .NET Core runtimes only.
> RecordException is available on .NET runtimes only.

This option can be set to instruct the instrumentation to record SqlExceptions
as Activity
Expand All @@ -277,7 +207,7 @@ using var tracerProvider = Sdk.CreateTracerProviderBuilder()
### Filter

> [!NOTE]
> Filter is supported on .NET and .NET Core runtimes only.
> Filter is available on .NET runtimes only.

This option can be used to filter out activities based on the properties of the
`SqlCommand` object being instrumented using a `Func<object, bool>`. The
Expand Down
Loading
Loading